-
Hmmm.. Events?
Hey everybody
I'm trying to pass an argument with a dispatchEvent, but I can't seem to understand it. I know that you are able to make custom events, but I'm really confused??
What is an custom event? What is exactly the difference between the Event object and the constant that you give the new event object?
I have read all the actionscript tips of the day at Kirupa, but that just makes me more confused. Does anyone have a complete exmaple of how to use a custom event, that passes a parameter to the listener?
I can't figure out what is what with all these events :-)
-
It's pretty simple really. In order to dispatch an event you just need to create one. A custom event is essentially an object with information about the event.
to dispatch an event just do dispatchEvent(new Event('MY_EVENT'));
-
But... the event "MY_EVENT" in your example is just a public constant string?
That's not an event object? I don't get it?
-
And here's an improvised complete but trivial example
Code:
public class StupidEvent extends Event{
public static var STUPID:String = "STUPID";
public var anarg:String; //argument
public function StupidEvent(type:String, a:String=''){
super(type);
anarg = a;
}
}
Code:
public class EventTester extends Sprite{
public var listenerSprite:Sprite;
public function EventTester(){
listenerSprite = new Sprite();
addChild(listenerSprite);
listenerSprite.graphics.beginFill(0xff0000);
listenerSprite.graphics.drawCircle(0, 0, 10);
listenerSprite.graphics.endFill();
listenerSprite.addEventListener(MouseEvent.CLICK, send);
addEventListener(StupidEvent.STUPID, hithere);
}
public function send(evt:MouseEvent):void{
var blah:String = 'blah';
var stupid:StupidEvent = new StupidEvent(StupidEvent.STUPID, blah);
dispatchEvent(stupid);
}
public function hithere(sevt:StupidEvent):void{
trace('hi there, the arg in the stupid event is set to: "+sevt.anarg);
}
}
Edit: d'oh. forgot "extends Event"
-
I GET IT!!!!!!!!!!!!!!! wuhu... :-) Thanks alot
-
5Tons, to properly extend an Event, you must also override the clone() and toString() methods or it won't work right. And you also shouldn't cripple the event by stripping out the bubbles and cancelable parameters.
Code:
package
{
import flash.events.Event;
public class StupidEvent extends Event
{
public static const STUPID:String = "stupid";
public var var1:String;
public function StupidEvent($type:String, $var1:String, $bubbles:Boolean = false, $cancelable:Boolean = false)
{
super($type, $bubbles, $cancelable);
var1 = $var1;
}
public override function clone():StupidEvent
{
return new StupidEvent(type, var1, bubbles, cancelable);
}
public override function toString():String
{
return formatToString("StupidEvent", "type", "var1", "bubbles", "cancelable", "eventPhase");
}
}
}
-
True enough. But I did call it a StupidEvent. :)
-
-
you guys are funny- i have a quick question...when i have done custom events, i always forget to override the toString() function...the events have still worked (by luck i suppose) but can anyone explain what the toString() function does, and why it should be overran?
-
It's just a method that takes the objects properties and print them out as a string. If you don't override that method, you won't be able to trace out the right properties of the custom event. I know tha much :-)