Almost friday: a useless post
After a looooong day working on a class serializer, I needed to do something as light as possible. So here it is: a class that can be used as a timer.
You can execute any method, when a given time has passed, and after that reset the timer to use it as a timeout controller
It know that the actual architecture is based on events, but here you will also find a class to implement callbacks.
So, first of all, here's the callback implementation
class net.designnation.blog.Callback
{
private var callbackObjVal: Object;
private var callbackMethodVal: String;
public function Callback( objParam: Object, methodParam: String )
{
this.callbackObj = objParam;
this.callbackMethod = methodParam;
}
public function set callbackObj( objParam: Object )
{
this.callbackObjVal= objParam;
}
public function get callbackObj( ): Object
{
return this.callbackObjVal;
}
public function set callbackMethod( methodParam: String )
{
this.callbackMethodVal= methodParam;
}
public function get callbackMethod( ): String
{
return this.callbackMethodVal;
}
public function fire( parameter: Object ): Object
{
return this.callbackObjVal[ this.callbackMethodVal ]( parameter );
}
}
The timer class is here:
import net.designnation.blog.*
class net.designnation.blog.Timer
{
private static var FRAMERATE: Number = 12;
private var timelineVal : MovieClip;
private var clip : MovieClip;
private var timeoutVal : Number;
private var callBackVal : Callback;
private var startedFlag : Boolean;
private var actualCount : Number;
function Timer( timelineParam: MovieClip, timeoutParam: Number, callBack: Callback )
{
this.timeline = timelineParam;
this.timeout = timeoutParam;
this.callback = callBack;
}
public function set timeline( value: MovieClip )
{
this.timelineVal = value;
}
public function set timeout( value: Number )
{
this.timeoutVal = value* FRAMERATE/ 1000;
}
public function set callback( value: Callback )
{
this.callBackVal = value;
}
public function init( )
{
this.actualCount = 0;
this.clip = this.timelineVal.createEmptyMovieClip( "clip",
this.timelineVal.getNextHighestDepth( ) );
this.clip[ "owner" ] = this;
this.clip.onEnterFrame = function( )
{
if ( this[ "owner" ].startedFlag )
{
if ( this[ "owner" ].actualCount< this[ "owner" ].timeoutVal )
{
this[ "owner" ].actualCount++;
}
else
{
this[ "owner" ].onFinish( );
}
}
};
}
public function start( )
{
this.startedFlag = true;
}
public function stop( )
{
this.startedFlag = false;
}
public function reset( )
{
this.actualCount = 0;
this.startedFlag = true;
}
public function resetTo( newTimeOut: Number )
{
this.actualCount = 0;
this.timeout = newTimeOut;
this.startedFlag = true;
}
public function remove( )
{
this.clip.removeMovieClip( );
delete this.clip;
}
private function onFinish( )
{
this.startedFlag = false;
this.actualCount = 0;
this.callBackVal.fire( { reqTime: this.timeoutVal } );
}
}
And finally, a class to test everything
import net.designnation.blog.*
class net.designnation.blog.TimerTest
{
private var timeline: MovieClip;
private var time : Number;
private var theTimer: Timer;
function TimerTest( timelineParam: MovieClip, timeoutParam: Number )
{
this.timeline = timelineParam;
this.time = timeoutParam;
this.init( );
}
public function init( )
{
this.theTimer = new Timer( this.timeline, this.time,
new Callback( this, "timeOutCallback" ) );
this.theTimer.init( );
this.theTimer.start( );
}
public function timeOutCallback( )
{
trace( "Time goes by..." );
/// For instance:
this.theTimer.resetTo( this.time );
}
}
And finally, we have the following code in a frame:
import net.designnation.blog.*
var theTest: TimerTest = new TimerTest( this, 1000 );
Happy friday....