PDA

View Full Version : How to create a timer?



geestring
November 11th, 2009, 09:41 PM
basically I want something to happen every 5 seconds continuously.

I think i should be using a while loop with setInterval... but i think that's as2.

does AS3 use a Timer class?

Krilnon
November 11th, 2009, 09:57 PM
var t:Timer = new Timer(5000);

t.addEventListener(TimerEvent.TIMER, onTimer);
t.start();

function onTimer(e:TimerEvent):void {
trace(e.target.currentCount);
}

?

Shaedo
November 11th, 2009, 09:58 PM
EDIT DAMN! beaten again!!!!! my indecision on whether to write a quick tutorial on how to use google VS how to use timer has lost my poll place!


import flash.utils.*;

// Create a new Timer object with a delay of 5 seconds
var myTimer:Timer = new Timer(5000);
myTimer.addEventListener('timer', timedFunction);

// Start the timer
myTimer.start();

// Function will be called every 5 seconds
function timedFunction(eventArgs:TimerEvent)
{
trace('Timer fired ' + myTimer.currentCount + ' times.');
}

Krilnon
November 11th, 2009, 10:00 PM
It's reassuring that our code was basically identical though!

Shaedo
November 11th, 2009, 10:15 PM
It's reassuring that our code was basically identical though!

reassuring for me, it should be damn alarming for you! :P

TheCanadian
November 11th, 2009, 10:48 PM
:lol:

geestring
November 12th, 2009, 11:27 AM
thanks. Is there a way to send more arguments to the function other than just TimerEvent?

geestring
November 12th, 2009, 12:35 PM
like i want that function being executed to access an array.

Krilnon
November 12th, 2009, 03:34 PM
Okay, that question is one that you should be able to easily find answers to in a search… It has been discussed to death.

geestring
November 12th, 2009, 03:56 PM
ok yea use the Dictionary class.

geestring
November 13th, 2009, 08:40 AM
...
var myTimer:Timer = new Timer(5000);

myTimer.addEventListener(TimerEvent.TIMER, function(e:TimerEvent){mainFunction(e,mainArray)}) ;

myTimer.start();





}



function mainFunction(e:TimerEvent, theArray:Array):void{
...



so i have this and that is how i pass extra arguments.

Next question. I've searched and havent got an answer. can e:TimeEvent be optional? I've tried e:TimerEvent = null but that doesnt work.

I want to run mainFunction before the timer because the Timer waits 5 seconds before running the function. I want to start it immediately.

[MuG]
November 13th, 2009, 09:01 AM
function mainFunction(e:TimerEvent, theArray:Array):void



First off this won't work. Listeners have to have only one parameter and it should be the event type that you created the listener with:

(like Krilnon said)



var t:Timer = new Timer(5000);

t.addEventListener(TimerEvent.TIMER, onTimer);
t.start();

function onTimer(e:TimerEvent):void {
trace(e.target.currentCount);
}



This parameter CAN be optional like:



function onTimer(e:TimerEvent = null):void
The reason why you might want to do this is to call the onTimer function from elsewhere in the code.

cbeech
November 13th, 2009, 09:19 AM
[MuG] "First off this won't work. Listeners have to have only one parameter ..."
- well that's true, however in the above code, gee is using an anonymous function as the handler and passing the arguments, so in this case it will work, and does just fine.

@gee - yes the parameters can be optional, however the order of them in the method mainFunction will not allow you to place a optional parameter before a 'non-optional' one - you must make BOTH of them optional in order to do so successfully, as in:


var myTimer:Timer = new Timer(1000);
myTimer.addEventListener(TimerEvent.TIMER, function(e:TimerEvent){mainFunction(e,mainArray)}) ;
myTimer.start();


function mainFunction(e:TimerEvent=null, theArray:Array=null):void{
trace(e.target.currentCount);
}

geestring
November 13th, 2009, 09:19 AM
my code above does work. Someone posted it and it allows one to pass arguments to the function...however the e:TimerEvent = null gives me compile errors

cbeech
November 13th, 2009, 09:19 AM
EDIT: damnit - something weird happened and ended up with a double post sorry bout that :) (think we may have posted at the exact same time gee lol ;) )

geestring
November 13th, 2009, 09:21 AM
ah cbeech you posted as i was posting...
yes! having both optional fixes everything!

Thanks guys

geestring
November 13th, 2009, 12:45 PM
for(var f:int = first; f<last; f++){

var string:String = theArray[f][2];


var imageLoader:Loader = new Loader();
imageLoader.load(new URLRequest(string));




imageLoader.x = (f-first) * 125;
imageLoader.addEventListener(MouseEvent.CLICK, function(e:MouseEvent){ link(e, f)});
mcHolder.addChild(imageLoader);
}

I did the same methodof passing variables for a button, but the "f" variable isnt dynamic depending on the button pressed.

geestring
November 13th, 2009, 12:59 PM
I worked around it and jus made a new object to pass the var.