00:00
00:00
Newgrounds Background Image Theme

Kingkavvvv just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

Removing Movieclips In A Array(as3)

573 Views | 5 Replies
New Topic Respond to this Topic

I'm trying to make a space shooter game, I've managed to get bullets firing from the ship. I want the bullets to be removed when they reach the top of the screen. Here is the code I have:

for each(var playerBullet:PlayerBullet in playerBullets)
{
playerBullet.move();
if(playerBullet.y<0)
{
stage.removeChild(playerBullet);
}
}

This brings up error:
Error #2025: The supplied DisplayObject must be a child of the caller
The playerBullet.move() code works perfectly fine but trying to remove the bullets brings up errors. Can anybody give me tips to fix this? Thanks in advance

Response to Removing Movieclips In A Array(as3) 2014-08-07 20:14:17


At 8/7/14 07:59 PM, 20max14 wrote: The playerBullet.move() code works perfectly fine but trying to remove the bullets brings up errors. Can anybody give me tips to fix this? Thanks in advance

The bullet you remove from the screen is still in the array, so it's still being checked to see if it's offscreen.

Response to Removing Movieclips In A Array(as3) 2014-08-07 20:18:30


Welcome to the forum =)

Do not add things to stage.
You can rtfm here:
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/DisplayObjectContainer.html#addChild%28%29

So, generally, objects should not be added to the Stage, directly, at all.

So whenever you write this:

stage.addChild(...)

write this instead:

addChild(...)

The answer to your removal problem is the title of this thread: you remove objects that are in an array, but you do not remove them in an array.
Therefore they are evaluated again the next time this loop runs.
To solve this, you should remove the object from the array, too.
The Array class provides methods to do that.

Response to Removing Movieclips In A Array(as3) 2014-08-07 20:33:19


Thanks guys I managed to get the bullets to remove themselves, I think I've removed them from the array properly as well. However the game still slows down after a lot of bullets have been fired. How can I stop this from happening?
Here is the FLA and all relevant files
http://speedy.sh/SsGfU/Shooter.rar

Response to Removing Movieclips In A Array(as3) 2014-08-07 21:55:51


At 8/7/14 08:33 PM, 20max14 wrote: Thanks guys I managed to get the bullets to remove themselves, I think I've removed them from the array properly as well. However the game still slows down after a lot of bullets have been fired. How can I stop this from happening?
Here is the FLA and all relevant files
http://speedy.sh/SsGfU/Shooter.rar

I doubt anyone is gonna download that. Just post your code. Your fla doesn't matter.

Response to Removing Movieclips In A Array(as3) 2014-08-08 02:39:15


At 8/7/14 09:55 PM, MSGhero wrote: I doubt anyone is gonna download that. Just post your code. Your fla doesn't matter.

I managed to get it working, thanks anyway