Search code examples
actionscript-3flashtimeractionscriptadobe

as3 timer not stopping nor will reset


I'm building a clock in actionscript 3, I want the ability to start, stop and reset the timer. I have working the start. I had the others working but suddenly nothing? I'm not sure if it's because I added more code and put in extra keyframes in the time line, but surely it can't because the start timer works.

Below is my code:

import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.events.MouseEvent;

var time = new Date();
var seconds = time.seconds;
var minutes = time.minutes;
var hours = time.hours;

var myTimer: Timer = new Timer(1000);
var secondTimer: Timer = new Timer(12000); //12 seconds

myStartButton.addEventListener(MouseEvent.CLICK, startTimer);
myStopButton.addEventListener(MouseEvent.CLICK, stopTimer);
myResetButton.addEventListener(MouseEvent.CLICK, resetTimer);

myTimer.addEventListener(TimerEvent.TIMER, secondAndMinuteRotate);
secondTimer.addEventListener(TimerEvent.TIMER, hourRotate);


function startTimer(event: MouseEvent): void
{
    myTimer.start();
    secondTimer.start();
}

function stopTimer(event: MouseEvent): void
{
    myTimer.stop(); //here's my issue
    secondTimer.stop();
}

function resetTimer(event: MouseEvent): void
{
    myTimer.reset(); //and here's my other issue
    secondTimer.reset();
}


function secondAndMinuteRotate(event: TimerEvent): void
{
    mySecondHand.rotation += 6;
    myMinuteHand.rotation += 0.1;
}

function hourRotate(event: TimerEvent): void
{
    myHourHand.rotation += 0.1; 
}

if (hours < 12)
{
    amOrPm.text = "AM";
}
else if (hours > 12)
{
hours = hours - 12;
}


if(hours < 10)
{
    hours = "0" + hours;
}
if(minutes < 10)
{
    minutes = "0" + minutes;
}

if(seconds < 10)
{
    seconds = "0" + seconds;
}

digitalText.text = hours + ':' + minutes + ':'+ seconds;

Link to File on Dropbox


Solution

  • Add stop(); in code of first frame.