Problems with the ImageCollision function

BlitzMax Forums/BlitzMax Programming/Problems with the ImageCollision function

En929(Posted 2009) [#1]
I'm trying to make a game like pac-man. I'm trying to make it so that when my character collides with an apple on screen, the apple disappears (as does when pac-man eats one). But, everytime my character collides with the apple, I get a message that says "Object does not exist." Thus, what do I need to do to make this error go away? My code looks like this:



Graphics 640, 480, 16, 2
SetBuffer BackBuffer()

Eatum = LoadImage ("Eatum.png")

apple = LoadImage("Apple.png")


Type Eatum

Field x
Field y

End Type



Type apple

Field x
Field y

End Type


For z = 1 To 5
a.apple = New apple

a\x = -100 + 50 * z
a\y = 150


Next


While Not KeyDown (1)

Cls

DrawImage (Eatum, x,y)

DrawImage (apple, a\x,a\y)


If ImagesCollide (Eatum,x,y,0,apple, a\x,a\y,0) Then
Delete a


EndIf


If KeyDown(203) Then x = x - 3
If KeyDown(205) Then x = x + 3
If KeyDown(200) Then y = y - 3
If KeyDown(208) Then y = y + 3



Flip

Wend


ImaginaryHuman(Posted 2009) [#2]
a.apple = New apple

You have this in a loop, but each time the loop iterates the newly created apple object overwrites the previous one. You're trying to store all 5 apples in 1 `a` variable.

You need an array of apples ie:

local Apples:Apple[5]
for z=0 to 4
Apples[z] = New apple
Apples[z]\x = -100 + 50 * z
Apples[z]\y = 150
Next