I'm confused. :S

Subscribe to I'm confused. :S 5 posts

avatar for pugzy pugzy 21 posts
Flag Post

My function is supposed to check the collisions between the bullets and enemies, and for the first enemies, or so, it does. But then I get, “Error #1010: A term is undefined and has no properties. At Main.as:44.”

		function checkCol()
		{
			for (var i = 0; i < EnemyList.length; i++)
			{
				for (var q = 0; q < BulletList.length; q++)
				{
					if (EnemyList[i].hitTestObject(BulletList[q]))
					{
						EnemyList[i].updateMe(BulletList[q].returnDamage());
						 
						if (EnemyList[i].returnHealth() <= 0)
						{
							if(this.contains(EnemyList[i]))
							{
								EnemyList[i].removeListeners();
								this.removeChild(EnemyList[i]);
							}
							EnemyList.splice(i,1); 
								i--;
						}
						stage.removeChild(BulletList[q]);
						BulletList.splice(q,1);
						q--;
					}
				}
			}
		}

Line 44 is

if (EnemyList[i].hitTestObject(BulletList[q]))
 
avatar for Aaants Aaants 161 posts
Flag Post

The logic of it is a bit off, you should probably break out of the loop(s) when a hit is found. Imagine you have 1 enemy and 10 bullets. If the first bullet hits and kills the only enemy, the loop is going to check the other 9 bullets against EnemyList[-1]

 
avatar for DrYoshiyahu DrYoshiyahu 678 posts
Flag Post

As Aaants said, you should break out of the loop when an enemy or bullet is destroyed, what I do though is I delete the enemies in one go after all the bullets have checked them all. It gets pretty messy looping through bullets and enemies and collisions and listeners and splicing and children all at the same time. I’ve been there.

for (loop) {
    if (obj.hitTest) {
        if (enemyHealth<=0) {
            obj.tobeDeleted = true;
        }
    }
}

//Now that the bullet collision loop is over:

for (loop) {
    if (obj.tobeDeleted == true) {
        deleteObj()
    }
}
 
avatar for Aesica Aesica 972 posts
Flag Post

Try looping through your arrays from last to first instead of first to last.

 
avatar for jasonjie88 jasonjie88 302 posts
Flag Post

I would use an ENTER_FRAME listener which triggers the loop if EnemyList.numChildren > 0. Also I would alo constantly check if there are bullets on the stage through the listener. Then remove the bullets when there are none, i.e. BulletList.length = 0. Trigger the listener again when the player shoots, and use a conditional (if(BulletList.length > 0)) to not trigger the listener multiple times.