Very short trick: addEventListener () AS3, a handler more events

In Actionscript 3.0 you must use addEventListener() to intercept any event:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/ **
* Given a MovieClip mc, you set events and handlers
* /
( MouseEvent . CLICK , on_click ) ; mc. addEventListener ( MouseEvent . CLICK, on_click);
( MouseEvent . ROLL_OVER , on_roll_over ) ; mc. addEventListener ( MouseEvent . ROLL_OVER, on_roll_over);
( MouseEvent . ROLL_OUT , on_roll_out ) ; mc. addEventListener ( MouseEvent . ROLL_OUT, on_roll_out);
/ **
* Functions hander for the above events
* /
e : MouseEvent ) : void { on_click function (e: MouseEvent ): void {
/ / Click
}
e : MouseEvent ) : void { on_roll_over function (e: MouseEvent ): void {
/ / Roll over
}
e : MouseEvent ) : void { on_roll_out function (e: MouseEvent ): void {
/ / Roll-out
}

Alternatively you can use the form:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
( MouseEvent . CLICK , on_handler ) ; mc. addEventListener ( MouseEvent . CLICK, on_handler);
( MouseEvent . ROLL_OVER , on_handler ) ; mc. addEventListener ( MouseEvent . ROLL_OVER, on_handler);
( MouseEvent . ROLL_OUT , on_handler ) ; mc. addEventListener ( MouseEvent . ROLL_OUT, on_handler);
/ **
* I create a single function to handle all events
* /
e : MouseEvent ) : void { on_handler function (e: MouseEvent ): void {
e . type ) { switch (and. type) {
. CLICK : houses MouseEvent . CLICK:
/ / Click
break;
. ROLL_OVER : houses MouseEvent . ROLL_OVER:
/ / Roll over
break;
. ROLL_OUT : houses MouseEvent . ROLL_OUT:
/ / Roll-out
break;
}
}

9 comments: "Very short trick: addEventListener () AS3, a handler more events"

  1. January 11, 2009 The best posts of the week # 9 | EmaWebDesign:: Web Programming / Web Design :

    [...] Very short trick: addEventListener () AS3, a handler more events in Actionscript 3.0 you must use the addEventListener () to intercept any [...]

  2. December 2, 2009 Roberto:

    Hello, I like how you've improved, but I ask you something, if I had more than one MovieClip mc, type: mc1, mc2 ... .. (could be for example the buttons on a menu), how to manage them? Thanks

  3. December 2, 2009 Giovambattista Fazioli :

    @ Robert:

    if I had more than one MovieClip mc

    , in questo caso MouseEvent . In the function on_handler you can refer to the object of type Event , in this case MouseEvent . This object, in fact, allows you to have information about the object that received the event. If you try this:

    1
    2
    3
    4
    e : MouseEvent ) : void { on_handler function (e: MouseEvent ): void {
    e . target . name ) ; trace (and. target. name);
    / / ...
    }

    ) ti permetterà di sapere quale MovieClip hai selezionato. The property target (or currentTarget ) will let you know that you have selected MovieClip.
    che, anche se a prima vista sembrano del tutto identici, in alcuni casi (vedi bottoni complessi costruiti con più MovieClip annidati) possono darti la corretta informazione che cerchi. See the documentation is in the detail of target that currentTarget that although at first sight appear to be quite identical, in some cases (see buttons built with more complex nested MovieClips) can give you the correct information you are looking for.

  4. December 2, 2009 Roberto:

    @ Giovambattista Fazioli: I'll go look ...:) ... nice ... if I resubmit ..:)

  5. December 2, 2009 Roberto:

    @ Giovambattista Fazioli:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    ( MouseEvent . MOUSE_OVER , on_handler ) ; btn1_mc. addEventListener ( MouseEvent . MOUSE_OVER, on_handler);
    ( MouseEvent . MOUSE_OUT , on_handler ) ; btn1_mc. addEventListener ( MouseEvent . MOUSE_OUT, on_handler);

    e : MouseEvent ) { on_handler function (e: MouseEvent ) {
    e . type ) { switch (and. type) {
    . MOUSE_OVER : houses MouseEvent . MOUSE_OVER:
    e . currentTarget . name ) ; //funziona, visualizza "btn1_mc" ossia il nome dell'istanza del primo pulsante trace (and. currentTarget. name) / / works, it displays "btn1_mc" or the name of the first button
    ( 2 ) ; btn1_mc. gotoAndPlay (2);
    break;
    . MOUSE_OUT : houses MouseEvent . MOUSE_OUT:
    ( 16 ) ; btn1_mc. gotoAndPlay (16);
    break;
    }
    }

    Then

    1
    e . currentTarget . name ) trace (and. currentTarget. name)

    works, but how can I attach to education next sequence? in place of

    1
    ( 2 ) ; btn1_mc. gotoAndPlay (2);
  6. December 2, 2009 Giovambattista Fazioli :

    @ Robert: It is very simple: e.target is a pointer to the instance of the MovieClip, then just use:

    1
    . gotoAndPlay ( 2 ) ; and. target. gotoAndPlay (2);

    In practice btn1_mc , or any other instance, corresponds to e.target

  7. December 2, 2009 Roberto:

    @ Giovambattista Fazioli: Well what to say, I tried to be good with geek
    [Cc_actionscript3] (e.currentTarget.name). GotoAndPlay (2) [\ as] ... taken away ... And so name the parentheses, and function ... so ... .. THANK YOU ....:)

  8. December 2, 2009 Roberto:

    @ Robert: I would conclude, if someone tried to info about that in the case with more cubic meters (eg buttons), with an array and a click you can group and assign different mc "addEventListener" es :

    1
    2
    3
    4
    5
    var i : int = 0 ; i < btn_array . length ; i ++ ) { for (var i: int = 0, i <btn_array. length; i + +) {
    . addEventListener ( MouseEvent . CLICK , on_handler ) ; btn_array [i]. addEventListener ( MouseEvent . CLICK, on_handler);
    . addEventListener ( MouseEvent . MOUSE_OVER , on_handler ) ; btn_array [i]. addEventListener ( MouseEvent . MOUSE_OVER, on_handler);
    . addEventListener ( MouseEvent . MOUSE_OUT , on_handler ) ; btn_array [i]. addEventListener ( MouseEvent . MOUSE_OUT, on_handler);
    }

    ... .. Hello and I hope to be useful to someone else ....:)

  9. December 2, 2009 Giovambattista Fazioli :

    @ Roberto: excellent! Also finally, if the series of "buttons" (MovieClip) are placed in a container MovieClip, you can even avoid the array and loop over all the "children" of the container. In this regard I suggest you read Everything you ever wanted to do with Actionscript 3
    Greetings and thanks for your contributions

Leave a comment

XHTML TAG PERMIT: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> INSERTION CODE:
 <pre></pre> // blocco generico <code></code> // blocco generico [cc_actionscript][/cc_actionscript] // Actionscript [cc_actionscript3][/cc_actionscript3] // Actionscript 3 [cc_css][/cc_css] // CSS Style Sheet [cc_html][/cc_html] // HTML [cc_js][/cc_js] // Javascript [cc_objc][/cc_objc] // Objective-C [cc_php][/cc_objc] // PHP [cc_sql][/cc_sql] // SQL 


Stop SOPA