Posted By

stiobhart on 06/11/09


Tagged

actionscript flash button actionscript3 college


Versions (?)

Who likes this?

6 people have marked this snippet as a favorite

johnwun
MacEvelly
pablomercado
sykespro
geekincognito
jondiscipulo


actionscript 3 - button action [listen for multiple events. eg. MOUSE_OVER, MOUSE_OUT, MOUSE_UP]


 / Published in: ActionScript 3
 

actionscript3 version of a more complex button action, which responds to rollover, rollout and clicking. this method uses one single 'buttonstuff' function with a 'switch' inside which causes the function to respond differently, depending on what the user action on the button was.

i think this is a neater way to do it than to have each individual 'addEventListener' trigger a separate function.

in actionscript3, button actions can no longer be attached to the buttons themselves or called from the main timeline. you need to put an 'addEventListener' in the timeline to 'listen' for interaction with the button and then call an appropriate function to deal with that action.

the button needs to have an instance name. in this case it is called 'stupidbutton'

  1. // add event listeners to listen for button events [mouse over, mouse out, mouse up]
  2. // and then - when one happens - trigger the function called 'buttonstuff'
  3. this.stupidbutton.addEventListener(MouseEvent.MOUSE_OVER, buttonstuff);
  4. this.stupidbutton.addEventListener(MouseEvent.MOUSE_OUT, buttonstuff);
  5. this.stupidbutton.addEventListener(MouseEvent.MOUSE_UP, buttonstuff);
  6. // end event listeners
  7.  
  8.  
  9. // buttonstuff function - triggered by the above listeners
  10. function buttonstuff(event:MouseEvent):void
  11. {
  12. // switch to 'switch' what the function does, depending
  13. //on which mouse event the button listener has picked up.
  14. //think of it as standing for "in case of this - do this"
  15. switch(event.type)
  16. {
  17.  
  18. // in 'case' the listener detected a mouse over
  19. case MouseEvent.MOUSE_OVER:
  20.  
  21. // replace the trace with your own button actions
  22. trace("button rolled over");
  23.  
  24. // each case always ends with a 'break'
  25. break;
  26. // end in 'case' the listener detected a mouse over
  27.  
  28. // in 'case' the listener detected a mouse out
  29. case MouseEvent.MOUSE_OUT:
  30.  
  31. // replace the trace with your own button actions
  32. trace("button rolled off");
  33.  
  34. // each case always ends with a 'break'
  35. break;
  36. // end in 'case' the listener detected a mouse out
  37.  
  38. // in 'case' the listener detected a mouse up
  39. case MouseEvent.MOUSE_UP:
  40.  
  41. // replace the trace with your own button actions
  42. trace("button released");
  43.  
  44. // each case always ends with a 'break'
  45. break;
  46. // end in 'case' the listener detected a mouse up
  47.  
  48. }
  49. // end switch to 'switch' what the function does, depending on
  50. //which mouse event the button listener has picked up
  51.  
  52. }
  53. // end buttonstuff function

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: sykespro on October 26, 2009

I like this approach! Thanks

Posted By: jondiscipulo on March 4, 2011

Thanks for posting this.

You need to login to post a comment.