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.
And within this listener-function I can get and trace the Buttons label ("Yes" or "No") by calling event.currentTarget.MyButtonTextfield.text.
So my question is:
How can I RETURN these pushed values ("Yes" or "No") from within the classes listener-function to the Main.fla ?
Because within the MAIN.fla I want to put the call of the DialogBox in an IF-ELSE-Statement, which shall "catch" the pushed "Yes" or "No".
What is the official way to do something like that?
I hope you can help me! Thanks in advance!
Tine
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 valuepublic function DialogEvent (choice:String,....
_choice = choice;
in the constructor in addition to calling thesuper()
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 pusheddispatchEvent(new DialogEvent ("replace this with the button text", DialogEvent.CHOICE));
to dispatch the eventdialogBox.addEventListener(DialogEvent.CHOICE, onDialogChange);
private function onDialogChange(event:DialogEvent):void
, trytrace(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 theclone()
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:
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:
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).
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
:The reason to use them is to allow checking the event types at compile time. Compare these two calls to the constructor:
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:
Additionally, it allows for code completion if you are using an editor that supports it.
Just for completeness:
As you can see in the documentation, the constructor of the
Event
class does not take a single argument of typeEvent
. this code does not compile.