Be a Supporter!

as2 collision detection issue

  • 232 Views
  • 2 Replies
New Topic Respond to this Topic
Bum-Secks
Bum-Secks
  • Member since: Oct. 12, 2005
  • Offline.
Forum Stats
Member
Level 04
Blank Slate
as2 collision detection issue 2010-10-19 20:20:28 Reply

So i'm working on a platforming game, and i can't get my bullets to collide with my enemies properly.
Here's what i'm using:

for(i=0;i<99;i++){
if (_root["enemy"+i].hitTest (_root["bullet"+i]._x, _root["bullet"+i]._y, true)){                      
_root["bullet"+i].removeMovieClip();
	}
}

This works fine on the walls, but not enemies. Is it because they're both using the same variable"i"? I tried adding another "for" loop to see if i could give a different variable to the bullets, but the test lagged so bad i couldn't even see if it worked =(
The strange thing is the collision does work once, but only on the enemy at the edge of the map. After that one shot works the rest just go right through, it's driving me crazy!

Fion
Fion
  • Member since: Aug. 21, 2005
  • Offline.
Forum Stats
Member
Level 39
Blank Slate
Response to as2 collision detection issue 2010-10-19 21:13:02 Reply

You are right about it being because they are using the same iterator 'i', see in this case you test if enemy1 is touching bullet1, if enemy2 is touching bullet2 and so on like that only testing one bullet against one enemy. Like you said to test them all you'll need a loop within a loop, if that lags too much then you'll have to come up with an alternate method to do your hitTesting, such as seperating the objects into areas and only testing against ones which are within a certain range of each other.


.

BBS Signature
Bum-Secks
Bum-Secks
  • Member since: Oct. 12, 2005
  • Offline.
Forum Stats
Member
Level 04
Blank Slate
Response to as2 collision detection issue 2010-10-19 22:02:39 Reply

At 10/19/10 09:13 PM, Fion wrote: You are right about it being because they are using the same iterator 'i', see in this case you test if enemy1 is touching bullet1, if enemy2 is touching bullet2 and so on like that only testing one bullet against one enemy.

Thanks man, i think i got this now =)