Copy link to clipboard
Copied
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
if bullets remain on screen, you either
1. have not executed that code just prior to changing scene or
2. add bullets to the displaylist without add them to tower1BulletArray or
3. continue to add bullets to the displaylist after that code executes
Copy link to clipboard
Copied
to remove all the bullets in your array use:
for(var i:int=tower1BulletArray.length-1;i>=0;i--){
tower1BulletArray.parent.removeChild(tower1BulletArray);
tower1BulletArray.splice(i,1);
}
Copy link to clipboard
Copied
Hi, thanks for the quick reply. I believe you meant i>=0
And also, by changing that code in my clearLevel function, i now get a
Error #1009: Cannot access a property or method of a null object reference.
Any idea?
Copy link to clipboard
Copied
Hi, I managed to solve the errors (kind of) by using this code.
for (var i:int = tower1BulletArray.length - 1; i >= 0; i--)
{
if (tower1BulletArray.parent)
{
tower1BulletArray
tower1BulletArray.splice(i, 1);
}
}
However, the problem still persists that the bullets stay in the screen after I change the scene. Any solution? Thanks!
Copy link to clipboard
Copied
if bullets remain on screen, you either
1. have not executed that code just prior to changing scene or
2. add bullets to the displaylist without add them to tower1BulletArray or
3. continue to add bullets to the displaylist after that code executes
Copy link to clipboard
Copied
Got it. Thank you so much!
Copy link to clipboard
Copied
you're welcome.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now