Estoy en un dilema. No se cual es mas efectivo a la hora de utilizar para poder comunicar varias clases.
En el DispatchEvent el unico incoveniente que le veo que tengo que añadir un ollente en el addEventListener para poder capturar ese momento y posteriormente eleminarlo si no se vuelve a utilizar.
Por otro lado, tengo Function y con el metodo call hago casi lo mismo, salvo que no tengo que estar eliminando nada.
Tengo un ejemplo sencillo donde puedo enseñaros a lo que me refiero. En este ejemplo os detallo un tipico cargador de imagenes externas donde hago referencia a los dos metodos a utilizar. Si no doy valores a las variabes de funciones actuo por eventos.
este es la clase:
Código ActionScript :
package { import flash.display.Loader; import flash.system.LoaderContext; import flash.net.URLRequest; import flash.events.Event; import flash.events.ProgressEvent; import flash.events.IOErrorEvent; public class CargarImagen extends Loader { private var Carpeta: String= "imagenes/"; private var Archivo: String; public var FuncionAbierto: Function; // Esquema --> function NombreFunction () public var FuncionIniciado: Function; // Esquema --> function NombreFuncion (Ancho: Number, Alto: Number) public var FuncionProgreso: Function; // Esquema --> function NombreFuncion (bytesTotales: Number, bytesLeidos: Number) public var FuncionCompleta: Function; // Esquema --> function NombreFunction (NombreObjeto: DisplayObject) public var FuncionError: Function; // Esquema --> function NombreFunction (Error: IOErrorEvent) public function CargarImagen (Nombre: String) { super(); Archivo= Nombre; this.contentLoaderInfo.addEventListener (Event.OPEN, estaAbierto); this.contentLoaderInfo.addEventListener (Event.INIT, estaIniciado); this.contentLoaderInfo.addEventListener (ProgressEvent.PROGRESS, estaProgreso); this.contentLoaderInfo.addEventListener (Event.COMPLETE, estaCompletado); this.contentLoaderInfo.addEventListener (IOErrorEvent.IO_ERROR, hayError); } public function Iniciar(): void { this.load (new URLRequest (Carpeta+Archivo), new LoaderContext ()); } private function QuitarEventos (): void { this.contentLoaderInfo.removeEventListener (Event.OPEN, estaAbierto); this.contentLoaderInfo.removeEventListener (Event.INIT, estaIniciado); this.contentLoaderInfo.removeEventListener (ProgressEvent.PROGRESS, estaProgreso); this.contentLoaderInfo.removeEventListener (Event.COMPLETE, estaCompletado); this.contentLoaderInfo.removeEventListener (IOErrorEvent.IO_ERROR, hayError); } private function estaAbierto (e: Event): void { if (FuncionAbierto!=null) FuncionAbierto.call (null) else dispatchEvent (new Event (Event.OPEN,false,false)); } private function estaIniciado (e: Event): void { if (FuncionIniciado!=null) FuncionIniciado.call (null,e.target.width,e.target.height) else dispatchEvent (new Event (Event.INIT,false,false)); } private function estaProgreso (e: ProgressEvent): void { if (FuncionProgreso!=null) FuncionProgreso.call (null,e.bytesTotal,e.bytesLoaded) else dispatchEvent (new ProgressEvent (ProgressEvent.PROGRESS,false,false,e.bytesLoaded,e.bytesTotal)); } private function estaCompletado (e: Event): void { QuitarEventos(); if (FuncionCompleta!=null) FuncionCompleta.call (null,e.target.content) else dispatchEvent (new Event (Event.COMPLETE,false,false)); } private function hayError (e: IOErrorEvent): void { QuitarEventos(); if (FuncionError!=null) FuncionError.call (null,e) else dispatchEvent ( new IOErrorEvent (IOErrorEvent.IO_ERROR,false,false,e.text,e.errorID)); } } }
Ahora para poder utilizarlo:
para utilizar "Function"
Código ActionScript :
import CargarImagen; import flash.display.DisplayObject; var Carga: CargarImagen= new CargarImagen ("Imagen.jpg"); Carga.FuncionCompleta= ImagenCompleta; Carga.FuncionProgreso= ImagenProgreso; Carga.Iniciar(); function ImagenCompleta (Imagen: DisplayObject): void { addChild(Imagen); } function ImagenProgreso (BytesTotales: Number, BytesLeidos: Number): void { trace(BytesLeidos+"/"+BytesTotales); }
para utilizar Eventos
Código ActionScript :
import CargarImagen; import flash.display.DisplayObject; import flash.events.Event; import flash.events.ProgressEvent; var Carga: CargarImagen= new CargarImagen ("Imagen.jpg"); Carga.addEventListener (Event.COMPLETE, ImagenCompleta); Carga.addEventListener (ProgressEvent.PROGRESS, ImagenProgreso); Carga.Iniciar(); function ImagenCompleta (e: Event): void { addChild(e.target.content); Carga.removeEventListener (Event.COMPLETE, ImagenCompleta); Carga.removeEventListener (ProgressEvent.PROGRESS, ImagenProgreso); } function ImagenProgreso (e: ProgressEvent): void { trace(e.bytesLoaded+"/"+e.bytesTotal); }
El caso es que el codigo es muy parecido entre uno y otro, y la efectividad creo que tambien (el tiempo de ejecucion es el mismo), de alli mi duda.
Agradeceria mucho vuestros comentarios y experiencia.
saludos