Flash CS3: The new event handling

Finally with ActionScript 3.0 no longer have two different management of events as they did before ActionScript 2.0 (see: Event management: similarities between Flash and Javascript ). The method addEventListener() , ubiquitous in the new architecture allows to manage new and exceptionally clean all possible events, even personal ones. The new organization in the package allows you to import the events that we serve and treat everyone equally:

1
2
3
4
/ / Events
. KeyboardEvent ; import flash.events. KeyboardEvent ;
. MouseEvent ; import flash.events. MouseEvent ;
. Event ; import flash.events. Event ;

The differences between the management of the events are so scomprase. . A MovieClip no longer has the pseudo-event properties like onRelease or onEnterFrame . The classic RollOver, for example, becomes:

1
2
3
4
5
( MouseEvent . ROLL_OVER , listener ) ; mio_mc. addEventListener ( MouseEvent . ROLL_OVER, listener);
...
e : MouseEvent ) : void { listener function (e: MouseEvent ): void {
...
}

The solution proposed in flash: how to initialize components in the constructor of a class , for example, results in ActionScript 3.0 is as follows:

1
2
3
4
5
6
7
8
9
10
11
12
. Event ; import flash.events. Event ;

MiaClasse extends MovieClip { public class MyClass extends MovieClip {

function MyClass () {
Event . ADDED_TO_STAGE , _init ) ; addEventListener ( Event . ADDED_TO_STAGE, _init);
}
/ /
_init ( e : Event ) : void { private function _init (e: Event ): void {
...
}
}

The management of the keyboard in a TextFiled is:

1
2
3
4
5
6
7
8
9
. KeyboardEvent ; import flash.events. KeyboardEvent ;
...
( KeyboardEvent . KEY_DOWN , _onKeyDown ) ; text_txt. addEventListener ( KeyboardEvent . KEY_DOWN, _onKeyDown);

e : KeyboardEvent ) : void { _onKeyDown function (e: KeyboardEvent ): void {
e . keyCode == 13 ) { if (e. keyCode == 13) {
...
}
}

This new event management, new to ActionScript, but known to those who work in JavaScript, for example (on ECMAScript 4), is also reflected in their management and event production. , e vogliamo fare in modo che quando viene premuto in tasto {INVIO} durante la digitazione viene rilasciato un nostro personale evento. Imagine you create a class TextFieldExt , an extension of the classic TextField , and we want to make sure that when button is pressed in the {INVIO} is released while typing one of our staff event. a bordo che chiamiamo text_txt . First of all we build our MovieClip with a TextField on board we call text_txt . As seen previously Associació a listener to this control and we generate our own personal event in a simple way:

1
2
3
4
5
6
7
8
9
10
. KeyboardEvent ; import flash.events. KeyboardEvent ;
...
( KeyboardEvent . KEY_DOWN , _onKeyDown ) ; text_txt. addEventListener ( KeyboardEvent . KEY_DOWN, _onKeyDown);

e : KeyboardEvent ) : void { _onKeyDown function (e: KeyboardEvent ): void {
e . keyCode == 13 ) { if (e. keyCode == 13) {
Event = new Event ( "RETURN" ) ; var I: Event = new Event ("RETURN");
dispatchEvent ( me ) ; this. dispatchEvent (me);
}
}

When we get to use our instance of TextFieldExt we could write:

1
2
3
4
5
( "RETURN" , _onReturn ) ; myTextFieldExt. addEventListener ("RETURN" _onReturn);
...
e : Event ) : void { _onReturn function (e: Event ): void {
...
}

. Where myTextFieldExt is an instance of our class TextFieldExt . Last, not least, as indicated in the Guide inside of Flash CS3, you can of course create custom events like classes, ie classes that we extend the class Event , then a more thorough and detailed. As the class Alarm proposed in this guide ... but we will see him again ;) .

One Response to "Flash CS3: The new event handling"

  1. April 15, 2008 Actionscript 3.0, all with the new operator | Undolog.com :

    [...] "Uniform", as happened with the events (see the new event handling in Flash CS3 and Flash CS3: The new event management), one of the many new features in ActionScript 3.0 is the disappearance of all those ad hoc methods [...]

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