imagecollide / type deletion

Blitz3D Forums/Blitz3D Beginners Area/imagecollide / type deletion

Bremer(Posted 2003) [#1]
I have some problems with my code for a new game I'm making. Its the first time that I'm using types and the following code produces an error message:

[code]
for e.enemy = each enemy
for b.bullet = each bullet
if imagerectcollide(enemyImg,e\x,e\y,0,b\x,b\y,8,1) then
delete b
delete e
end if
next
next
[\code]

It gives me this error: Object does not exist!

What am I doing wrong?


DJWoodgate(Posted 2003) [#2]
You are deleting e before you are finished with it. Set a flag and delete e in the outer loop.


DarkNature(Posted 2003) [#3]
its because the loops are trying to perform actions on objects which have been deleted (the enemy object).

quickest solution:

add a field to the enemy type like
 field dead 
and flag it 'true' if imagescollide is satisfied. like this:

[code]
if imagerectcollide(enemyImg,e\x,e\y,0,b\x,b\y,8,1) then
e\dead=true
delete b
[code]

next, check whether e\dead=true outside of this loop and, if so delete it.

i'm rambling now, so i'll just show you :)

[code]
for e.enemy = each enemy
for b.bullet = each bullet
if imagerectcollide(enemyImg,e\x,e\y,0,b\x,b\y,8,1) then
e\dead=true
delete b
end if
next
if e\dead then delete e
next
[\code]

this should work.

have fun.


Bremer(Posted 2003) [#4]
Thanks guys, I didn't think about that, well that just goes to tell that after hours of programming you tend to overlook the most obvious. Maybe now I can get on with my game.


Curtastic(Posted 2003) [#5]
or you could just exit the bullet loop like:
delete e
exit


Bremer(Posted 2003) [#6]
well, yes I guess I could, but what if more than one bullet hits the enemy. I would like it to remove any bullets hitting. I changed my code as per DarkNature and DJWoodgate and it worked out just fine. Thanks again guys.