How to remove current Event Listener before adding new one - actionscript-3

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?

Related

End the program itself when there is no movement Action Script 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
}

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);
}
}

looping a function in action script

hi guys I've been trying this for hours, buts still i can't loop this function, at least i want to repeat it unto 5 times, but it only loop for once, i tried using for loop, while, and do while, but still it won't loop
var myTimer:Timer = new Timer(1000); // 1 second
myTimer.addEventListener(TimerEvent.TIMER, runMany);
myTimer.start();
function runMany(event:TimerEvent):void {
myTimer.stop();
myTimer.start();
trace("runMany() called # " + getTimer() + " ms");
if(getTimer() > 3000)
{
//random call of captopn and costumer in 3 seconds
myTimer.stop();
capsChoice = randomRange(1, 3);//random caption
costumChoice = randomRange(1, 3);//randomcostum
//condition in caption
more codes in here………...
}
}
Try removing myTimer.stop() in the eventlistener function, because that function call stops your entire Timer object once it's called.
Also, when you create a Timer, you can give it a repeat counter in the Constructor Paramters.
var myTimer:Timer = new Timer(1000, 5); // 1 second, repeat 5 times
myTimer.addEventListener(TimerEvent.TIMER, runMany);
myTimer.start();
function runMany(event:TimerEvent):void {
//...
this will call the function 5 times with a delay of 1 second between executions.
And another thing to keep in mind, you only need to start the timer once, if you call myTimer.start() in the listener function every time, it will try to loops of 5 executions again and again, leading to an infinite loop.
See also : AS3 Documentation Timer
Working with the Timer Class

Multiple timers help! FLASH as3

I'm making a quiz game, there's 5 questions involved. I made a timer for the 1st question so when the user hasnt answered the question within 10secs they'll go to question 2 (another frame).
Question 1 timer works fine, how do i get question 2 to do the exactly the same as question 1? I tried adding the same code into question 2 however it gives me an error.
Thanks
My code:
stop();
var count:Number = 10;
var myTimer:Timer=new Timer(1000,2);
myTimer.addEventListener(TimerEvent.TIMER, countdown);
myTimer.start();
function countdown(event:TimerEvent):void {
count00.text=String((count)-myTimer.currentCount);
if(count00.text == "0"){
gotoAndStop(85);
}
}
myTimer.stop();
myTimer.removeEventListener(TimerEvent.TIMER, countdown);
First off, you shouldnt be doing this over multible frames. But thats a different discussion.
You should stop your timer, remove the listener and move to next a frame in an onComplete event.
stop();
var count:Number = 10;
var myTimer:Timer=new Timer(1000,count);// this should be the total count
myTimer.addEventListener(TimerEvent.TIMER, countdown);
myTimer.addEventListener(TimeEvent.TIMER_COMPLETE, timerDone);
myTimer.start();
function countdown(event:TimerEvent):void {
count00.text=String((count)-event.currentTarget.currentCount);//get currentCount from event
}
function timerDone(e:TimerEvent):void{
trace("Timer finishing!");
myTimer.stop();
myTimer.removeEventListener(TimerEvent.TIMER, countdown);
gotoAndStop(85);
}
Btw. you are setting the repeatCount of the timer to 2, new Timer(1000,2); this will give you 2 seconds countdown, not much time for a question ;) it should be count
Also there musst be a dynamic textField with the instance name count00 in the same frame (not layer) as the code!!
Please always include relevant error messages in the question!

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();