Skip to main content
Known Participant
November 16, 2009
Question

Timer Class doubts

  • November 16, 2009
  • 1 reply
  • 798 views

Please let me know the fundamentals of the Timer class. I cannot understand why a Timer which generates the TimerEvent.TIMER is made to listen i.e why does a timer is made to listen to its event... And why not a stage can be a listener.  So confused that Timer is created and then it generates an even means it is a Broadcaster and then also listens to those events i.e.listener.

I am a bit confused about the constructor for the timer. Does it instantiates a timer when every time a timer event occurs.....

Please help me out with the concept.

Thanks

Regards

This topic has been closed for replies.

1 reply

November 16, 2009

All classes that are event dispatchers work this way... ie you add listeners to a NetStream class to catch errors and status events, because it is those classes that generate the events. Listeners attached to the objects are used to call functions anywhere you like...

So - the Timer class is the one dispatching the TIMER events - so it needs listeners attached to it. You don't attach a TIMER event listener to your stage because the stage doesn't dispatch those events.

You make a new Timer like so:

var myTimer:Timer = new Timer(1000);

Now when you say myTimer.start(); a TIMER event will be dispatched to myTimer every second. If you want to capture those events you add a listener to myTimer:

myTimer.addEventListener(TimerEvent.TIMER, someFunction);

Now, someFunction will be called every second - a new Timer is not instantiated each time an event is dispatched, there is just the one Timer instance - myTimer.

If you want the timer to run one time, you use the second parameter of the Timer constructor:

var myTimer:Timer = new Timer(1000, 1);

myTimer.addEventListener(TimerEvent.TIMER, someFunction);

myTimer.start();

Now, the timer will run one time and that is all, and someFunction will be called just the one time. If you want it to run again you can simply reset and re-start the timer:

myTimer.reset();

myTimer.start();

Hope that helps.

Inspiring
November 17, 2009

yeah, like dmennenoh said.

Classes that dispatch events basically keep a list of who is listening and for what. So the addEventListener is telling an instance to whom the event needs to be dispatched. So it sort of reads like this:

tellWhom.addEventLister(whichEvent,whereToSendIt)

Known Participant
November 17, 2009

Thanks sir for answering my querry.

I still have some additional doubts:

There are two timer say T1 and T2 and both are working on different parameters means both are despatching events.

Can T1 listen to the event of T2.

Just confused as in the following constructor

stage.addEventListener( MouseEvent.CLICK, someFunction);

In the above the event class is the Mouse Event and type of event is CLICK... The listener here is the stage... Means the Broadcaster here is the MouseEvent class.... And the listener is the stage..... Two entirely different entities..

In the constructor

var timer1 = new Timer ( 2000,5)

var timer2 = new Timer ( 3000,8)

Here I have two timers and they are firing two events at different events.

timer1. addEventListener ( TimerEvent.TIMER, someFunction1)

timer2. addEventListener ( TimerEvent.TIMER, someFunction2)

So in the above constructor we have event listeners attached to the Timers... These timers are also generating events... They are also catching their own events......

I still cannot understand conceptually the difference in the constructor of the stage example and these timer examples..

Please help me sir

Thanks in anticipation