Posted By

markaltman on 08/21/09


Tagged

button options listeners buttonMode mouseChildren


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

peterbelsky
TrentSkunk


Button Code - with useful options


 / Published in: ActionScript 3
 

URL: markaltman.ca

Code to make Sprite or MovieClip act as a button. Useful options include buttonMode to create "hand" on mouseover and mouseChildren=false so you can target the button correctly (for example, when using event.target) and not any nested children by mistake.

  1. // assume myButton is an existing sprite that you want to work like a button
  2.  
  3. myButton.addEventListener(MouseEvent.MOUSE_OVER,buttonOver);
  4. myButton.addEventListener(MouseEvent.MOUSE_OUT,buttonOut);
  5. myButton.addEventListener(MouseEvent.CLICK,buttonClick);
  6. myButton.buttonMode=true;
  7. myButton.mouseChildren = false;
  8. function buttonOver(event:MouseEvent)
  9. {
  10. event.target.scaleX=1.2; // make button a bit larger
  11. event.target.scaleY=event.target.scaleX;
  12. }
  13. function buttonOut(event:MouseEvent)
  14. {
  15. event.target.scaleX=1;
  16. event.target.scaleY=event.target.scaleX;
  17. }
  18. function buttonClick(event:MouseEvent)
  19. {
  20. // click action here
  21. }

Report this snippet  

You need to login to post a comment.