Hi all i’m new here and have been using these forums as a personal lifeline, as i have made substantial progresses through my creating of my simple game i have finally hit a snag of which i cannot find the answer for anywhere.
The problem is that I have a timer counting down which is currently set at 2.5 seconds for testing purposes, when the timer finishes out and reaches zero i want the entire stage to be cleared and the playhead to move to frame 5. So far i have this working but my problem is that my remaining children are on the stage and I do not know how to remove all of them since they all of the same name but it is stated that which one i want to remove is not defined. Frame 5 is the lose screen and Frame 4 is the Win screen, I also am having the problem of the timer continuing and creating an error if frame 4 is reached before the timer runs out. Here is my code and thanks in advance it’s greatly appreciated and I will return the favor when i have learned more Actionscript
stop();
//Global Variables
var weap:MovieClip;
var timer:Timer;
init();
// ========================================//
function init()
{
timer = new Timer(20);
timer.addEventListener(TimerEvent.TIMER, runGame);
timer.start();
this.stage.addEventListener(MouseEvent.CLICK, changeColor);
//Creates the targets to be hit by weapon.
makeTargets(Math.round (Math.random() * 10), 1);
makeTargets(Math.round (Math.random() * 10), 2);
//Adds the weapon attached to mouse to the stage.
weap = new Weap();
weap.x = weap.y = 100;
weap.name = "weap";
addChild(weap);
}
function runGame(evt:TimerEvent)
{
//Calling the functions.
followMouse(evt);
rotateWeapon(evt);
checkCollision();
}
function makeTargets(numTargets, frame)
{
for(var i:int = 0; i < numTargets; i++)
{
var target:MovieClip = new Target();
target.x = Math.random() * stage.stageWidth;
target.y = Math.random() * stage.stageHeight;
addChild(target);
target.name = "target";
target.gotoAndStop(frame);
}
}
//This makes the weapon follow the mouse
function followMouse(evt:TimerEvent):void
{
weap.x += velFriction(weap.x, mouseX, 8);
weap.y += velFriction(weap.y, mouseY, 8);
}
function velFriction(orig:Number, dest:Number, coeff:Number):Number
{
return (dest-orig)/coeff;
}
//This rotates the object to point at the mouse.
function rotateWeapon(evt:Event):void
{
weap.rotation = getAngle(mouseX, mouseY, weap.x, weap.y);
}
function getAngle(x1:Number, y1:Number, x2:Number, y2:Number):Number
{
var radians:Number = Math.atan2(y1-y2, x1-x2);
return rad2deg(radians);
}
function rad2deg(rad:Number):Number
{
return rad * (180/Math.PI);
}
//This changes the object when clicking.
function changeColor(evt:MouseEvent):void
{
weap.play();
}
//This checks for collisions between objects
function checkCollision()
{
for(var i:uint = 0; i < this.numChildren; i++)
{
if(this.getChildAt(i).name != weap.name)
{
var targ = this.getChildAt(i);
if(weap.hitTestObject(targ))
{
if (weap.currentFrame == targ.currentFrame)
{
//Removes target when collision is correctly matched.
// trace("same")
removeChild(targ);
trace(numChildren);
if(this.numChildren == 1)
{
trace("gotoAndStop(4)")
removeChild(weap);
timer.removeEventListener(TimerEvent.TIMER, runGame)
this.gotoAndPlay(4);
}
}
else
{
// trace("different")
//Removes target when collision is incorrectly matched.
removeChild(targ);
//Repeat loop to add two of the following each time it is called.
for(var j:uint = 0; j < 2; j++)
{
//Adds new targets if incorrect collision is detected.
var target:MovieClip = new Target();
target.x = Math.random() * 500;
target.y = Math.random() * 300;
addChild(target);
target.name = "target";
target.gotoAndStop(targ.currentFrame);
trace(numChildren);
}
}
}
}
}
}
//timer length in seconds
var timerLength:Number = 5;
//new timer, 1000ms = 1 sec
var t:Timer = new Timer(500, timerLength);
t.addEventListener(TimerEvent.TIMER, checkTime);
//start timer
t.start();
function checkTime (e:TimerEvent):void
{
trace(--timerLength);
if(timerLength == 0)
{
trace("gotoAndStop(5)");
removeChild(weap);
timer.removeEventListener(TimerEvent.TIMER,runGame);
this.gotoAndPlay(5);
}
}