Skip to main content
cornbread02
Inspiring
June 2, 2015
Question

getting a timer to continue after returning to frame where it was created/started

  • June 2, 2015
  • 1 reply
  • 371 views

In my boredom I am trying to create the contestant podiums from Sale of The Century and have them set up to look like the TV show (see here: https://youtu.be/rZSdhRZQjak?t=25s). Now this is done between 5 frames on the timeline (with a different graphic for each frame. Everything is fine and swell until I added a timer for the speed round. I created a dynamic TextField in Flash. The problem is that every time I reset the podium by using the gotoAndStop method, my timer stops as well. I tried moving the textfield (because I have to have a removeChild method as it was created in flash) and the gotoAndStop but that still causes issues. I tried by creating the textfield in AS, but I just got the empty rectangle. Below is the code from the frame that is the default podium position and where the timer is created.

The question is how do I get the timer to continue to run after I had gone through the player "buzzin" frames and back to the default?

stop();

removeChild(srclock_txt);

pScore1.text = (firstPlayerScore).toString();

pScore2.text = (secondPlayerScore).toString();

pScore3.text = (thirdPlayerScore).toString();

pName1.text = firstPlayer;

pName2.text = secondPlayer;

pName3.text = thirdPlayer;

var leftArrow: Boolean = false;

var rightArrow: Boolean = false;

var upArrow: Boolean = false;

stage.addEventListener(KeyboardEvent.KEY_DOWN, buzzin);

function buzzin(event: KeyboardEvent): void {

    trace(event.keyCode);

    if (event.keyCode == 37) {

        leftArrow = true;

        gotoAndPlay(4);

    } else if (event.keyCode == 39) {

        rightArrow = true;

        gotoAndPlay(14);

    } else if (event.keyCode == 38) {

        upArrow = true;

        gotoAndPlay(9);

    }

}

stage.addEventListener(KeyboardEvent.KEY_DOWN, give_me_60);

function give_me_60(event: KeyboardEvent): void {

var clock_back: Shape = new Shape();

    clock_back.graphics.beginFill(0x000000);

    clock_back.graphics.lineStyle(3, 0xff0000);

    clock_back.graphics.drawRect(262.5, 312.4, 52, 40.6);

    clock_back.graphics.endFill();

    if (event.keyCode == 84) {

        addChild(clock_back);

        addChild(srclock_txt);

}}

var countDownDec:Number =1;

var totalSecs = 10;

var countDownSecs = totalSecs;

srclock_txt.text = countDownSecs;

var myTimer:Timer = new Timer (countDownDec*1000);

myTimer.addEventListener(TimerEvent.TIMER, tick);

function tick (e:TimerEvent): void {

    if (countDownSecs == 0) {

        myTimer.stop ();

    } else {

    countDownSecs = countDownSecs - countDownDec;

    srclock_txt.text = countDownSecs;

}}

stage.addEventListener(KeyboardEvent.KEY_DOWN, start_clock);

function start_clock (event:KeyboardEvent): void {

    if (event.keyCode == 71) {

        srclock_txt.text = totalSecs;

        myTimer.start();

}}

stage.addEventListener(KeyboardEvent.KEY_DOWN, stop_clock);

function stop_clock (event:KeyboardEvent): void {

    if (event.keyCode == 83) {

        myTimer.stop();

    }

}

This topic has been closed for replies.

1 reply

kglad
Community Expert
Community Expert
June 2, 2015

the code you showed in attached to some frame, correct?

and is the problem you are seeing caused by the code you showed re-executing when that frame is re-entered?

cornbread02
Inspiring
June 2, 2015

So the game starts on frame 3. The code is from frame 3 of the Actions layer (1 & 2 is where names are entered and screen initializes the game).

For example, player 1's podium lights up on frames 4-8. When the clock is not there, it is fine. When it is, I get through the frames perfectly. But when I go back to frame 3, the clock & textfield stop (i.e., it freezes on 8).

Thinking my problems was dealing with the code, I moved the the textfield (and thus the removeChild) to frame 2. When I tested that I got the re-executing and thus the clock freezes and starts a new timer in the textfield on top of the previous one.

Does that clear it up?

SIDENOTE: After posting this, I am at the moment trying to see if making the podium frames a MovieClip would solve this, but I am having difficulties with not being able to clear them when I press my reset key. Thus I'm yet to see if this would actually solve my problem. I have a bunch of AS3 books....guess I should have gotten one for Flash *sigh*

kglad
Community Expert
Community Expert
June 2, 2015

when your return to the frame that contains that code (frame 3), the code re-executes and it doesn't appear that you want that.

to remedy, you can enclose that code so it only executes the first time the frame is entered and not thereafter:

var alreadyExecuted:Boolean;

if(!alreadyExecuted){

alreadyExecuted=true;

// the code you don't want re-executed...

.

.

.

}