Timeout for flash program
Copy link to clipboard
Copied
Hey everyone,
I am looking for some code that will make my program restart if it remains idle for a certain amount of time. I have looked around a good bit and cant find one that works in ActionScript 3.0. Any help would be greatly appreciated.
Thanks in advance!
Copy link to clipboard
Copied
See if this leads to something you can use:
Copy link to clipboard
Copied
Hmmmm. I think that maybe there is a thread to follow there but I am pretty new to AS3.0 so I dont know that I completely understand what needs to be done.
it says "and you would use the timer class and reset() and start() it when there's activity."
so i would need to code a timer, and set the timer to function only when there is inactivity. then have the timer reset the program when it reaches a certain time. does that sound about right? any ideas on how one would accomplish this with code in AS3.0?
Copy link to clipboard
Copied
As far as the code goes, look into the Timer class in the Flash help documentation. Study the start and reset methods that support it.
As far as the logic goes, you don't need to understand the language in order to figure out what needs to be done--you have to figure that out and make code that realizes that logic. The Timer would be pretty much running/resetting constantly. When the application starts a Timer would be activated. As soon as any activity occurs the Timer would be reset and started again... meaning for every interaction with the interface, the Timer is reset and restarted. If the time ever comes when the Timer times out, the listener for the Timer calls whatever function to do whatever you need to do to start your module over again.
Try creating a simple example for yourself outside of the file you plan to implement this in. That will make it easier for you to work with and come to understand.
Copy link to clipboard
Copied
Thanks for the suggestions. So I have been poking around and trying to figure this out. I came across a tutorial that I think is a good lead, but I think it may be AS2 as opposed to AS3. Correct me if I am wrong. I keep getting the error "Warning: 1090: Migration issue: The onEnterFrame is not triggered automatically by Flash Player at run time in ActionScript 3.0. You must first register this handler for the event using addEventListener ( 'enterFrame', callback_handler).
Copy link to clipboard
Copied
I found some code that seems to be working:
// timeout
var nPrevMouseX:Number;
var nPrevMouseY:Number;
var myTimer:Timer = new Timer(5000);
myTimer.addEventListener(TimerEvent.TIMER, fOnTimer, false, 0, true);
myTimer.start();
function fOnTimer(evtTimer:TimerEvent):void {
// If the mouse has not moved
if(mouseX == nPrevMouseX && mouseY == nPrevMouseY){
gotoAndStop(1);
stage.addEventListener(MouseEvent.MOUSE_MOVE, fOnMouseMove, false, 0, true);
myTimer.reset();
}else{
// Capture the mouse's position
nPrevMouseX = mouseX;
nPrevMouseY = mouseY;
}
}
function fOnMouseMove(evtMouseMove:MouseEvent):void {
Mouse.show();
// Restart the timer
myTimer.start();
// Remove the listener
stage.removeEventListener(MouseEvent.MOUSE_MOVE, fOnMouseMove);
}
Copy link to clipboard
Copied
Here's a timeout class, that you can use in conjunction with docStrangluv's mouse movment.
The timeout class is pretty easy to configure, you can see an example here:
http://gieson.com/tidbits/Timeout/Timeout.html
And download the source from here:
http://gieson.com/tidbits/Timeout/
-gobot

