Marzo 25, 2004

Un post que no sirve para mucho

Tras un día entero serializando y deseralizando clases, necesitaba hacer algo lo más "flashero" posible. Pues aquí está: una clase que puede utilizarse como temporizador.

Permite ejecutar algo determinado ( que se le pasa como parámetro ) cuando ha pasado el tiempo que se le pasa como parámetro también, y permite resetear ese temporizador de manera que actúe como timeout, permite pararlo, eliminarlo, ...

En primer lugar, y aunque ya sé que la arquitectura actual de flash está basada en eventos más que en callbacks, se va a definir una clase que recibe como parámetros una instancia de una clase, y un método a ejecutar. De esta forma, se podrán ejecutar callbaks a medida.

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

La clase Timer es:

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

Y finalmente, una clase para realizar el test:

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..." );
  /// Y por ejemplo:
  this.theTimer.resetTo( this.time );
 }
}

Y finalmente, en el frame:

import net.designnation.blog.*

var theTest: TimerTest = new TimerTest( this, 1000 );

Feliz viernes....

Escrito por Cesar Tardaguila en: Marzo 25, 2004 10:06 PM
Comentarios