Error #2025:The supplied DisplayObject must be a child of the caller - Removing Object from Array
Hi guys, I'm pretty new to as3 and I'm trying to make a game where the player supposedly clicks on the stage and 3 towers which I've spawned dynamically should shoot towards the area. Everything works in terms of tower rotation, etc, but the bullets will not be removed from the stage when I exit the level into another scene. The boundary checking is fine, too.
Here's a part of the code in the Main.as file.
private function clickTower1(e:MouseEvent😞void
{
for each (var tower1:mcTower1 in tower1Array)
{
var newLaser1:mcLaser1 = new mcLaser1();
newLaser1.rotation = tower1.rotation;
newLaser1.x = tower1.x + Math.cos(newLaser1.rotation * Math.PI / 180) * 40;
newLaser1.y = tower1.y + Math.sin(newLaser1.rotation * Math.PI / 180) * 40;
newLaser1.addEventListener(Event.ENTER_FRAME, laser1Handler);
tower1BulletArray.push(newLaser1); stage.addChild(newLaser1);
}
}
private function laser1Handler(e:Event😞void
{
//Make laser move in direction of turret.
var newLaser1:MovieClip = e.currentTarget as MovieClip;
newLaser1.x += Math.cos(newLaser1.rotation * Math.PI / 180) * laser1Speed;
newLaser1.y += Math.sin(newLaser1.rotation * Math.PI / 180) * laser1Speed;
//Boundary checking if (newLaser1.x < -50 || newLaser1.x > 800 || newLaser1.y > 600 || newLaser1.y < -50)
{
newLaser1.removeEventListener(Event.ENTER_FRAME, laser1Handler); stage.removeChild(newLaser1);
tower1BulletArray.splice(0, 1);
}
}I have a function called exitLevel, which basically, as the name states, exits the level when a button is clicked. It worked perfectly before I started coding the bullets.
private function exitLevel(e:MouseEvent😞void
{
stage.frameRate = 6;
gamePaused = false;
clearLevel();
gotoAndStop(1, 'exitLevel');
btnExitLevel.addEventListener(MouseEvent.CLICK, levelSelect1);
}
private function clearLevel():void
{
stage.removeEventListener(Event.ENTER_FRAME, update);
stage.removeChild(buttonCreep1); stage.removeChild(buttonCreep2);
for (var i = creep1Array.length - 1; i >= 0; i--)
{
removeChild(creep1Array[i]);
creep1Array.splice(i, 1);
//trace ("Creep1 Removed");
}
for (var j = creep2Array.length - 1; j >= 0; j--)
{
removeChild(creep2Array[j]);
creep2Array.splice(j, 1);
//trace ("Creep2 Removed");
}
for (var k = tower1Array.length - 1; k >= 0; k--)
{
removeChild(tower1Array[k]); tower1Array.splice(k, 1);
}
for (var l = tower1BulletArray.length - 1; l >= 0; l--)
{
stage.removeChild(tower1BulletArray[l]);
tower1BulletArray.splice(0, 1);
}
}After debugging, it says the error is at the end, where i try to remove the child from the stage. What is wrong? Sorry, I'm a beginner at as3 so any answers might have to be spoonfeeding... I'll try to learn and understand, though. Thanks!
I did take some of the code off of a guide on the web, and I don't understand it totally, so can someone explain to me what this code does as well? What is e.currentTarget? Thanks!
var newLaser1:MovieClip = e.currentTarget as MovieClip;Here's the full .as file if anybody wants to take a look. http://pastebin.com/5ff4BQa5
