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

Timer - Need Help!

Explorer ,
Jun 30, 2015 Jun 30, 2015

Copy link to clipboard

Copied

Hello I have the following structure: | Input Box B |        | Input Box A |              | Button |

.

When the user clicks the button timerA starts.

The numbers of timerA are placed into the input box A.

     IF input box A reaches '10'

          TimerA reset Input Box A Changes to 0.

          Input Box B = Prior Number + 1.

.

Now my question is after resetting the timer how can I initiate this again, without having to click the button every time. Note that that entering the command 'Timer.start' within the IF condition will break the code. As this will cause Input Box A to exceed '10' and continue infinitely without changing Input Box B at every '10'. Thanks for the help I hope I have explained this clearly.

Message was edited by: Corey Frankee-Smith Space being wiped..

TOPICS
ActionScript

Views

654

Translate

Translate

Report

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 ,
Jul 01, 2015 Jul 01, 2015

Copy link to clipboard

Copied

You should include the relevant code along with notes to indicate what you wish to change about it.  It will help to explain your statement regarding breaking the code.

Votes

Translate

Translate

Report

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 ,
Jul 09, 2015 Jul 09, 2015

Copy link to clipboard

Copied

I looked at your code and made several changes but then I realized it looks like you're making an assumption that is not correct: When you set a Timer to run with an interval of 1ms, it doesn't actually run that fast. It will run as fast as it can, but will never actually reach that speed. For something like a clock, you can set a Timer or listen for ENTER_FRAME and then measure the time offset from some start time you recorded or the time since the last frame. Either way, you use getTimer() to get the actual milliseconds that have elapsed since the SWF began running. With that you can make accurate clocks.

You may find that it's a lot easier to use a Date object which is designed to parse the information you're looking for from something like elapsed time.

Here are a few of the changes I made, and the script that will compile and run until the far left text fields show 1 0.

I un-nested your countup function from within your AdvCountupHandler function.

I wrapped two strings in parseInt so the > comparison you were doing would work.

Moved 2 variables outside the functions so they may continue to be referenced after the functions finish executing:

var myTimerA:Timer = new Timer(1,0);

var nCountB:Number = 0;

I see several instances of the following and I think you mean to be doing something other than what's happening:

countupM_txt.text=String(0+1);

changed the comparison operator that kicks things off from != to ==

if (countupA_txt.text == "")

The current code, with changes I made. Keep in mind this still doesn't do what you want, but I wanted to include it here regardless:

import flash.events.MouseEvent;

import flash.utils.Timer;

stop();

//* Restrict Input to Numbers

countupA_txt.restrict = countupB_txt.restrict = countupC_txt.restrict = countupD_txt.restrict = countupF_txt.restrict = countupH_txt.restrict = countupJ_txt.restrict = countupK_txt.restrict = "0-9";

countupE_txt.restrict = countupG_txt.restrict = "0-5";

countupI_txt.restrict = "0-2";

countupM_txt.restrict = "0-9";

countupL_txt.restrict = "0-3";

//* Reset Input Values to Blank

countupA_txt.text = countupB_txt.text = countupC_txt.text = countupD_txt.text = countupE_txt.text = countupF_txt.text = countupG_txt.text = countupH_txt.text = countupI_txt.text = countupJ_txt.text = countupK_txt.text = countupL_txt.text = "";

countupM_txt.text = "";

/*(C)*/

countupA_btn.addEventListener(MouseEvent.CLICK, AdvCountupHandler);

var myTimerA:Timer = new Timer(1,0);

var nCountB:Number = 0;

function AdvCountupHandler(event:MouseEvent):void

{

  trace( "click confirm" );

  if (countupA_txt.text == "")

  // if (countupA_txt.text != "")

  {

  // nCountB = parseInt(countupA_txt.text);

  // var myTimerA:Timer = new Timer(1,0);

  myTimerA.addEventListener(TimerEvent.TIMER, countup);

  myTimerA.start();

  //*End of Internal Function

  }

  //*End of External If Statement

}

//*End of External Function

function countup(e:TimerEvent):void

{

  trace( "tick @ time: " + getTimer() );

  nCountB++;

  countupA_txt.text = nCountB.toString();

  if (countupI_txt.text == "2" && parseInt( countupH_txt.text ) > 3 )

  {

  trace("24 Hour Fix");

  if (countupJ_txt.text != "")

  {

  var countupBFA:Number = parseInt(countupJ_txt.text);

  var countupBFB:Number = countupBFA + 1;

  countupJ_txt.text = countupBFB.toString();

  countupI_txt.text = "0";

  var countupBFC:Number;

  countupBFC = 10 - parseInt(countupH_txt.text);

  countupH_txt.text = String(countupBFC);

  }

  else if (countupJ_txt.text=="")

  {

  countupJ_txt.text=String(0+1);

  countupI_txt.text = "0";

  var countupBFD = 10 - parseInt(countupH_txt.text);

  countupH_txt.text = String(countupBFD);

  }

  }

  if (countupL_txt.text == "3" && countupK_txt.text == "6" && parseInt( countupJ_txt.text ) > 4)

  {

  trace("Day Fix for if Calculating Numbers 365 to 369");

  if (countupM_txt.text != "")

  {

  trace("First Poss Complete");

  var countupBFG:Number = parseInt(countupM_txt.text);

  var countupBFH:Number = countupBFG + 1;

  countupM_txt.text = countupBFH.toString();

  countupL_txt.text = "0";

  var countupBFI:Number;

  var countupBFJ:Number;

  countupBFI = 10 - parseInt(countupK_txt.text);

  countupBFJ = 10 - parseInt(countupJ_txt.text);

  countupK_txt.text = String(countupBFI);

  countupJ_txt.text = String(countupBFJ);

  }

  else if (countupM_txt.text=="")

  {

  trace("Second Poss Complete");

  countupM_txt.text=String(0+1);

  countupL_txt.text = "0";

  var countupBFE = 10 - parseInt(countupK_txt.text);

  var countupBFF = 10 - parseInt(countupJ_txt.text);

  countupK_txt.text = String(countupBFE);

  countupJ_txt.text = String(countupBFF);

  }

  }

  else if (countupL_txt.text == "3" && (countupK_txt.text == "7" || countupK_txt.text == "8" || countupK_txt.text == "9"))

  {

  trace("Day fix for if Calculating Numbers Greater 370 to 399");

  if (countupM_txt.text != "")

  {

  trace("First Poss Complete");

  var countupBFK:Number = parseInt(countupM_txt.text);

  var countupBFL:Number = countupBFK + 1;

  countupM_txt.text = countupBFL.toString();

  countupL_txt.text = "0";

  var countupBFM:Number;

  var countupBFN:Number;

  countupBFM = 10 - parseInt(countupK_txt.text);

  countupBFN = 10 - parseInt(countupJ_txt.text);

  countupK_txt.text = String(countupBFM);

  countupJ_txt.text = String(countupBFN);

  }

  else if (countupM_txt.text=="")

  {

  trace("Second Poss Complete");

  countupM_txt.text=String(0+1);

  countupL_txt.text = "0";

  var countupBFO = 10 - parseInt(countupK_txt.text);

  var countupBFP = 10 - parseInt(countupJ_txt.text);

  countupK_txt.text = String(countupBFO);

  countupJ_txt.text = String(countupBFP);

  }

  }

  if (countupA_txt.text == "10")

  {

  myTimerA.reset();

  countupA_txt.text = "0";

  var countupB:Number = parseInt(countupB_txt.text);

  if (countupB_txt.text != "")

  {

  var countupBA:Number = countupB + 1;

  countupB_txt.text = countupBA.toString();

  }

  else if (countupB_txt.text=="")

  {

  countupB_txt.text=String(0+1);

  }

  }

  if (countupB_txt.text == "10")

  {

  countupB_txt.text = "0";

  var countupC:Number = parseInt(countupC_txt.text);

  if (countupC_txt.text != "")

  {

  var countupCA:Number = countupC + 1;

  countupC_txt.text = countupCA.toString();

  }

  else if (countupC_txt.text=="")

  {

  countupC_txt.text=String(0+1);

  }

  }

  if (countupC_txt.text == "10")

  {

  countupA_txt.text = String(Number(0));

  countupB_txt.text = String(Number(0));

  countupC_txt.text = String(Number(0));

  var countupD:Number = parseInt(countupD_txt.text);

  if (countupD_txt.text != "")

  {

  var countupDA:Number = countupD + 1;

  countupD_txt.text = countupDA.toString();

  }

  else if (countupD_txt.text=="")

  {

  countupD_txt.text=String(0+1);

  }

  }

  if (countupD_txt.text == "10")

  {

  countupD_txt.text = "0";

  var countupE:Number = parseInt(countupE_txt.text);

  if (countupE_txt.text != "")

  {

  var countupEA:Number = countupE + 1;

  countupE_txt.text = countupEA.toString();

  }

  else if (countupE_txt.text=="")

  {

  countupE_txt.text=String(0+1);

  }

  }

  if (countupE_txt.text == "6")

  {

  countupE_txt.text = "0";

  var countupF:Number = parseInt(countupF_txt.text);

  if (countupF_txt.text != "")

  {

  var countupFA:Number = countupF + 1;

  countupF_txt.text = countupFA.toString();

  }

  else if (countupF_txt.text=="")

  {

  countupF_txt.text=String(0+1);

  }

  }

  if (countupF_txt.text == "10")

  {

  countupF_txt.text = "0";

  var countupG:Number = parseInt(countupG_txt.text);

  if (countupG_txt.text != "")

  {

  var countupGA:Number = countupG + 1;

  countupG_txt.text = countupGA.toString();

  }

  else if (countupG_txt.text=="")

  {

  countupG_txt.text=String(0+1);

  }

  }

  if (countupG_txt.text == "6")

  {

  countupG_txt.text = "0";

  var countupH:Number = parseInt(countupH_txt.text);

  if (countupH_txt.text != "")

  {

  var countupHA:Number = countupH + 1;

  countupH_txt.text = countupHA.toString();

  }

  else if (countupH_txt.text=="")

  {

  countupH_txt.text=String(0+1);

  }

  }

  if (countupH_txt.text == "10")

  {

  countupH_txt.text = "0";

  var countupI:Number = parseInt(countupI_txt.text);

  if (countupI_txt.text != "")

  {

  var countupIA:Number = countupI + 1;

  countupI_txt.text = countupIA.toString();

  }

  else if (countupI_txt.text=="")

  {

  countupI_txt.text=String(0+1);

  }

  }

  if (countupI_txt.text == "2" && countupH_txt.text == "4")

  {

  countupH_txt.text = "0";

  countupI_txt.text = "0";

  var countupJ:Number = parseInt(countupJ_txt.text);

  if (countupJ_txt.text != "")

  {

  var countupJA:Number = countupJ + 1;

  countupJ_txt.text = countupJA.toString();

  }

  else if (countupJ_txt.text=="")

  {

  countupJ_txt.text=String(0+1);

  }

  }

  if (countupJ_txt.text == "10")

  {

  countupJ_txt.text = "0";

  var countupK:Number = parseInt(countupK_txt.text);

  if (countupK_txt.text != "")

  {

  var countupKA:Number = countupK + 1;

  countupK_txt.text = countupKA.toString();

  }

  else if (countupK_txt.text=="")

  {

  countupK_txt.text=String(0+1);

  }

  }

  if (countupK_txt.text == "10")

  {

  countupK_txt.text = "0";

  var countupL:Number = parseInt(countupL_txt.text);

  if (countupL_txt.text != "")

  {

  var countupLA:Number = countupL + 1;

  countupL_txt.text = countupLA.toString();

  }

  else if (countupL_txt.text=="")

  {

  countupL_txt.text=String(0+1);

  }

  }

  if (countupL_txt.text == "3" && countupK_txt.text == "6" && countupJ_txt.text == "5")

  {

  countupL_txt.text = "0";

  countupK_txt.text = "0";

  countupJ_txt.text = "0";

  var countupM:Number = parseInt(countupM_txt.text);

  if (countupM_txt.text != "")

  {

  var countupMA:Number = parseInt(countupM_txt.text) + 1;

  countupM_txt.text = countupMA.toString();

  }

  else if (countupM_txt.text=="")

  {

  countupM_txt.text=String(0+1);

  }

  }

  //*End of Internal If Statements

}

Votes

Translate

Translate

Report

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 ,
Jul 10, 2015 Jul 10, 2015

Copy link to clipboard

Copied

If your description was accurate, then the code I posted earlier works perfect. I just didn't include the button to start the timer, and didn't include the addition in fieldB. Here's the code with those added:

var timerA:Timer = new Timer(250);

timerA.addEventListener(TimerEvent.TIMER, update);

myButton.addEventListener(MouseEvent.CLICK, startTimer);

function startTimer(e:MouseEvent):void

{

  timerA.start();

}

function update(e:TimerEvent):void

{

     fieldA.text = String(timerA.currentCount);

     if(timerA.currentCount == 10){

          timerA.reset();

          timerA.start();

         fieldB.text = String(parseInt(fieldB.text) + 1);

     }

}

Votes

Translate

Translate

Report

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 ,
Jul 10, 2015 Jul 10, 2015

Copy link to clipboard

Copied

LATEST

There's 2 important modifications I made that you should consider keeping: un-nesting the function and moving the declaration of the timer outside the function.

By declaring the timer outside the function (moved to the main timeline) it can be referenced by anything with access to the main timeline. Any variables declared in a function are removed when the function finishes running. If there's anything that you want to reference after the function has finished running it has to be declared outside the function.

If you nest functions the nested function is re-declared each time the wrapper function runs. That's not ideal. 

If you add the myTimer.start() call to the revised code that I posted the timer will reset and that may be what you're looking for:

...

myTimerA.reset();

myTimerA.start(); // add this line

countupA_txt.text = "0";

...

If the code architecture you've created doesn't do what you'd hoped, you may like to look into getTimer() and/or the Date method, or search around for a completed sample and maybe you'll find a countup/countdown timer that does something similar to what you are trying to do and you can use that as a starting point. Good luck!

Votes

Translate

Translate

Report

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 ,
Jul 01, 2015 Jul 01, 2015

Copy link to clipboard

Copied

You're doing something wrong. Calling reset() on the timer resets it and also resets the currentCount - which is what you should be using for display... something like so:

var timerA:Timer = new Timer(1000);

timerA.addEventListener(TimerEvent.TIMER, update);

timerA.start();

function update(e:TimerEvent):void

{

     myField.text = String(timerA.currentCount);

     if(timerA.currentCount == 10){

          timerA.reset();

          timerA.start();

     }

}

time text field will display 1-10 over and over...

Votes

Translate

Translate

Report

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