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

Timer to Reset to Main State

New Here ,
Jul 25, 2011 Jul 25, 2011

Copy link to clipboard

Copied

I have built an application in Flash Catalyst that I am publishing as an Air app for a desktop. I am trying to get the application to go back to the main menu (State1) when 5 minutes in up from any State.  I also would need the timer to reset if I moved States.  The application is an information kiosk and so once a person has stopped using it and left it in say State3, I want it to move back to State1 (menu) for the next person to use. I know that I can’t do this in Catalyst so I am attempting to do it in Builder. However, I have no experience in writing code.  I have been searching though the Forums for help but I really don’t have enough understanding to take something similar and rewrite it for my use.  If anyone could give me some help that would be great. 

Thanks

Views

1.3K

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

correct answers 1 Correct answer

Participant , Jul 26, 2011 Jul 26, 2011

Well for one thing you shouldn't be using const like this,

const state:String = currentState;

const means that the variable won't change but you are setting it to a variable that could be changing. Instead it should just be set as a normal variable like so,

var state:String = currentState;

But since you are just setting the variable state to equal the value of currentState we can instead just remove this line and use the variable currentState when we check to see if we are on the 'Menu' state. So he

...

Votes

Translate

Translate
Participant ,
Jul 25, 2011 Jul 25, 2011

Copy link to clipboard

Copied

Here is some code that should help you out with the timer you need. Make sure you call the timerInit method when your application starts. When you go to a new state/view then just make a call to resetTimer method. Update the "Add Navigation Code Here" with your code for going back to your original state/view.

<fx:Script>      <![CDATA[                     private var timeout:int = 5; //Timeout value in minutes                     private var timePassed:int;           private var startTime:int;           private var loop:Timer;                     protected function timerInit():void                              {                timePassed = 0;                startTime = getTimer();                loop = new Timer(500);                loop.addEventListener(TimerEvent.TIMER, timerCheck);                loop.start();           }                     protected function resetTimer():void           {                startTime = getTimer();           }                     protected function timerCheck(event:TimerEvent):void           {                timePassed = ((getTimer() - startTime) / 1000) / 60;                               if (timePassed >= timeout)                {                     trace("Timeout has been reached");

                    // Add navigation code here

               }           }                ]]> </fx:Script>

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
New Here ,
Jul 25, 2011 Jul 25, 2011

Copy link to clipboard

Copied

Ok So I have entered the code in the correct location, but not sure how to do the rest? What is "timerInit method" and how do I call it when my application starts? What is "resetTimer method", how do I call it and where do I put it? Where do I find the "Navigation Code" to put in the area you provided? I am sorry but I am really lost, I have never coded and do not really understand xml.

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
Participant ,
Jul 25, 2011 Jul 25, 2011

Copy link to clipboard

Copied

What is "timerInit method" and how do I call it when my application starts?

I am referring to the function called timerInit. To call this function you would go to the Application tag of your application and add the property creationComplete. The value of this property will be "timerInit()". So when your application is created and ready to go it will trigger the creationComplete which we have told to run the timerInit function. Below is an example of the application tag with the creationComplete property added to it.

<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"                xmlns:s="library://ns.adobe.com/flex/spark"                xmlns:mx="library://ns.adobe.com/flex/mx"                minWidth="955" minHeight="600"                creationComplete="timerInit()">

What is "resetTimer method", how do I call it and where do I put it?

Again just referring to the function called resetTimer. When you click a button to change states/views you are executing code to do this. You would just need to add a call to the resetTimer function when you are executing this code. You will probably want to add this code before the code that chages the state/view. It may look something like this if you are changing states,

<s:Button id="resetTimerButton" click="resetTimer(); currentState='State2';" />

Where do I find the "Navigation Code" to put in the area you provided?

The navigation code would be whatever you are doing to go back to the main screen of your application. Are you using a viewstack or just changing states of the application to navigate around?

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
New Here ,
Jul 25, 2011 Jul 25, 2011

Copy link to clipboard

Copied

Ok, got the "timerInit" funtion in the right spot.  The "resetTimer" funtion need to go into my buttons that move from state to state. However, the "click" in my button refer to a "ClickHandler" so do I put the code resetTimer(); in the "ClickHandler"?

Example of Button:

<s:Button id="button1" d:userLabel="TourButton" includeIn="Menu" x="134" y="366"

widh="364" height="292" buttonMode="false" click="tour_clickHandler()"skinClass="components.Tour3">

Example of ClickHandler with Code inserted:

protected function tour_clickHandler():void
   {
    const state:String = currentState;
    if ( state == 'Menu' ) {
     currentState='Tour';        

     resetTimer();   }

And yes I am just moving through my states by useing buttons.  I have one Next button and two back buttons.

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
Participant ,
Jul 25, 2011 Jul 25, 2011

Copy link to clipboard

Copied

That should work perfectly for reseting the timer. Just remeber you will need to do that everywhere you are going to be changing states.

Now for the timerCheck function, just rename "NameOfMainState" to the name of your main state that you want to go to when the timeout occurs.

protected function timerCheck(event:TimerEvent):void {      timePassed = ((getTimer() - startTime) / 1000) / 60;      if (timePassed >= timeout)      {

            resetTimer(); //Reset the timer back to 0

         if (state != "NameOfMainState") //If not showing the main state

            {

                 state = "NameOfMainState" //Go back to the main state of your application

            }

     } }

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
New Here ,
Jul 26, 2011 Jul 26, 2011

Copy link to clipboard

Copied

It Worked!!!!! With a miner adjustment to the timeCheck function. Would not recognize the last two lines, so added "const state:String = currentState;" above and changed "state = "Menu" to "currentState='Menu';". I am not sure why it worked but it looks the same as the state change functions in the clickhandler so I tried it and it work? See code below. If this is improper coding please let me know? Thanks so much for staying with me on this, I could not have done it without help and I have learned a great deal from this. Thanks again!

protected function timerCheck(event:TimerEvent):void
   {
    timePassed = ((getTimer() - startTime) / 1000) / 60;
   
    if (timePassed >= timeout)
    {
      resetTimer(); //Reset the timer back to 0
     const state:String = currentState;
     if (state != 'Menu') //If not showing the main state
        {
      currentState='Menu'; //Go back to the main state of your application
     }
    }
   }

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
Participant ,
Jul 26, 2011 Jul 26, 2011

Copy link to clipboard

Copied

Well for one thing you shouldn't be using const like this,

const state:String = currentState;

const means that the variable won't change but you are setting it to a variable that could be changing. Instead it should just be set as a normal variable like so,

var state:String = currentState;

But since you are just setting the variable state to equal the value of currentState we can instead just remove this line and use the variable currentState when we check to see if we are on the 'Menu' state. So here is the changed code,

protected function timerCheck(event:TimerEvent):void {      timePassed = ((getTimer() - startTime) / 1000) / 60;      if (timePassed >= timeout)      {           resetTimer(); //Reset the timer back to 0

          if (currentState != 'Menu') //If not showing the main state           {                currentState='Menu'; //Go back to the main state of your application           }      } }

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
New Here ,
Jul 26, 2011 Jul 26, 2011

Copy link to clipboard

Copied

LATEST

I have made the changes, Thanks again!

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