Clase de ActionScript 3 para retardar la ejecución de funciones

Posted by admin | AS3,Class,Trucos | Thursday 16 August 2007 3:11 pm

Esta clase de ActionScript 3 permite retrasar un cierto tiempo la ejecución de una acción de flash. Únicamente hay que crear un controlador de tiempo (TimerEvent.TIMER en AS3 o setInterval en AS2) que ejecute la acción deseada en el tiempo que le indiquemos.

Esta sería la clase Delay.as:

Actionscript:
  1. package com.zguillez.utils
  2. {
  3.    import flash.display.Sprite;
  4.    import flash.utils.Timer;
  5.    import flash.events.TimerEvent;
  6.    //------------------------------------------
  7.    public class Delay
  8.    {
  9.       private static var _ruta:Sprite;
  10.       private static var _tiempo:Number;
  11.       private static var _timer:Timer;
  12.       private static var _accion:String;
  13.       //------------------------------------------
  14.       public static function action(tiempo:Number,segundos:Boolean,ruta:Sprite,accion:String)
  15.       {
  16.          _ruta = ruta;
  17.          _accion = accion;
  18.          _tiempo = (segundos == true) ? tiempo * 1000 : tiempo / _ruta.stage.frameRate * 1000 ;
  19.          _timer = new Timer(_tiempo, 1);
  20.          _timer.addEventListener(TimerEvent.TIMER, ejecuta);
  21.          _timer.start();
  22.       }
  23.       //------------------------------------------
  24.       private static function ejecuta(event:TimerEvent)
  25.       {
  26.          _ruta[_accion]();
  27.       }
  28.       //------------------------------------------
  29.       public static function reset()
  30.       {
  31.          _timer.reset();
  32.       }
  33.       //------------------------------------------
  34.    }
  35. }

Las funciones y propiedades son estáticas para NO necesitar instanciar la clase (constructor new). Así que para usarla en nuestro .fla o dentro de otra clase pondríamos esto:

Actionscript:
  1. import com.zguillez.utils.Delay
  2.  
  3. Delay.action(3, true, this, "saluda")
  4.  
  5. function saluda()
  6. {
  7.    trace("hola");
  8. }

De esta manera la función "saluda" se ejecutará a los 3 segundos de haber hecho la llamada.

Como parámetros a la función "accion" le pasaremos un boolean indicándole si el valor numérico son segundos o fotogramas.

Actionscript:
  1. _tiempo = (segundos == true) ? tiempo * 1000 : tiempo / _ruta.stage.frameRate * 1000 ;

También le indicaremos la ruta de la función a ejecutar con lo que puede estar dentro de cualquier MovieClip o clase.

La clase dispone de una función "reset" que permite cancelar la ejecución de la función en el caso de que aun estemos a la espera.

Post relacinonados:

  1. La clase Timer
  2. Clase de ActionScript 3 para añadir sombras a MovieClips
  3. Clase para cargar contenido externo en Actionscript 3
  4. Añadir funciones al teclado con Actionscript 3
  5. Clase para dibujar líneas de puntos en ActionScript 3

2 Comments »

  1. Comment by Rafeo — 8 February, 2012 @ 12:41 pm

    Gracias Zguillez, ya se a dónde venir cuando se me presente este problem.

  2. Comment by Martín Dos Santos — 8 February, 2012 @ 12:41 pm

    Se agradece, estoy aprendiendo a actionscript y esto me ha sido muy util también podrías poner unas tags como delay con actionscript igual con este comentario basta seguro.

    Martín Dos Santos
    Turismo

RSS feed for comments on this post. TrackBack URI

Leave a comment

Get Adobe Flash playerPlugin by wpburn.com wordpress themes