AS3 Custom DialogBox: How to send pushed Buttons label to Main.fla

前端 未结 1 1137
情深已故
情深已故 2021-01-26 00:56

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.<

1条回答
  •  野趣味
    野趣味 (楼主)
    2021-01-26 01:11

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

    0 讨论(0)
提交回复
热议问题