event listeners not duplicated

by bigfish
♥0 | Line 32 | Modified 2009-11-10 00:58:29 | MIT License
play

ActionScript3 source code

/**
 * Copyright bigfish ( http://wonderfl.net/user/bigfish )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/fTgq
 */

package {
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
    public class FlashTest extends Sprite {
		private var btn:Sprite;
        public function FlashTest() {
			//addbutton
			btn= new Sprite();
			btn.graphics.beginFill(0xFF0000);
			btn.graphics.drawRect(0,0,200,100);
			addChild(btn);
			//add listeners
			for(var i:int = 0; i < 500; ++i)
			{
				btn.addEventListener(MouseEvent.CLICK, hndlr, false, 0, true);
			}
			//notice that when you click the button, event fires only once
			var tf:TextField = new TextField();
			tf.text = "removeListener";
			tf.y = 110;
			tf.addEventListener(MouseEvent.CLICK, remove, false, 0, true);
			addChild(tf);
			
        }
		private function hndlr(e:MouseEvent):void
		{
			trace("click");
		}
		//only needs to be remove once
		private function remove(e:MouseEvent):void
		{
			btn.removeEventListener(MouseEvent.CLICK, hndlr, false);
		}
    }
}