Skip to main content
Inspiring
October 10, 2016
Answered

Removing element from stage causes error, only if two or more of element exist.

  • October 10, 2016
  • 1 reply
  • 385 views

Okay so I've taken a long break from ActionScript, but I didn't think this would be a very big issue.

I'm making a dummy game right now, and whenever the player object fires more than a single bullet.. The game crashes with an error.

But if they shoot a bullet, wait for it to disappear, then shoot another, it's fine.

The error I get is:

ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.

  at flash.display::DisplayObjectContainer/removeChild()

  at Game/moveBullet()[Game.as:148]

  at Game/runGame()[Game.as:54]

  at flash.utils::Timer/_timerDispatch()

  at flash.utils::Timer/tick()

And here is the related code:

  for(var j:int = 0; j < bullets.length; j++) {

  if(bullets.canRemove) {

  stage.removeChild(bullets);

  //trace(bullets.parent);

  }

  }

Now when I commented out the removeChild, and used the trace statement.. It said [object Stage] for the parent. So that shouldn't be an issue.

I have the items removing like this because I thought it was my game logic that was causing the failure. So I made it set a boolean in the main loop to true, and only if that boolean is true it should attempt to remove the object.

Here is the game logic:

  for (var i:int = bullets.length; i >= 0; i--)

  {

  if (bullets != null)

  {

  for (var p:int = 0; p < test.length; p++)

  {

  if (bullets.hitTestObject(test

))

  {

  trace("Nailed it");

  stage.removeChild(test

);

  //stage.removeChild(bullets);

  //fired--;

  bullets.canRemove = true;

  }

  }

  bullets.x +=  3;

  if (bullets.x > stage.stageWidth)

  {

  //bullets.parent.removeChild(bullets);

  //fired--;

  bullets.canRemove = true;

  }

  }

  }

I hope someone can figure this out.. It's frustrating me.

This topic has been closed for replies.
Correct answer WatsonjWT2013

I managed to fix the issue.

What I did was move the 'death' of the bullet to it's own function:

private function removeBullet(bullet:Bullet)
{
bullet.remove();
bullets.splice(bullets.indexOf(bullet), 1);
fired--;
}

And then I created this function in the Bullet.as file:

  public function remove()

  {

  this.parent.removeChild(this);

  }

Now they are removing properly.

1 reply

kglad
Community Expert
Community Expert
October 11, 2016

when removing elements, use a reverse for-loop AND remove (splice) those elements from your array.

Inspiring
October 11, 2016

I was/am using a reverse for loop:

  1. for (var i:int = bullets.length; i >= 0; i--)

It was only after it was not working that I created the second loop (that wasn't reversed).

Using splice doesn't solve my problem though.

Okay.. I have to use pastebin because the on-site editor is breaking when I paste my code..

Pastebin - Game Code

So with this code in place, I get no errors. But the game still freezes when there's more than 1 bullet on screen and one exits the screen.

If I remove the bit where:

bullets = bblt;

Then it "Works" - as in the bullets will stay off screen, not delete from stage (but that code isn't even added so). You're able to fire more, but they just pile outside the frame.. If the bullet was more than just a square, that'd be a disaster for performance.

WatsonjWT2013AuthorCorrect answer
Inspiring
October 11, 2016

I managed to fix the issue.

What I did was move the 'death' of the bullet to it's own function:

private function removeBullet(bullet:Bullet)
{
bullet.remove();
bullets.splice(bullets.indexOf(bullet), 1);
fired--;
}

And then I created this function in the Bullet.as file:

  public function remove()

  {

  this.parent.removeChild(this);

  }

Now they are removing properly.