In our last example we have seen, how we can replace the legacy onRelease event handlers by using addEventHandler. At the first look, it seems to be more complicated than before, and that's true. But if you look at the following more complicated example, which makes use of event bubbling, you'll see the positive effects of this new event system.
We create a grid of MovieClips, which react to mouseUp and emit a new ReleaseEvent to interested parties in the event chain.
Instead of having to assign event handlers to particular, probably deeply nested items, we can simply sit and wait, until the events bubble up. All the problems with late initialization of inner clips or clips, which are loaded at runtime, are easily handled by this new system. Also the event system is unified for MovieClips and components, which means we have to learn about one system only, and not two or more, like it was before.
Also notice the new "override" keyword. Whenever you need to overwrite an inherited method, you need to use it, otherwise the compiler complains. This is a good tool, to avoid overwriting, if you don't want it.
Cheers,
Ralf.
/////////////////
因为是个人读书的笔记,所以没有进行翻译,只是对其中关键地方进行注释。如有错误欢迎指出。
原文转载自: www.helpqlodhelp.com/blog/
We create a grid of MovieClips, which react to mouseUp and emit a new ReleaseEvent to interested parties in the event chain.
package { import flash.events.Event; import flash.events.MouseEventType; import flash.util.trace; import flash.display.Sprite; public class Test extends Sprite{ public function Test() { for( var i:int = 0; i<10; i++){ for( var j:int=0; j<10; j++){ var mc:Sprite= new Test2(i,j); this.addChild( mc); } } this.addEventListener("Release", releaseListener); } public function releaseListener( evt:Event){ trace( "Test1::releaseListener " + evt); } } private class Test2 extends Sprite{ private var i:int; private var j:int; public function Test2(i:int, j:int){ this.i = i; this.j = j; graphics.beginFill(0xff0000); graphics.drawRect(0, 0, 10, 10); graphics.endFill(); x = i * 11; y = j * 11; this.addEventListener(MouseEventType.MOUSE_UP, mouseUpListener); } public override function toString():String{ return '[Test2 i=' + i + ' j=' + j + ']'; } public function mouseUpListener( evt:Event){ trace( "Test2::mouseUpListener " + evt); //we don't want this event to bubble up evt.stopPropagation(); //instead we create a new event and dispatch it this.dispatchEvent( new ReleaseEvent( this)); } } private class ReleaseEvent extends Event { private var source:Test2; public function ReleaseEvent( source:Test2){ super("Release", true, true); this.source = source; } public override function toString():String{ return '[Event type="Release" bubbles=true ' + 'cancelable=true source="' + source + '"]"'; } } }
Instead of having to assign event handlers to particular, probably deeply nested items, we can simply sit and wait, until the events bubble up. All the problems with late initialization of inner clips or clips, which are loaded at runtime, are easily handled by this new system. Also the event system is unified for MovieClips and components, which means we have to learn about one system only, and not two or more, like it was before.
Also notice the new "override" keyword. Whenever you need to overwrite an inherited method, you need to use it, otherwise the compiler complains. This is a good tool, to avoid overwriting, if you don't want it.
Cheers,
Ralf.
/////////////////
因为是个人读书的笔记,所以没有进行翻译,只是对其中关键地方进行注释。如有错误欢迎指出。
原文转载自: www.helpqlodhelp.com/blog/
回复Comments
{commenttime}{commentauthor}
{CommentUrl}
{commentcontent}