ActionScript 3.0 End Game Countdown Timer - actionscript-3

I'm making a game in actionscript 3.0. I currently have a countdown timer (counts down from 30 seconds). Once the 30 seconds is up I want the frame to move from frame 2 to frame 3. I have put gotoAndStop(3) in the code under the timer but when the timer starts it goes to frame 3 straight away. It doesnt go to frame 3 when the 30 seconds is up. I would appreciate any help at all!
var nCount:Number = 30;
var myTimer:Timer = new Timer(1000, nCount);
timer_text.text = nCount.toString(nCount);
myTimer.start();
myTimer.addEventListener(TimerEvent.TIMER, countdown);
function countdown(e:TimerEvent):void
{
nCount--;
timer_text.text = nCount.toString();
gotoAndStop(3);
}

You are calling gotoAndStop(3); on the very first timer event, i.e. after one second since you are not checking the value of nCount. You need to call gotoAndStop(3); only when nCount is zero.
function countdown(e:TimerEvent):void {
nCount--;
timer_text.text = nCount.toString();
if (nCount == 0) {
gotoAndStop(3);
}
}

Related

End the program itself when there is no movement Action Script 3

Turn off when the programa is not touched for 5-10 minutes.
I am using timer
Even when the program is touched, it closes when the time is up
How can i solve it?
var myTimer:Timer = new Timer(300000);
myTimer.addEventListener(TimerEvent.TIMER, timerListener, false, 0, true);
function timerListener (e:TimerEvent):void{
fscommand("quit");
}
myTimer.start();
myTimer.reset(); reset it and then start it again myTimer.start(); you just have to put that in some event handler that indicates "activity" - perhaps every n time to keep it from firing a lot
var myTimer:Timer = new Timer(300000);
myTimer.addEventListener(TimerEvent.TIMER, timerListener, false, 0, true);
function timerListener (e:TimerEvent):void{
fscommand("quit");
}
myTimer.start();
I won't dive into the custom event class but there are a good number of sources for that but basically use the .reset() and .start() in those.
For example
https://gamedev.stackexchange.com/a/12230
https://stackoverflow.com/a/23559690/125981
Here is a simple example to study...
var myTimer:Timer = new Timer(300000);
myTimer.addEventListener(TimerEvent.TIMER, timerListener, false, 0, true);
stage.addEventListener(MouseEvent.MOUSE_MOVE, reset_Timer); //check for any mouse movement
myTimer.start();
function timerListener (e:TimerEvent) :void
{
//# function happens when Timer amount is reached (eg: mouse did not move to reset it)
//choose one below..
//fscommand("quit"); //# close app
//myTimer.stop(); myTimer.start(); //# stop and then restart Timer
//stage.removeEventListener(MouseEvent.MOUSE_MOVE, reset_Timer); //# cancel any further usage of this function
}
function reset_Timer (e:MouseEvent) :void
{
//# function happens after mouse not moved for total millisecond count of Timer amount
myTimer.reset(); //reset countdown because mouse was moved
}

Problems with adding and subtracting time from/to Timers

Now I am also using a timer in frame 8, which is my Gamescreen frame to try and create an energy bar, so decreasing by 1 every second, and everytime the character collides with an object then increment the value of count by 1 (which in my min is 1sec, right?), however the timer runs out prematurely, when the label is showing 3secs left after collecting 3 items the timer automatically ends, HELP ME! :)
var count:Number = 5; (temporary value for testing)
var theTimer:Timer = new Timer(1000, count);
theTimer.addEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete);
theTimer.start();
function whenTimerComplete(e:TimerEvent):void
{
theTimer.removeEventListener(TimerEvent.TIMER_COMPLETE, whenTimerComplete); //Remove listener
gotoAndStop("frameFive"); // Advance to score screen.
}
theTimer.addEventListener(TimerEvent.TIMER, theCountdown);
function theCountdown(e:TimerEvent):void
{
count--;
timerLabel.text = count.toString()
}
//Start the timer and show in the label.
timerLabel.text=count.toString();
theTimer.start();
All help and a solution is VERY much appreciated.
Here's an example countdown timer:
Launch Flash example
FLA source code
Countdown Timer AS3 source code
CS6 ZIP of source code
CS5 ZIP of source code
Create a countdown timer class at the root of your FLA:
CountdownTimer.as
package {
import flash.events.TimerEvent;
import flash.utils.Timer;
public class CountdownTimer extends Timer {
public var time:Number = 0;
public function CountdownTimer(time:Number = Number.NEGATIVE_INFINITY, delay:Number = 1000) {
super(delay, repeatCount);
if (!isNaN(time))
this.time = time;
repeatCount = Math.ceil(time / delay);
addEventListener(TimerEvent.TIMER, timerHandler);
}
protected function timerHandler(event:TimerEvent):void {
time -= delay;
if (time == 0)
dispatchEvent(new TimerEvent(TimerEvent.TIMER_COMPLETE));
}
public function dispose():void {
removeEventListener(TimerEvent.TIMER, timerHandler);
}
}
}
On the timeline of your FLA, create a timer with the total number of milliseconds to countdown:
var timer:CountdownTimer = new CountdownTimer(60000);
timer.addEventListener(TimerEvent.TIMER, timerHandler);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, timerCompleteHandler);
timer.start();
In the example above, the timer will countdown for 1-minute (60-seconds). Each second the timerHandler will be called. When it reaches 0, the timerCompleteHandler will be called.
function timerHandler(event:TimerEvent):void {
timerText.text = (timer.time / 1000).toString();
}
function timerCompleteHandler(event:TimerEvent):void {
timerText.text = "COMPLETE";
}
To add time to the timer, add milliseconds to time. If you want the timer to dispatch timer complete event when it reaches 0, update the repeatCount:
timer.time += 1000;
timer.repeatCount += 1;
Likewise to remove time from the timer, subtract milliseconds from time; and again, if you want the timer to dispatch timer complete event when it reaches 0, update the repeatCount:
timer.time -= 1000;
timer.repeatCount -= 1;

AS 3 Flash Countdown Timer For Game Over Screen

I am currently in a Flash game programming class with action script 3 and can't seem to find a clock anywhere that counts down and then does an action. I've been using Lynda tutorials and that hasn't been helpful and I have also Google searched quite a bit, but no luck. I have a Countdown clock and have been try "if" statements, but no luck.
Can someone guide me in the right direction on what I am doing wrong?
//game timer
var count:Number = 60; // amount of time
var myTimer:Timer = new Timer(1000,count); // time in ms, count is calling from line above
myTimer.addEventListener(TimerEvent.TIMER, countdown);
myTimer.start();
function countdown(event:TimerEvent):void
{
myText_txt.text = String((count)-myTimer.currentCount); //dynamic txt box shows current count
}
//if and else if statements
if (((count)-myTimer.currentCount) == 58)
{
gotoAndStop(2);
}
This should help ;) Without any magic numbers.
var myTimer:Timer = new Timer(1000,60); // every second for 60 seconds
myTimer.addEventListener(TimerEvent.TIMER, onTimer);
myTimer.addEventListener(TimerEvent.TIMER_COMPLETE, onComplete);
myTimer.start();
function onTimer(e: TimerEvent):void {
myText_txt.text = String(myTimer.repeatCount - myTimer.currentCount);
}
function onComplete(e: TimerEvent):void{
gotoAndStop(2);
}
Add your if statement inside countdown like so:
function countdown(event:TimerEvent):void
{
myText_txt.text = String((count)-myTimer.currentCount); //dynamic txt box shows current count
//if and else if statements
if (((count)-myTimer.currentCount) == 58)
{
gotoAndStop(2);
}
}

Multiple timers help! FLASH as3

I'm making a quiz game, there's 5 questions involved. I made a timer for the 1st question so when the user hasnt answered the question within 10secs they'll go to question 2 (another frame).
Question 1 timer works fine, how do i get question 2 to do the exactly the same as question 1? I tried adding the same code into question 2 however it gives me an error.
Thanks
My code:
stop();
var count:Number = 10;
var myTimer:Timer=new Timer(1000,2);
myTimer.addEventListener(TimerEvent.TIMER, countdown);
myTimer.start();
function countdown(event:TimerEvent):void {
count00.text=String((count)-myTimer.currentCount);
if(count00.text == "0"){
gotoAndStop(85);
}
}
myTimer.stop();
myTimer.removeEventListener(TimerEvent.TIMER, countdown);
First off, you shouldnt be doing this over multible frames. But thats a different discussion.
You should stop your timer, remove the listener and move to next a frame in an onComplete event.
stop();
var count:Number = 10;
var myTimer:Timer=new Timer(1000,count);// this should be the total count
myTimer.addEventListener(TimerEvent.TIMER, countdown);
myTimer.addEventListener(TimeEvent.TIMER_COMPLETE, timerDone);
myTimer.start();
function countdown(event:TimerEvent):void {
count00.text=String((count)-event.currentTarget.currentCount);//get currentCount from event
}
function timerDone(e:TimerEvent):void{
trace("Timer finishing!");
myTimer.stop();
myTimer.removeEventListener(TimerEvent.TIMER, countdown);
gotoAndStop(85);
}
Btw. you are setting the repeatCount of the timer to 2, new Timer(1000,2); this will give you 2 seconds countdown, not much time for a question ;) it should be count
Also there musst be a dynamic textField with the instance name count00 in the same frame (not layer) as the code!!
Please always include relevant error messages in the question!

Action Script 3 Timer Not Stopping

Okay, so I am a very amateur AS3 programmer. I have a timer setup, and after 45 seconds it should move to scene 6, however if it calls hitTestObject, it should stop and reload from 0 when the scene reloads. EDIT: I know this code is probably really bad coding, I'm also taking tips on how to fix this code up. Here's my code:
var myTimer:Timer = new Timer(1000, 1); // 1 second
myTimer.addEventListener(TimerEvent.TIMER, onEnterFrame);
myTimer.start();
{MAIN FUNCTION}
function onEnterFrame(e:Event):void {
var startTime:int=getTimer();
var currentTime:int=getTimer();
trace(currentTime);
if (currentTime>45000){
gotoAndStop(1, "Scene 6");
}
}
My issue, is that the timer keeps running when it hits test object and so when scene 3 is reloaded, the timer just keeps going. Therefore you only have to play for a total of 45 seconds, no matter how many times you die. It should be that once you die the timer reloads when you reload scene 3. Any ideas on what I can do?
Hope this helps:
var myTimer:Timer;
function resetTimer():void
{
// check if timer already initialize
if(myTimer != null)
{
// just reset
myTimer.stop();
myTimer.reset();
}
else
{
// initialize timer and set a 45 sec delay;
myTimer = new Timer(45*1000,1);
myTimer.addEventListener(TimerEvent.TIMER, handleTimerTick);
}
myTimer.start();
}
function handleTimerTick(event:TimerEvent):void
{
// stop and null timer;
myTimer.stop();
myTimer.removeEventListener(TimerEvent.TIMER, handleTimerTick);
myTimer = null;
// goto my 6th scene
gotoAndStop(1, "Scene 6");
}
// whenever a hit test is performed and a timer reset is needed just call
resetTimer();