PDA

View Full Version : Parent class wait for subclass Timer to finish, and then continue...



mhulse
June 12th, 2009, 06:54 PM
Hi!

I am slowly learning my way around AS3...

Quick question:

How do tell my parent class to wait for a subclass Timer finish event?

Here is my "Timing" subclass:


package com.site.peel {

import flash.utils.Timer;
import flash.events.TimerEvent;

public class Timing {

private var _period:uint;

public function Timing(t:uint = 10000):void {

_period = t;

init();

};

private function init():void {

var flashTimer:Timer = new Timer(_period);

flashTimer.addEventListener(TimerEvent.TIMER, onTime);

flashTimer.start()

};

private function onTime(evt:TimerEvent):void {

trace('Timer has completed...');

};

};

};My parent class calls the above like so:



...
...
...
private var _timing:Timing;
...
...
_timing = new Timing();
...
...
...
I am able to see the trace "Timer has completed...", but I am not sure how to tell my parent class that the subclass has finished the timer.

Additionally, once I am done using the Timing subclass, how do I destroy the event listener from the parent class? Should I just create another method in the Timing class that removes the event listener, and call it like so:

_timing.removeListener();

Does that makes sense?

I would love a little guidance. :)

Many TIAs!
Cheers,
Micky

mhulse
June 12th, 2009, 07:07 PM
Hrmm, maybe I use a for loop and a getter?

mhulse
June 12th, 2009, 07:39 PM
Hrmm, maybe I use a for loop and a getter?

Ah, this seems to be working:


package com.site.peel {

import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.events.Event;
import flash.events.EventDispatcher;

public class Timing extends EventDispatcher {

public static const TIMING_COMPLETE:String = 'timing_completed';

private var _period:uint;
private var _timer:Timer;

public function Timing(t:uint = 10000):void {

_period = t;

init();

};

private function init():void {

_timer = new Timer(_period);

_timer.addEventListener(TimerEvent.TIMER, onTime, false, 0, true);

_timer.start()

};

private function onTime(evt:TimerEvent):void {

_timer.removeEventListener(TimerEvent.TIMER, onTime);

this.dispatchEvent(new Event(Timing.TIMING_COMPLETE));

};

};

};

And from the parent class:


...
...
...
_timing = new Timing();
_timing.addEventListener(Timing.TIMING_COMPLETE, onChange);
...
...
...
private function onChange(e:Event):void { trace('changed'); }
...
...
...

I have not fully tested, but the this seems like the best way to go.

Any tips?

Thanks!
Micky

senocular
June 12th, 2009, 07:45 PM
if the Timing class adds the listener, it should also destroy it.

If you want the owner class to know when the timer has completed, dispatch an event from the Timing class and have the owner class listen for that ('parent' class is an inaccurate term as it implies inheritance)

nvm, I was late :)

mhulse
June 12th, 2009, 07:54 PM
Hi senocular! Many thanks for your help! I am a big fan of yours! Your website (http://www.senocular.com/) and tutorials have been very helpful to me. Thank you for sharing all of your expertise with the rest of us!


if the Timing class adds the listener, it should also destroy it. If you want the owner class to know when the timer has completed, dispatch an event from the Timing class and have the owner class listen for that ('parent' class is an inaccurate term as it implies inheritance)

Ah, excellent, many thanks for the clarifications!

Sorry about the mis-use of "parent"... I am still learning all the ins/outs of classes. :: blushes :: :blush:


nvm, I was late :)


Thanks for the reply though! Still very helpful. :)

Have an excellent day!
Cheers,
Micky