Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How do i add a countdown timer in as3 that starts when the program begins?

New Here ,
Aug 22, 2014 Aug 22, 2014

I also want it to remove everything that is on the stage when it reaches 0 and produce a Game Over screen.

TOPICS
ActionScript
1.6K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Aug 22, 2014 Aug 22, 2014

One way would be to use a Timer and have it tick off based on whatever time increments you wish to display and when it completes the repeat count needed to reach the timeout you have it take care of removing whatever content you intend.  If you have no idea where to start with this either look up the Timer class in the help documents or find a Timer tutorial using Google.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
Aug 25, 2014 Aug 25, 2014

You can do as Ned said and use a Timer or you do a simple compare between your apps start time and the current time. For example:

var startTime:Number = new Date().valueOf();

and then any time you want to see the elpased time:

var elapsed:Number = new Date().value() - startTime;

This will give you the elapsed time in milliseconds - divide by 1000 to get seconds.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guru ,
Aug 25, 2014 Aug 25, 2014
LATEST

import flash.events.TimerEvent;

import flash.utils.Timer;

var countdown:Timer = new Timer (1000,TARGET_TIME);

//after 60 Seconds every DisplayObject will be removed from stage

const TARGET_TIME:int = 60;

countdown.addEventListener(TimerEvent.TIMER, tick);

countdown.addEventListener(TimerEvent.TIMER_COMPLETE, gameOver);

function tick(e:TimerEvent):void {

    //this will count upwards 1.2.3.4....

    trace(e.currentTarget.currentCount);

}

function gameOver(e:TimerEvent):void {

    // this function will remove all DisplayObjects from your Stage

    // starting from the top

    //It will not remove shapes or anything the user might have drawn on the stage

    //just things that are accessible to ActionScript (Sprites, MovieClips)

    for (var i:int = this.numChildren-1 ; i >= 0; i--) {

        removeChildAt(i);

    }

}

//countdown will start only if you call it explicitly

countdown.start();

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines