0

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.

oliboon
  • 351
  • 1
  • 5
  • 19

2 Answers2

1

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.

BadFeelingAboutThis
  • 14,445
  • 2
  • 33
  • 40
  • A little tip, this can never be null so there's no point to checking for that. – BadFeelingAboutThis Jul 26 '12 at 21:44
  • I've tried your method and I'm not getting the error at all. However, only one of the coin is picked up and the other just stays there until it de-spawn when it goes past the bottom limit. I receive no error at all. – oliboon Jul 26 '12 at 22:40
  • Try putting a trace statement in your switch. so right after 'case 1:' put 'trace(this);' this will confirm if your function is getting called for each coin. Since you don't show how removeCoin is called, it's hard to know what you're actual problem is - my guess is that your removeCoin function isn't getting called properly. – BadFeelingAboutThis Jul 26 '12 at 23:06
  • Also not really sure the point of your switch statement in the removeCoin() method. – BadFeelingAboutThis Jul 26 '12 at 23:09
  • I will try that and make sure the function is called for each coin. The switch statement is basically so I can switch level but it's currently being changed. Thank you. – oliboon Jul 26 '12 at 23:18
  • So I've tried the trace and it seems to be called for each coins but it seems there's one that is not removed as a child. The same code works when that coin gets past the screen limit. – oliboon Jul 26 '12 at 23:49
  • I've tried the trace elsewhere and it seems to be the issue because it's not called each time for the `if(this.parent) this.parent.removeChild(this)` I don't know why since it has worked sometimes for the first 2 coins then it can only pick one. – oliboon Jul 26 '12 at 23:56
  • I've modified it and changed the coin system so it's not an issue anymore. Thanks for the help anyway, it helped a lot. – oliboon Jul 29 '12 at 01:15
0

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);

DFreeman
  • 542
  • 6
  • 13
  • This seems to be the issue. It had been changed to another method but this will help for sure. Thank you. – oliboon Jul 30 '12 at 19:14