Be a Supporter!

Problem with collision

  • 125 Views
  • 2 Replies
New Topic Respond to this Topic
distortiono
distortiono
  • Member since: Dec. 3, 2013
  • Offline.
Forum Stats
Member
Level 01
Blank Slate
Problem with collision 2013-12-03 10:20:54 Reply

Hello, i'm trying to self learn as3 here, but I seem to meet some problem with collision detection... having some errors but don't seem to know how to get around it.

for each(var i:EnemyA in aEnemyA)
	{
		for each(var j:Bullet in aBullet)
		{
				if(aEnemyA[aEnemyA.indexOf(i)].hitTestPoint(aBullet[aBullet.indexOf(j)].localToGlobal(mcBulletPoint).x, 
										  aBullet[aBullet.indexOf(j)].localToGlobal(mcBulletPoint).y , true))
			   {
				    /*Remove Bullet off Screen if collides with Asteroid*/
					trace(aEnemyA);
					aBullet[aBullet.indexOf(j)].Destroy();
					aBullet.splice(j,1);
					aEnemyA[aEnemyA.indexOf(i)].Destroy();
					aEnemyA.splice(i,1);

			   }
		}
		
	}

I have an EnemyA class and I have pushed all off them onto an array and added them on stage... but now the index i seems to add an additional weird EnemyA Sprite onto the stage =\...

Previously i used a double for loop to do the splicing but I was given a #1010 anonymous error.

Sam
Sam
  • Member since: Oct. 1, 2005
  • Offline.
Forum Stats
Moderator
Level 19
Programmer
Response to Problem with collision 2013-12-03 11:13:13 Reply

It seems you don't understand how a for each in loop works. Each iteration, it will take the next EnemyA and assign it to "i". Similarly for "j" and Bullet.

You don't need to do this:

aEnemyA[aEnemyA.indexOf(i)].hitTestPoint

Because you already have "i" to work with (it is literally the next EnemyA instance in aEnemyA), and the above line is essentially doing what this would do:

i.hitTestPoint

Because of this, you're also doing your splice incorrectly. Splice when taking two parameters, takes two integers - the start position and end position. But i and j are of types EnemyA and Bullet - not integer - as mentioned above. Hopefully you can take the steps to fix this, it should be fairly obvious now.

distortiono
distortiono
  • Member since: Dec. 3, 2013
  • Offline.
Forum Stats
Member
Level 01
Blank Slate
Response to Problem with collision 2013-12-03 11:59:14 Reply

Ah! Thank you so much :) for the help, its been really helpful :) Been wracking my brains trying to understand what went wrong :)