0
votes

I get this error once in a while for a specific object. For some reason, this issue seems to start when I spawn 2 of this object instead of one. I basically have enemies that drops coins and one enemy drops 2. When I pick them up at the same time I start getting this error.

public function removeCoin(){
        removeEventListener(Event.ENTER_FRAME, moveCoin);
        if(this.parent){
            this.parent.removeChild(this);
        }
        parentMC.level.spawnedCoins.splice(this, 1);
}

This is the function called by the collision check when there is a collision between the player and the coin. The issue usually starts when I pick up two coins at once from this function.

var dropCoin:Number = Math.random() * 100;
    if(dropCoin > 40){
        var newCoin1:coin = new coin(parentMC);
        var newCoin2:coin = new coin(parentMC);
        newCoin1.x = x+7;
        newCoin1.y = y;
        parentMC.level.levelObjects.addChild(newCoin1);
        parentMC.level.spawnedCoins.push(newCoin1);
        newCoin2.x = x-7;
        newCoin2.y = y;
        parentMC.level.levelObjects.addChild(newCoin2);
        parentMC.level.spawnedCoins.push(newCoin2);
     }

Edited the code.

2

2 Answers

1
votes

That error means that the item you're trying to remove from the display list (by calling removechild) either isn't on the display list, or isn't a child of the object your calling removeChild on.

Without analyzing all your code, a quick check can fix your problem likely.

Change you existing chunk of code:

if(this != null){
   parentMC.lvl1.levelObjects.removeChild(this);
}

to this:

if(this.parent){
 this.parent.removeChild(this);
}

This checks if 'this' has a parent, if so, it removes itself from it's parent.

0
votes

I think your problem might be:
parentMC.level.spawnedCoins.splice(this, 1);

If spawnedCoins is just an array then splice should take 2 integer args startIndex and deleteCount relevant adobe help page

By passing an object what I think is happening is that it is casting the object to an int, value of '1' (i.e. not null).

What I think you want instead is parentMC.level.spawnedCoins.splice(parentMC.level.spawnedCoin.indexOf(this), 1);