Mark R
Mark R

Reputation: 1

Actionscript 3 Control Timeline after Duration of no input from user

can anyone help me with this. I know its something very basic, but I just cant work it out.

What I need is for the timeline gotoandstop at frame 1 after 15 seconds of inactivity.

Basically this is for a directory board so if no one is using it, it will return back to the home screen after a period of inactivity.

Any help would be greatly appreciated.

Thankyou

Upvotes: 0

Views: 220

Answers (2)

Amir Rasti
Amir Rasti

Reputation: 768

its possible to simulate it so:

class test extends MovieClip{
    public var myTimer:Number;
    public var input:TextField;

    function test(){
       myTimer=0;
       input=new TextField();
       this.addChild(input);
       this.addEventListener(Event.ENTER_FRAME,timer);
       input.addEventListener(Event.CHANGE, input_from_user);
    }

    function timer(ev){
      myTimer +=(1/25);//if the frame rate is 25 frame per sconde
      if(myTimer ==15){
         this.gotoAndStop(1);
         this.removeEventListener(Event.ENTER_FRAME,timer);   
      }
    }

    function input_from_user(ev){
        myTimer =0;
    }
}

Upvotes: 1

BadFeelingAboutThis
BadFeelingAboutThis

Reputation: 14406

What you can do, is use a Timer object. Then, whenever the user moves the mouse or clicks or presses a key, reset that timer back to 15 seconds.

On your frame 1, make a timer object:

//create the timer object var
var resetTimer:Timer;

//if it doesn't exist yet, create a new timer object and assign it to that var
if(!resetTimer){
    resetTimer = new Timer(15000,1); //tick 1 time with a delay of 15 
    //listen for the TIMER event (fires when the delay is up)
    resetTimer.addEventListener(TimerEvent.TIMER, reset);seconds
}else{
    resetTimer.reset(); //if it did previously exist, stop/reset it (for when you revisit frame 1)
}

//go back to the first frame if the timer fires
function reset(e:Event = null):void {
    resetTimer.reset(); //reset the timer
    gotoAndStop(1);  //go to frame 1
}


//LISTEN for various user input type events on stage (globally)
stage.addEventListener(MouseEvent.MOUSE_DOWN, userInput);
stage.addEventListener(MouseEvent.MOUSE_MOVE, userInput);
stage.addEventListener(KeyboardEvent.KEY_DOWN, userInput);
stage.addEventListener(KeyboardEvent.KEY_UP, userInput);

//if there was user input, reset the timer and start it again
function userInput(e:Event = null):void {
    resetTimer.reset();
    resetTimer.start();
}

The only thing left to do is, when you leave frame 1 and want the timeout to be applicable call resetTimer.start(). Presumably that would be on frame 2.

Upvotes: 1

Related Questions