I made my own AS3-DialogBox-class. It has 2 Buttons (\"Yes\" and \"No\")
Within the class there is a listener that starts a function when one of the Buttons was pushed.<
DialogEvent extends Event
.private var _choice:String = "";
public function get choice():String { return _choice; }
to retrieve the value from the objectEvent
class, then add a first parameter that takes the chosen value public function DialogEvent (choice:String,....
_choice = choice;
in the constructor in addition to calling the super()
with the rest of the parameterspublic static const CHOICE:String = "choice";
for convenience, this is optional. It helps checking for typos in the code at compile time.DialogBox
when one of the Buttons was pushed
dispatchEvent(new DialogEvent ("replace this with the button text",
DialogEvent.CHOICE));
to dispatch the eventdialogBox.addEventListener(DialogEvent.CHOICE,
onDialogChange);
private function onDialogChange(event:DialogEvent):void
, try
trace(event.choice);
public override function clone():Event
that calls your
modified constructor to take your additional variable into account
when cloning your custom event object as demonstrated in the
documentation of the clone() methodIn this case the dialog dispatches the custom event. You can also do this on the previous level and let each labeled button dispatch a custom event.
Regarding the questions in the comment:
What do you mean with the word "duplicate" [in step 4.? ... I]s there a location where i can see and "copy" (= duplicate) the code of As3's "own" Event-class?
This is the intended meaning of "duplicate". The location you are looking for is the documentation. Take a look at the documentation of the constructor of the Event class:
Event () Constructor
public function Event(type:String, bubbles:Boolean = false, cancelable:Boolean = false)
To make this clear: you do not have to duplicate the constructor like this. You can choose other names for the parameters or leave them out. However, it is recommended to duplicate the constructor, because you want to use your custom event like any other event with the addition of some other parameter (the choice).
public function DialogEvent(choice:String, type:String, bubbles:Boolean = false, cancelable:Boolean = false)
{
super(type, bubbles, cancelable);
_choice = choice;
What is the reason for declaring this constant. Doesn't it work WITHOUT this constant?
The constant is optional. However, it is common to have constants like that among the built in Event classes, e.g. the constants defined by MouseEvent:
CLICK : String = "click"
CONTEXT_MENU : String = "contextMenu"
DOUBLE_CLICK : String = "doubleClick"
The reason to use them is to allow checking the event types at compile time. Compare these two calls to the constructor:
new DialogEvent ("replace this with the button text",
DialogEvent.CHOICE));
new DialogEvent ("replace this with the button text",
"chojce"));
Both compile, only the first works as intended, given that you are listening for an event of type "choice". There's no way to check for a wrong spelled type if it is given as a String literal. Properties of a class however can be checked at compile time:
new DialogEvent ("replace this with the button text",
DialogEvent.CHOJCE)); // throws an error
Additionally, it allows for code completion if you are using an editor that supports it.
Just for completeness:
Shall I just create the constructor like:
public function DialogEvent (choice:String, receivedEvent:Event) { _choice = choice; super(receivedEvent); }
As you can see in the documentation, the constructor of the Event
class does not take a single argument of type Event
. this code does not compile.