Skip to main content
Participating Frequently
October 4, 2012
Answered

Universal timer across multiple frames

  • October 4, 2012
  • 2 replies
  • 2244 views

Hello again

I'm creating a flash quiz and each question is timed. When the timer runs out I have some actionscript that says 'when timer reaches 0, go to and stop frame x', but the problem is I have quite a few pages and some of my timers are conflicting. Is there a way I can make one timer control everything?

At the moment I have one timer that controls when the clock times out

var count:Number = 6;

var myTimer:Timer=new Timer(1000,count);

myTimer.addEventListener(TimerEvent.TIMER, countdown);

myTimer.start();

function countdown(event:TimerEvent):void {

myText_txt.text=String((count)-myTimer.currentCount);

if(myText_txt.text == "0"){

gotoAndStop(5);

}

}

I have 'correct' and 'incorrect' pages, so when a question a question is answered correctly, you will jump to the 'correct' frame on the timeline. I then have a timer that controls when you leave that page

Example

var myTimer2:Timer;

myTimer2 = new Timer(3000, 3)

myTimer2.start();

myTimer2.addEventListener(TimerEvent.TIMER, backtoStart2)

function backtoStart2(evt:TimerEvent):void{

    myTimer2.start();

    if(myTimer2.currentCount == 3) (gotoAndStop(3));

    trace (myTimer2.currentCount)

}

My problem is that I have 8 questions, and it seems silly to make a new timer for each page, especially when I will keep getting a duplicate function definition error. What is the best way to approach this if I want all my actionscript on one frame?

This topic has been closed for replies.
Correct answer Andrei1-bKoviI

You don't need to jump through all these hoops.

Code below on the first frame will progress your movie automatically. Just replace frame sequence with desired target frames in frameSequence array. Also make sure that the layer with this code is continuous. Remove all the code from other frames - I mean all of it.

Read code comments.


stop();

var count:Number = 6;

// sequence of frames through which movie should go

var frameSequence:Array = [5, 10, 20, 30];

/**

* frame to jump to after countdown is finished,

* first value is the first element of frameSequence array

*/

var frameToGo:int = frameSequence[0];

var myTimer:Timer = new Timer(1000, count);

myTimer.addEventListener(TimerEvent.TIMER, countdown);

myTimer.start();

function countdown(event:TimerEvent):void

{

    myText_txt.text = String(count - myTimer.currentCount);

   

    if (myTimer.currentCount == count)

    {

        // get index of frameToGo in the array

        var index:int = frameSequence.indexOf(frameToGo);

        /**

         * get next frame in sequence if there is any left

         * if no frames left to go - set value to 0

         */

        frameToGo = index < frameSequence.length - 1 ? frameSequence[index + 1] : 0;

        // move timer to the original state

        myTimer.reset();

        /**

         * if next frame is not 0 - proceed

         * otherwise - do nothing

         */

        if (frameToGo > 0)

        {

            myText_txt.text = String(count);

            // restart timer

            myTimer.start();

            gotoAndStop(frameToGo);

        }

        else

        {

            myText_txt.text = "done";

        }

       

    }

}

2 replies

Andrei1-bKoviICorrect answer
Inspiring
October 6, 2012

You don't need to jump through all these hoops.

Code below on the first frame will progress your movie automatically. Just replace frame sequence with desired target frames in frameSequence array. Also make sure that the layer with this code is continuous. Remove all the code from other frames - I mean all of it.

Read code comments.


stop();

var count:Number = 6;

// sequence of frames through which movie should go

var frameSequence:Array = [5, 10, 20, 30];

/**

* frame to jump to after countdown is finished,

* first value is the first element of frameSequence array

*/

var frameToGo:int = frameSequence[0];

var myTimer:Timer = new Timer(1000, count);

myTimer.addEventListener(TimerEvent.TIMER, countdown);

myTimer.start();

function countdown(event:TimerEvent):void

{

    myText_txt.text = String(count - myTimer.currentCount);

   

    if (myTimer.currentCount == count)

    {

        // get index of frameToGo in the array

        var index:int = frameSequence.indexOf(frameToGo);

        /**

         * get next frame in sequence if there is any left

         * if no frames left to go - set value to 0

         */

        frameToGo = index < frameSequence.length - 1 ? frameSequence[index + 1] : 0;

        // move timer to the original state

        myTimer.reset();

        /**

         * if next frame is not 0 - proceed

         * otherwise - do nothing

         */

        if (frameToGo > 0)

        {

            myText_txt.text = String(count);

            // restart timer

            myTimer.start();

            gotoAndStop(frameToGo);

        }

        else

        {

            myText_txt.text = "done";

        }

       

    }

}

Esayem23Author
Participating Frequently
October 6, 2012

A true gentleman, thank you very much!

Inspiring
October 6, 2012

You are welcome!

Ned Murphy
Legend
October 4, 2012

If you want to use one Timer for all purposes then you should look into the properties and methods of the Timer class in the help documentation to learn how you can modify it on the fly to suit your changing uses for it.

Esayem23Author
Participating Frequently
October 4, 2012

Shall try!