End the program itself when there is no movement Action Script 3 - actionscript-3

Turn off when the programa is not touched for 5-10 minutes.
I am using timer
Even when the program is touched, it closes when the time is up
How can i solve it?
var myTimer:Timer = new Timer(300000);
myTimer.addEventListener(TimerEvent.TIMER, timerListener, false, 0, true);
function timerListener (e:TimerEvent):void{
fscommand("quit");
}
myTimer.start();

myTimer.reset(); reset it and then start it again myTimer.start(); you just have to put that in some event handler that indicates "activity" - perhaps every n time to keep it from firing a lot
var myTimer:Timer = new Timer(300000);
myTimer.addEventListener(TimerEvent.TIMER, timerListener, false, 0, true);
function timerListener (e:TimerEvent):void{
fscommand("quit");
}
myTimer.start();
I won't dive into the custom event class but there are a good number of sources for that but basically use the .reset() and .start() in those.
For example
https://gamedev.stackexchange.com/a/12230
https://stackoverflow.com/a/23559690/125981

Here is a simple example to study...
var myTimer:Timer = new Timer(300000);
myTimer.addEventListener(TimerEvent.TIMER, timerListener, false, 0, true);
stage.addEventListener(MouseEvent.MOUSE_MOVE, reset_Timer); //check for any mouse movement
myTimer.start();
function timerListener (e:TimerEvent) :void
{
//# function happens when Timer amount is reached (eg: mouse did not move to reset it)
//choose one below..
//fscommand("quit"); //# close app
//myTimer.stop(); myTimer.start(); //# stop and then restart Timer
//stage.removeEventListener(MouseEvent.MOUSE_MOVE, reset_Timer); //# cancel any further usage of this function
}
function reset_Timer (e:MouseEvent) :void
{
//# function happens after mouse not moved for total millisecond count of Timer amount
myTimer.reset(); //reset countdown because mouse was moved
}

Related

How to remove current Event Listener before adding new one

How do I remove current EventListener before adding new one to it? I have below code does a simple countdown:
var myTimer:Timer = new Timer(1000);
function countDown(dateToProcess):void
{
myTimer.addEventListener(TimerEvent.TIMER, countdownTimer);
myTimer.start();
function countdownTimer(e:Event):void
{
//display dateToProcess.getTime() - today.getTime();
trace(myTimer.currentCounter);
}
}
function countDown is called every X seconds and new datetoProcess value will be passed. However, every time when this function is called, a new TimerEvent.TIMER EventListener is added because the code told it to, is there anyway to remove the current Event Listener before adding new one?
I've tried:
if (myTimer.hasEventListener(TimerEvent.TIMER))
{
myTimer.stop();
myTimer.removeEventListener(TimerEvent.TIMER, countdown);
trace("remove");
}
but doesn't seem to be working, it is still adding event Listener, I used trace(myTimer.currentcounter) and output like:
1
2
2
3
3
3
4
4
4
4
Anyone can help with this one please?
Still not sure what you're after for, but here's one shot using the same Timer:
var myTimer:Timer = new Timer(1000,0);
function countDown(dateToProcess):void{
myTimer.addEventListener(TimerEvent.TIMER, countdownTimer);
myTimer.start();
}
function countdownTimer(e:TimerEvent):void{
myTimer.stop();
myTimer.removeEventListener(TimerEvent.TIMER, countdownTimer);
trace(myTimer.currentCount);
}
Or:
function countDown(dateToProcess):void{
if(myTimer.hasEventListener(TimerEvent.TIMER)){
myTimer.stop();
myTimer.removeEventListener(TimerEvent.TIMER, countdownTimer);
}
myTimer.addEventListener(TimerEvent.TIMER, countdownTimer);
myTimer.start();
}
function countdownTimer(e:TimerEvent):void{
trace(myTimer.currentCount);
}
So it works as you thought it would, you probably just had something in wrong place?

MOUSE_MOVE Event not working with Timer

I have a Timer, and when the time expires it goes to another scene. When you move the mouse, Timer gets reset. However, when time expires (and it goes to the other scene) and I go back to the first scene (via clicking the on the stage of the other scene), my timer mouse event no longer works. The time does not get reset, and instead time expires and goes to the other scene again. I put a trace in my MOUSE_MOVE event method so I see what is going on in there, and the time is just not being reset. Can anyone help?
Here is my code:
//Define, Set and Start Timer
var myTimer:Timer;
myTimer = new Timer(10000, 1);
myTimer.addEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete);
myTimer.start();
//Mouse Move Events, Reset Timer when you move the mouse
stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoved);
stage.addEventListener(TouchEvent.TOUCH_BEGIN, mouseMoved);
//Timer Complete Method
function onTimerComplete(event : TimerEvent) : void
{
gotoAndStop(1, "Screensaver");
}
//Time Mouse Move Method, Reset Timer when you move the mouse
function mouseMoved(event:MouseEvent):void
{
myTimer.stop();
myTimer.reset();
myTimer.start();
}
And here is the click event on my screensaver scene:
stage.addEventListener(MouseEvent.CLICK, stopScreensaver);
function stopScreensaver(e:MouseEvent):void
{
screensaver1.visible = false;
screensaver2.visible = false;
screensaver3.visible = false;
screensaver4.visible = false;
screensaver5.visible = false;
screensaver6.visible = false;
screensaver7.visible = false;
screensaver8.visible = false;
touchStart.visible = false;
timer.stop();
timer.reset();
stage.removeEventListener(MouseEvent.CLICK, stopScreensaver);
timer.removeEventListener(TimerEvent.TIMER,timerListener);
stage.removeChild(whiteBackground);
gotoAndStop(1, "Home");
}

Problems with adding and subtracting time from/to Timers

Now I am also using a timer in frame 8, which is my Gamescreen frame to try and create an energy bar, so decreasing by 1 every second, and everytime the character collides with an object then increment the value of count by 1 (which in my min is 1sec, right?), however the timer runs out prematurely, when the label is showing 3secs left after collecting 3 items the timer automatically ends, HELP ME! :)
var count:Number = 5; (temporary value for testing)
var theTimer:Timer = new Timer(1000, count);
theTimer.addEventListener(TimerEvent.TIMER_COMPLETE, onTimerComplete);
theTimer.start();
function whenTimerComplete(e:TimerEvent):void
{
theTimer.removeEventListener(TimerEvent.TIMER_COMPLETE, whenTimerComplete); //Remove listener
gotoAndStop("frameFive"); // Advance to score screen.
}
theTimer.addEventListener(TimerEvent.TIMER, theCountdown);
function theCountdown(e:TimerEvent):void
{
count--;
timerLabel.text = count.toString()
}
//Start the timer and show in the label.
timerLabel.text=count.toString();
theTimer.start();
All help and a solution is VERY much appreciated.
Here's an example countdown timer:
Launch Flash example
FLA source code
Countdown Timer AS3 source code
CS6 ZIP of source code
CS5 ZIP of source code
Create a countdown timer class at the root of your FLA:
CountdownTimer.as
package {
import flash.events.TimerEvent;
import flash.utils.Timer;
public class CountdownTimer extends Timer {
public var time:Number = 0;
public function CountdownTimer(time:Number = Number.NEGATIVE_INFINITY, delay:Number = 1000) {
super(delay, repeatCount);
if (!isNaN(time))
this.time = time;
repeatCount = Math.ceil(time / delay);
addEventListener(TimerEvent.TIMER, timerHandler);
}
protected function timerHandler(event:TimerEvent):void {
time -= delay;
if (time == 0)
dispatchEvent(new TimerEvent(TimerEvent.TIMER_COMPLETE));
}
public function dispose():void {
removeEventListener(TimerEvent.TIMER, timerHandler);
}
}
}
On the timeline of your FLA, create a timer with the total number of milliseconds to countdown:
var timer:CountdownTimer = new CountdownTimer(60000);
timer.addEventListener(TimerEvent.TIMER, timerHandler);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, timerCompleteHandler);
timer.start();
In the example above, the timer will countdown for 1-minute (60-seconds). Each second the timerHandler will be called. When it reaches 0, the timerCompleteHandler will be called.
function timerHandler(event:TimerEvent):void {
timerText.text = (timer.time / 1000).toString();
}
function timerCompleteHandler(event:TimerEvent):void {
timerText.text = "COMPLETE";
}
To add time to the timer, add milliseconds to time. If you want the timer to dispatch timer complete event when it reaches 0, update the repeatCount:
timer.time += 1000;
timer.repeatCount += 1;
Likewise to remove time from the timer, subtract milliseconds from time; and again, if you want the timer to dispatch timer complete event when it reaches 0, update the repeatCount:
timer.time -= 1000;
timer.repeatCount -= 1;

AS 3 Flash Countdown Timer For Game Over Screen

I am currently in a Flash game programming class with action script 3 and can't seem to find a clock anywhere that counts down and then does an action. I've been using Lynda tutorials and that hasn't been helpful and I have also Google searched quite a bit, but no luck. I have a Countdown clock and have been try "if" statements, but no luck.
Can someone guide me in the right direction on what I am doing wrong?
//game timer
var count:Number = 60; // amount of time
var myTimer:Timer = new Timer(1000,count); // time in ms, count is calling from line above
myTimer.addEventListener(TimerEvent.TIMER, countdown);
myTimer.start();
function countdown(event:TimerEvent):void
{
myText_txt.text = String((count)-myTimer.currentCount); //dynamic txt box shows current count
}
//if and else if statements
if (((count)-myTimer.currentCount) == 58)
{
gotoAndStop(2);
}
This should help ;) Without any magic numbers.
var myTimer:Timer = new Timer(1000,60); // every second for 60 seconds
myTimer.addEventListener(TimerEvent.TIMER, onTimer);
myTimer.addEventListener(TimerEvent.TIMER_COMPLETE, onComplete);
myTimer.start();
function onTimer(e: TimerEvent):void {
myText_txt.text = String(myTimer.repeatCount - myTimer.currentCount);
}
function onComplete(e: TimerEvent):void{
gotoAndStop(2);
}
Add your if statement inside countdown like so:
function countdown(event:TimerEvent):void
{
myText_txt.text = String((count)-myTimer.currentCount); //dynamic txt box shows current count
//if and else if statements
if (((count)-myTimer.currentCount) == 58)
{
gotoAndStop(2);
}
}

Action Script 3 Timer Not Stopping

Okay, so I am a very amateur AS3 programmer. I have a timer setup, and after 45 seconds it should move to scene 6, however if it calls hitTestObject, it should stop and reload from 0 when the scene reloads. EDIT: I know this code is probably really bad coding, I'm also taking tips on how to fix this code up. Here's my code:
var myTimer:Timer = new Timer(1000, 1); // 1 second
myTimer.addEventListener(TimerEvent.TIMER, onEnterFrame);
myTimer.start();
{MAIN FUNCTION}
function onEnterFrame(e:Event):void {
var startTime:int=getTimer();
var currentTime:int=getTimer();
trace(currentTime);
if (currentTime>45000){
gotoAndStop(1, "Scene 6");
}
}
My issue, is that the timer keeps running when it hits test object and so when scene 3 is reloaded, the timer just keeps going. Therefore you only have to play for a total of 45 seconds, no matter how many times you die. It should be that once you die the timer reloads when you reload scene 3. Any ideas on what I can do?
Hope this helps:
var myTimer:Timer;
function resetTimer():void
{
// check if timer already initialize
if(myTimer != null)
{
// just reset
myTimer.stop();
myTimer.reset();
}
else
{
// initialize timer and set a 45 sec delay;
myTimer = new Timer(45*1000,1);
myTimer.addEventListener(TimerEvent.TIMER, handleTimerTick);
}
myTimer.start();
}
function handleTimerTick(event:TimerEvent):void
{
// stop and null timer;
myTimer.stop();
myTimer.removeEventListener(TimerEvent.TIMER, handleTimerTick);
myTimer = null;
// goto my 6th scene
gotoAndStop(1, "Scene 6");
}
// whenever a hit test is performed and a timer reset is needed just call
resetTimer();