jagguy
asked on
pass variable
I am sure i asked this before and couldnt find it the answer. I will check again.
to pass a variable in a custom event I need to create a custom class and override a clone function?
i want the event picked up by parent class
dispatchEvent(new Event("introClicked")); //from
dispatchEvent(new Event("introClicked",a)); //to
to pass a variable in a custom event I need to create a custom class and override a clone function?
i want the event picked up by parent class
dispatchEvent(new Event("introClicked")); //from
dispatchEvent(new Event("introClicked",a)); //to
What you want is this:
Then you dispatch it as follows:
class myEvent extends EventWhat this does is extend event so that you pass a string, as usual, and you can pass this object a. bubbles and cancelable default the same way they do with the regular Event, so the user can tweak them if they want - they just have to pass the a object first.
{
public var A:Object
function myEvent( str:String, a:Object, bubbles:Boolean=false, cancelable:Boolean = false )
{
super( str, bubbles, cancelable );
this.A = a;
}
}
Then you dispatch it as follows:
dispatchEvent( new myEvent( "introClicked", a ) );To get the "a" object:
function myListener( e:myEvent ):void
{
trace( "Object = " + e.A );
}
ASKER
isnt the second parameter you cant override as it has to be a boolen?
dispatchEvent( new myEvent( "introClicked", a,b ) );
I want to pass an integer variable with the event which here it would be 'b'
dispatchEvent( new myEvent( "introClicked", a,b ) );
I want to pass an integer variable with the event which here it would be 'b'
What moagrius told you was for the Event class. The second parameter of the Event class has to be Boolean. The class I showed you is a derived class, and the second parameter in my example is an object. You can change it to an integer if you need.
You'll notice that in the line beginning super, it passes a string followed by two Booleans. The "super" is constructing the Event class, and passes in all the necessary parameters.
Take the class for a spin, you'll see it works fine. *:-]
You'll notice that in the line beginning super, it passes a string followed by two Booleans. The "super" is constructing the Event class, and passes in all the necessary parameters.
Take the class for a spin, you'll see it works fine. *:-]
just a quick note - you should make sure to define the overridden clone method if you want to bubble.
just a quick addition like this should be fine:
override public function clone():Event {
return new myEvent(type, A, bubbles, cancelable);
}
just a quick addition like this should be fine:
override public function clone():Event {
return new myEvent(type, A, bubbles, cancelable);
}
ASKER
ok let me do an example on my own and I will post back
ASKER
Ok I get an error when getting variable from e event
Access of possibly undefined property 'A' through a reference with static type flash.events:Event.
this si what I have
//main
myplayer.addEventListener( "eventClic ked",event ClickedHan dler);
private function eventClickedHandler(e:Even t):void {
trace( "Object = " + e.A ); //error
}
//player
private var myevent:ClassEvent;
private var a:int ;
public function goEvent():void{
a=4;
dispatchEvent( new ClassEvent( "eventClicked", a ) );
}
Access of possibly undefined property 'A' through a reference with static type flash.events:Event.
this si what I have
//main
myplayer.addEventListener(
private function eventClickedHandler(e:Even
trace( "Object = " + e.A ); //error
}
//player
private var myevent:ClassEvent;
private var a:int ;
public function goEvent():void{
a=4;
dispatchEvent( new ClassEvent( "eventClicked", a ) );
}
ASKER
Ok I get an error when getting variable from e event
Access of possibly undefined property 'A' through a reference with static type flash.events:Event.
this si what I have
//main
myplayer.addEventListener( "eventClic ked",event ClickedHan dler);
private function eventClickedHandler(e:Even t):void {
trace( "Object = " + e.A ); //error
}
//player
private var myevent:ClassEvent;
private var a:int ;
public function goEvent():void{
a=4;
dispatchEvent( new ClassEvent( "eventClicked", a ) );
}
//event
package {
import flash.events.Event;
public class ClassEvent extends Event
{
public var A:int ;
public function ClassEvent( str:String, a:int, bubbles:Boolean=false, cancelable:Boolean = false )
{
super( str, bubbles, cancelable );
this.A = a;
}
override public function clone():Event
{
return new ClassEvent(type, A, bubbles, cancelable);
}
}
}
Access of possibly undefined property 'A' through a reference with static type flash.events:Event.
this si what I have
//main
myplayer.addEventListener(
private function eventClickedHandler(e:Even
trace( "Object = " + e.A ); //error
}
//player
private var myevent:ClassEvent;
private var a:int ;
public function goEvent():void{
a=4;
dispatchEvent( new ClassEvent( "eventClicked", a ) );
}
//event
package {
import flash.events.Event;
public class ClassEvent extends Event
{
public var A:int ;
public function ClassEvent( str:String, a:int, bubbles:Boolean=false, cancelable:Boolean = false )
{
super( str, bubbles, cancelable );
this.A = a;
}
override public function clone():Event
{
return new ClassEvent(type, A, bubbles, cancelable);
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
you can give a custom event custom variables when defining the extended event class.
the example you gave isn't correct. the second argument in the Event constructor is a boolean (true or false).
new Event("whatever", true);
means it'll bubble up the display list - e.g, pass it to it's parent.
if the bubble argument - the second argument - is false, it will not bubble.