PDA

View Full Version : [AS3] Game carrys on running in background after gameover?


benjee
12-11-2010, 11:17 PM
Hi all im currently making a flash game for a small project at university and Im at a bit of a stump at present luckily its near the end :|.

Anyway my game is divided within 3 packages and some actionscript on frame 3 of my timeline which is where the game starts.

Frame 1 = Menu
Frame 2 = AssetDump
Frame 3 = Game
Frame 10 = GameOver/TimeOut Screen

var sfx_gun:Sound = new sfx();
var sp:MovieClip = new ship();
var tm:Timer = new Timer(1500);

tm.addEventListener(TimerEvent.TIMER, onstart);

var bulletholder:Sprite = new Sprite();

addChild(bulletholder);

addChild(sp);

addEventListener(Event.ENTER_FRAME,onenter);
stage.addEventListener(MouseEvent.CLICK,onclick);

tm.start();
function onstart(e:Event){


var em:Sprite = new enemy();
em.x= Math.random()*400;
addChild(em);

}

function onenter(e:Event):void {
sp.x-=(sp.x-mouseX)*.1;
sp.y-=(sp.y-570)*.2;
}

function onclick(e:Event) {
sfx_gun.play();
sp.y=585;
var bl:Sprite = new bullet();
bl.y=sp.y;
bl.x=sp.x;
bulletholder.addChild(bl);
}


var count:Number = 60;
var myTimer:Timer = new Timer(1000,count);
myTimer.addEventListener(TimerEvent.TIMER, countdown);
myTimer.start();
function countdown(event:TimerEvent):void {
timertxt.text = String((count)-myTimer.currentCount);

if(timertxt.text == "0"){
gotoAndStop(10);
}
}


This is my code which has a countdown for 60 seconds after they click play where they kill as many ninjas as possible pretty simple and when its gets to 0 it goes to frame 10 and stops, But and a big ugly But the game carrys on playing in the background and does not stop.

I have 3 package/as files for the enemys, bullets and death animation everything works fine.

Can any one tell me how I would go about having the 60 second timer countdown which it does but after 60 seconds going to frame 10 and actually make the game stop and not carry on running in the background?

Cheers for the help guys!

svenjoypro
12-12-2010, 12:30 AM
You have to remove all of your event listeners (well maybe not all of them, but any that you use for the game)

myTimer.removeEventListener(TimerEvent.TIMER, countdown);
tm.removeEventListener(TimerEvent.TIMER, onstart);
stage.removeEventListener(MouseEvent.CLICK,onclick );

and remove any and all ninjas and whatever else you have on the screen that you no longer want. It's convenient to add all of your ninja instances to an array (but remember to remove them from the array each time one gets killed) then when the game is over just remove each ninja still in the array.

while (ninjaArray.length >= 1) {
//REMOVE ANY EVENT LISTENERS ON THE NINJA
stage.removeChild(ninjaArray.pop());
}


Does that make sense?

benjee
12-12-2010, 12:41 AM
Wicked Svenjoypro cleared that up for me everything looks and works perfectly now thanks alot! :)