Returned object array and Collide image
BlitzMax Forums/BlitzMax Beginners Area/Returned object array and Collide image
| ||
| Hello! I have a problem with CollideImage and the return object array. This code in the Method Test_Collision2() throws an error: Local temp_ball:Tball temp_ball = Tball(anz_collisions[x]) I think it wrong Casting. How can i do it right? Help welcomed Greetings sky009
Strict
Const C_WIDTH = 1024
Const C_HEIGHT = 768
Const C_ANZBALL = 100
Const C_ANZBALL_TYP = 1
Graphics C_WIDTH, C_HEIGHT, 32
Local current:Int, old_FPS_time:Int, FPS:Int, CurrentFPS:Int
Type Tball
Field ball:timage
Field x:Float
Field y:Float
Field dx:Float
Field dy:Float
Field sp:Float
Method New()
ball = temp_image
x = Rand(C_WIDTH - 24)
y = Rand(C_HEIGHT - 24)
dx = Rndfloat() 'Float random zwischen 0 und ...
dy = Rndfloat() 'hans.f=Random(8)/10+0.4
sp = Rndfloat() * 2 + 0.3
End Method
Method Move()
x = x + dx * sp
y = y + dy * sp
End Method
Method Test_Screen_Limit()
If y < 0 Then dy = 1
If y > C_HEIGHT - 24 Then dy = -1
If x < 0 Then dx = 1
If x > C_WIDTH - 24 Then dx = -1
End Method
Method Test_Collision()
For Local temp_ball:Tball = EachIn ballList
If temp_ball <> Self
ResetCollisions
CollideImage temp_ball.ball, temp_ball.x, temp_ball.y, 0, 0, 1
If CollideImage(ball, x, y, 0, 1, 0)
If x < temp_ball.x
dx = -1
temp_ball.dx = 1
EndIf
If x > temp_ball.x
dx = 1
temp_ball.dx = -1
EndIf
If y < temp_ball.y
dy = - 1
temp_ball.dy = 1
EndIf
If y > temp_ball.y
dy = 1
temp_ball.dy = -1
EndIf
EndIf
EndIf
Next
End Method
Method Test_Collision2()
Local anz_collisions:Object[]
ResetCollisions
For Local temp_ball:Tball = EachIn ballList
If temp_ball <> Self
'Write to Writemask
CollideImage temp_ball.ball, temp_ball.x, temp_ball.y, 0, 0, 1
EndIf
Next
'Test Collision
anz_collisions = CollideImage(ball, x, y, 0, 1, 0)
If anz_collisions
For Local x = 0 To Len(anz_collisions) -1
Local temp_ball:Tball
temp_ball = Tball(anz_collisions[x])
If x < temp_ball.x
dx = -1
temp_ball.dx = 1
EndIf
If x > temp_ball.x
dx = 1
temp_ball.dx = -1
EndIf
If y < temp_ball.y
dy = - 1
temp_ball.dy = 1
EndIf
If y > temp_ball.y
dy = 1
temp_ball.dy = -1
EndIf
Next
EndIf
End Method
End Type
' Create Ball Image
SetClsColor 0, 0, 0
SetMaskColor 0, 0, 0
SetColor 0, 0, 255
DrawOval 100,100, 24,24
Global temp_image:timage = CreateImage(24, 24)
GrabImage temp_image, 100, 100
' Create Ball List
Global ballList:TList = CreateList()
Local x:Int
For x = 1 To C_ANZBALL
Local temp_ball:Tball = New Tball
ListAddLast ballList, temp_ball
Next
SetColor 255, 255, 255
While Not KeyDown(KEY_ESCAPE)
Cls
For Local ball:Tball = EachIn ballList
DrawImage ball.ball, ball.x, ball.y
ball.Move
ball.Test_Screen_Limit
ball.Test_Collision2
Next
'Test FPS
current = MilliSecs()
If current >= old_fps_time + 1000
old_fps_time = current
FPS = CurrentFPS
CurrentFPS = 1
Else
CurrentFPS = CurrentFPS + 1
EndIf
DrawText "Balls: " + C_ANZBALL +" FPS: " + FPS, 100, 100
Flip
FlushMem
Wend
|
| ||
| Hi, When you use CollideImage to write to a collision layer, you must provide an object to be returned when a later collision occurs. You don't appear to be doing this anywhere. |
| ||
| Try this link it has some collision helper functions. http://www.blitzmax.com/Community/posts.php?topic=41263 |
| ||
| Hi! I thought with this peace of code i provide the object.
...
Local anz_collisions:Object[]
ResetCollisions
For Local temp_ball:Tball = EachIn ballList
If temp_ball <> Self
'Write to Writemask
CollideImage temp_ball.ball, temp_ball.x, temp_ball.y, 0, 0, 1
EndIf
Next
'Test Collision
anz_collisions = CollideImage(ball, x, y, 0, 1, 0)
...
@Force The helper funktions are working (see Method Test_Collision). I understand, that CollideImage returns a object array. But how is the returned array managed? (The object array confuses me a litle bit :-) ) In the code above anz_collisions is a object array. Probably 3 collisions are detected (anz_collisions [0]-[2]). Is the returned object a Tball type?? (in the above code) I thought local temp_ball:Tball = Tball(anz_collisions[0]) works. But it does´nt. A little tip, please. Perhaps i mess it all around. Greetings sky009 |