Copy link to clipboard
Copied
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.
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.
Copy link to clipboard
Copied
when removing elements, use a reverse for-loop AND remove (splice) those elements from your array.
Copy link to clipboard
Copied
I was/am using a reverse for loop:
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..
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.
Copy link to clipboard
Copied
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.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now