PDA

View Full Version : [AS3] Removing Child Arrays Error


Taufires
10-04-2011, 08:40 PM
Okay Guys,

I've been getting an error when I remove bullets and enemies in a new game I'm making. I get this error when my bullet collides with an enemy, which appears in the output panel.

It's throwing this error:
ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
at flash.display::DisplayObjectContainer/removeChild()
at Main/enemiesHitTest()
at Main/stage_onEnterFrame()

I've tried a lot of different ways of listening/running for the functions of enemiesHitTest and stage_onEnterFrame to try and stop the error from recurring.

Here's a small slice of my code which I think may be relevant to the problem. The code may look long but it's readability is quite simple.

public function Main()
{
addEventListener(Event.ADDED_TO_STAGE, init);

//Declaring the Player here
myPlayer = new Player();

}
private function init(e:Event):void
{

addEventListener(Event.ADDED_TO_STAGE, levelOne);
mainMenu.mainMenuPlayButton.addEventListener(Mouse Event.CLICK, levelOne);
mainMenu.mainMenuMoreButton.addEventListener(Mouse Event.CLICK, moreClick);
stage.addChild(mainMenu);

}
public function levelOne(e:Event):void
{
stage.removeChild(mainMenu);
stage.addEventListener(KeyboardEvent.KEY_DOWN, fireBullet);
stage.addEventListener(Event.ENTER_FRAME, stage_onEnterFrame);

//Player
stage.addChild(myPlayer);
myPlayer.x = 100;
myPlayer.y = 200;

//Enemy
stage.addChild(enemyEasy1_1);
enemyEasy1_1.x = 580;
enemyEasy1_1.y = 95;

stage.addChild(enemyEasy1_2);
enemyEasy1_2.x = 520;
enemyEasy1_2.y = 180;


}
public function fireBullet(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.SPACE)
{

playerBulletIndex++;
if (playerBulletIndex > 19)
{
playerBulletIndex = 0;
}
playerBullet = playerBulletArray[playerBulletIndex];
playerBullet.x = myPlayer.x + 1;
playerBullet.y = myPlayer.y;
stage.addChild(playerBullet);

}

}
public function stage_onEnterFrame(e:Event):void
{
moveBullets();
enemiesHitTest();
}
function moveBullets():void
{

for (var i:int = 0; i < 20; i++)
{
if (playerBulletArray[i].x > myPlayer.x)
{
playerBulletArray[i].x += 10;
}
}

}
function enemiesHitTest():void
{
//each of the enemies
for (var i:int = 0; i < 2; i++)
{
//Each of the bullets
for (var j:int = 0; j < 20; j++)
{
if (enemyEasy1Array[i].hitTestObject(playerBulletArray[j]) && hitTestDetectionBoolean == true)
{
trace("Hit!");
stage.removeChild(playerBulletArray[j]);
stage.removeChild(enemyEasy1Array[i]);

hitTestDetectionBoolean = false;


} else
{
hitTestDetectionBoolean = true;
}
}
}
}

Any Help would be greatly appreciated :D

henke37
10-04-2011, 09:10 PM
Obviously being removed from the stage isn't enough to prevent the object from coliding with each other. Remove them from the arrays too.