0
votes

I am making a platform game in flash cs4 as3 and am having a lot of trouble getting my different classes to work right together. I have a bullet class, enemy class, and a bumper class which determines the boundaries for the enemy. When the bullet hits the enemy the enemies death animation plays and then both are removed from the game. When the last enemy on that level gets removed though, i get this error saying a term is undefined and has no properties. Also if I kill the enemies out of the order they are called things don't work properly my codes must have some errors. Here is my code between the bullet and enemy classes:

if (enemyList.length>0) {
        for (var i:int = 0; i < enemyList.length; i++) {
            if (bulletList.length>0) {
                for (var j:int = 0; j < bulletList.length; j++) {
                    if (enemyList[i].hitTestObject(bulletList[j])) {
                        trace("Bullet and Enemy are colliding");
                        enemyList[i].gotoAndPlay("dead");
                        bulletList[j].removeSelf();
                        enemyList[i].xSpeedConst=0;
                        enemyList[i].isDead = true;
                    } else {
                        enemyList[i].isDead = false;
                    }
                }
            }
        }
    }
}

and also between the player and enemy and bumper and enemy.

if (enemyList[k].isDead == false && enemyList.length>0) {
        for (var k:int = 0; k < enemyList.length; k++) {
            if (enemyList[k].isDead == false && bumperList.length>0) {
                for (var h:int = 0; h < bumperList.length; h++) {
                    if (enemyList[k].hitTestObject(bumperList[h])) {
                        enemyList[k].changeDirection();
                    }
                }
            }
        }
    }

    if (enemyList[m].isDead == false && enemyList.length>0) {
        for (var m:int = 0; m < enemyList.length; m++) {
            if (enemyList[m].hitTestObject(player)) {
                trace("player collided with enemy");
                currentHP-=5;
                if (currentHP<=0) {
                    currentHP=0;
                    trace("you died");
                    gotoAndStop(1);
                }
                updateHealthBar();
            }
        }
    }

Sorry it's kinda long I just feel like Im missing something obvious in here or maybe all my code is just poorly structured. Either way any help would be greatly appreciated thanks!

1
The outside of for loop if (enemyList[k].isDead == false && enemyList.length>0) { should be if (enemyList.length>0) {.Yasuyuki Uno
And I think else {enemyList[i].isDead = false;} is redundant. It sometimes overwrite isDead=true to false, causes doesn't dead the enemy.(ex. If bulletList[4] hits enemy, enemy is dead. if bulletList[5] doesn't hit the enemy, enemy revive.)Yasuyuki Uno
@Yasuyuki: it doesn't make too much sense to wrap that for loop into that if statement, because the body of the for loop runs once for every array element. If there are no elements, it will not be executed. The if statement checking the length of the array is redundant. It should be removed entirely.null
@Dan: Please add the code of the method removeSelf()null
Thanks for the input everyone here is the code of my removeSelf function on the enemy class public function removeSelf():void { trace("remove enemy"); removeEventListener(Event.ENTER_FRAME, loop); this.parent.removeChild(this); }Dan

1 Answers

0
votes

Don't you need to remove the bullet and enemy from the lists once a bullet hit an enemy? Because you do

bulletList[j].removeSelf();

which will probably remove the bullet from the display list, but the bullet reference will still stay in the bulletList and will be checked against on the next iteration.

Also your very first check should be

if (enemyList.length != 0 && bulletList.length != 0)

There is no point to loop trough all enemies if there are no bullets and vice versa :)