Foros del Web » Creando para Internet » Flash y Actionscript »

Eventos en Clases de AS 2

Estas en el tema de Eventos en Clases de AS 2 en el foro de Flash y Actionscript en Foros del Web. Hola, estoy tratando de implementear una clase que pueda generar eventos. Lo he logrado pero a medias. Utilizo el eventDispatcher pero solo funciona si el ...
  #1 (permalink)  
Antiguo 16/12/2005, 21:21
 
Fecha de Ingreso: abril-2005
Ubicación: Ramos Mejía
Mensajes: 113
Antigüedad: 18 años, 11 meses
Puntos: 0
Eventos en Clases de AS 2

Hola, estoy tratando de implementear una clase que pueda generar eventos. Lo
he logrado pero a medias.

Utilizo el eventDispatcher pero solo funciona si el usuario invoca la rutina
que dispara el evento.
Si, por ejemplo, yo mismo, desde la clase intento disparar el evento, (desde
un onRelease de algún MovieClip dentro de mi clase) el evento no se dispara.
  #2 (permalink)  
Antiguo 16/12/2005, 21:47
Avatar de TMeister
Crazy Coder
 
Fecha de Ingreso: enero-2002
Ubicación: En la Oficina
Mensajes: 2.880
Antigüedad: 22 años, 2 meses
Puntos: 193
te conteste en los foros de macromedia pero lo pongo aqui tambien para consultas futuras.

Que tal.

Si no pones codigo es dificil ayudarte pero aqui te dejo algo con lo que puedes empezar

Eventos.as

Código:
import mx.events.EventDispatcher
 [Event("onClick")]
 [Event("onOver")]
class eventos extends MovieClip
{
    private var boton : MovieClip;
    function eventos ()
    {
        EventDispatcher.initialize (this);
        init ();
    }
    function init ()
    {
        boton.onRelease = function ()
        {
            _parent.dispatchEvent (
            {
                type : "onClick", parametro1 : "algo", param2 : "algo2"
            })
        }
        boton.onRollOver = function ()
        {
            _parent.dispatchEvent (
            {
                type : "onOver", parametro1 : "algo", param2 : "algo2"
            })
        }
    }
}
En tu Fla

Código:
clip_mc.addEventListener ("onClick", onClick);
clip_mc.addEventListener ("onOver", onOver);
function onClick (obj : Object)
{
    for (var e in obj)
    {
        trace ("key " + e + " value " + obj [e]);
    }
}
function onOver (obj : Object)
{
    for (var e in obj)
    {
        trace ("key " + e + " value " + obj [e]);
    }
}

Obviamente este clip debe tener el path a la clase y contener un Movieclip a su vez llamado boton.

Saludos!!
  #3 (permalink)  
Antiguo 17/12/2005, 12:05
 
Fecha de Ingreso: abril-2005
Ubicación: Ramos Mejía
Mensajes: 113
Antigüedad: 18 años, 11 meses
Puntos: 0
HOLA:
Perdón por insistir, pasa que soy nuevo con clases (en AS ojo).
Mi clase se llama botón y no hereda nada de un Movie Clip.
class boton{
var b:MovieClip;
var n:Number
//el botón base es un boton a partir del cual creo el cuerpo de este boton
function boton(texto, root, boton_base){
root.attachMovie(boton_base, "b"+n+"_mc",root.getNextHighestDepth());
b=root["b"+n+"_mc"];
n++;
}
}

Con este código controla un botón interno con muchas características. Pero no logro generar eventos que el creador del objeto pueda interceptar.

b.onRelease=function(){
lanzar evento.
}


Aca, obviamente falta el código que genera el evento. Pero no funciona si lo llamo desde la clase. Ahora, cuando la función que genera el evento es llamada desde el fla, entonces anda todo.
El problema es que el evento Click debe ser lanzado desde el interior de la clase y no desde el interior.
  #4 (permalink)  
Antiguo 17/12/2005, 13:01
 
Fecha de Ingreso: abril-2005
Ubicación: Ramos Mejía
Mensajes: 113
Antigüedad: 18 años, 11 meses
Puntos: 0
Aca va el código fuente completo de mi clase botón que no puede generar un evento!!
import mx.events.EventDispatcher;
[Event("onClick")]
class boton {
private var _padre:MovieClip;
private var _texto:TextField;
private var _cara:MovieClip;
private var n:Number;
private var h:Number;
private var w:Number;
private var ch:Number;
private var cw:Number;

//declare the dispatchEvent, addEventListener and removeEventListener methods that EventDispatcher uses (Step2)
function dispatchEvent() {};
function addEventListener() {};
function removeEventListener() {};

function boton(mcName:String, texto_str:String, padre_mc:MovieClip, n:Number){
mx.events.EventDispatcher.initialize(this);
this.n=n;
this._padre=padre_mc;
this._padre.attachMovie(mcName,"cara"+n+"_mc",n+11 00);
this._cara=_padre["cara"+n+"_mc"];
this._cara.createTextField("texto_txt",this._cara. getNextHighestDepth(),2,2,this._cara._width-4,this._cara._height-4);
this._cara.texto_txt.text=texto_str;

this._cara.onRollOver=function(){
this._alpha=50;
};

this._cara.onRollOut=fuera;
this._cara.onReleaseOutside=fuera;

function fuera(){
this._alpha=100;
}

this._cara.onRelease=function(){
// raiseonRelease;
var eventObject:Object = {target:_parent, type:'onClick'};
//dispatch the event (Step5)
this._padre.dispatchEvent(eventObject);
};
}

public function raiseonRelease(){
trace("hola soy la clase boton");
//define the event object that is passed to any listeners when the event is broadcast (Step4)
// You must specify a target property and then name of the event or the event “type” property in the event object

var eventObject:Object = {target:this, type:'onClick'};
//dispatch the event (Step5)
_parent.dispatchEvent(eventObject);
}

function get _mc():MovieClip{
return this._cara;
}

function set _x(t:Number){
this._cara._x=t;
}
function get _x():Number{
return this._cara._x;
}
function set _y(t:Number){
this._cara._y=t;
}

function get _y():Number{
return this._cara._y;
}

function set _height(t:Number){
this._cara._height=t;
}

function get _height():Number{
return this._cara._height;
}

function set _width(t:Number){
this._cara._width=t;
}

function get _width():Number{
return this._cara._width;
}

//--------------------------------------------------------------------
}


---------------------------------------------------
en el fla.
//esto no funciona
var b1:boton=new boton("boton1", "Inicio", _root,0);
var myListener:Object=new Object;

b1._x=20;
b1._y=50;

b1.addEventListener("onClick", myListener);

myListener.onClick=function(evtObj){
trace("Hola soy yo");
}
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 20:23.