Removing element from stage causes error, only if two or more of element exist.
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.
