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
- Create a class
DialogEvent extends Event
.
- Add
private var _choice:String = "";
- Add
public function get choice():String { return _choice; }
to retrieve the value from the object
- Duplicate the constructor of the
Event
class, then add a first parameter that takes the chosen value public function DialogEvent (choice:String,....
- Set
_choice = choice;
in the constructor in addition to calling the super()
with the rest of the parameters
- Add
public static const CHOICE:String = "choice";
for convenience, this is optional. It helps checking for typos in the code at compile time.
- In
DialogBox
when one of the Buttons was pushed
dispatchEvent(new DialogEvent ("replace this with the button text",
DialogEvent.CHOICE));
to dispatch the event
- In main.fla (suggestion: do not use all caps file names) wait for
the event to happen
dialogBox.addEventListener(DialogEvent.CHOICE,
onDialogChange);
- In
private function onDialogChange(event:DialogEvent):void
, try
trace(event.choice);
- Add
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()
method
In 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.