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

Make a timer

Explorer ,
Mar 14, 2013 Mar 14, 2013

I have a digital timer counting down in the background of a website I'm working on.  It's strictly an aesthetic thing and doesn't really serve any real purpose otherwise.  I just want it to look like a working timer.  Is there a way I can do this with actionscript so that I don't have to actually keyframe the numbers changing?  I'm actually more of an animation guy and would usually just animate it timing down but that's probably more work than it's worth.

thanks

TOPICS
ActionScript
809
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

correct answers 1 Correct answer

LEGEND , Mar 14, 2013 Mar 14, 2013

The example below displays countdown from 2 minutes to zero.

import flash.display.Sprite;

import flash.events.TimerEvent;

import flash.text.TextField;

import flash.text.TextFormat;

import flash.utils.Timer;

var countDownDisplay:TextField;

var timer:Timer;

var date:Date;

init();

function init():void

{

          countDownDisplay = new TextField();

          countDownDisplay.defaultTextFormat = new TextFormat("Arial", 120, 0x008040);

          countDownDisplay.multiline = countDownDisplay.wordWrap = false;

      

...
Translate
LEGEND ,
Mar 14, 2013 Mar 14, 2013

Search Google or even these forums using terms like "AS3 countdown tutorial"

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 ,
Mar 14, 2013 Mar 14, 2013

The example below displays countdown from 2 minutes to zero.

import flash.display.Sprite;

import flash.events.TimerEvent;

import flash.text.TextField;

import flash.text.TextFormat;

import flash.utils.Timer;

var countDownDisplay:TextField;

var timer:Timer;

var date:Date;

init();

function init():void

{

          countDownDisplay = new TextField();

          countDownDisplay.defaultTextFormat = new TextFormat("Arial", 120, 0x008040);

          countDownDisplay.multiline = countDownDisplay.wordWrap = false;

          countDownDisplay.border = true;

          countDownDisplay.autoSize = "left";

          countDownDisplay.x = countDownDisplay.y = 20;

          countDownDisplay.text = "00:00 ";

          addChild(countDownDisplay);

          date = new Date(1000 * 120);

          timer = new Timer(500);

          timer.addEventListener(TimerEvent.TIMER, onTimer);

          timer.start();

}

function onTimer(e:TimerEvent):void

{

          date.time -= timer.delay;

          countDownDisplay.text = date.toUTCString().match(/\d\d:\d\d\s/)[0];

          if (date.time <= 0)

          {

                    timer.stop();

          }

}

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
Explorer ,
Mar 20, 2013 Mar 20, 2013

Would you be able to tell me which part of this coded dictates the start time?  So like if I wanted to change it a higher number than 2 minutes?

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 ,
Mar 21, 2013 Mar 21, 2013
LATEST

The line

date = new Date(1000 * 120);

sets it to 2 minutes.

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