AS3: Using currentTarget to listen for your buttons
A lot of times when I am working on a project that has a lot of buttons, I like to simplify my event listeners into two methods, watching the currentTarget to see where to go. This is extremely helpful when you have to have a mouse down effect that needs to keep looping, say on a zoom. So, when the user holds the mouse down, I add an ENTER_FRAME event that keeps the event in a loop. By using currentTarget and a switch statement, you can keep your listeners set up nice and clean, also helps the next guy figure out what you are trying to accomplish…
btnOne.addEventListener(MouseEvent.MOUSE_DOWN,checkMouseDown); btnOne.addEventListener(MouseEvent.MOUSE_UP,checkMouseUp); //or even btnTwo.addEventListener(MouseEvent.CLICK,checkMouseDown);
function checkMouseDown(e:MouseEvent):void { switch (e.currentTarget) { case btnOne : stage.addEventListener(Event.ENTER_FRAME,myMethod); break; case btnTwo : myMethod(null); break; } } function checkMouseUp(e:MouseEvent):void { switch (e.currentTarget) { case btnOne : stage.removeEventListener(Event.ENTER_FRAME, myMethod); break; } } function myMethod(e:Event):void{ //Do Something }
Other posts you may find helpful:
-
http://dmgdavepotts.wordpress.com/2010/02/10/websites-accesed-so-far-to-help-understand-actionscript-3/ websites accesed so far to help understand ActionScript 3 « Dave Potts Blog
