Button Template Class in AS3


/ Published in: ActionScript 3
Save to your folder(s)

Here is a template button class, ive been using for my projects. Cheers!


Copy this code and paste it in your HTML
  1. /*
  2.  
  3. Author: Alvin Crespo
  4. Date: 3/1/2010
  5.  
  6. TemplateButton is a template class for buttons.
  7.  
  8. */
  9. package acrespo.classes.templates{
  10.  
  11. //display
  12. import flash.display.MovieClip;
  13.  
  14. //events
  15. import flash.events.Event;
  16. import flash.events.MouseEvent;
  17.  
  18. public class TemplateButton extends MovieClip{
  19.  
  20. private var cButtonArea:MovieClip;
  21.  
  22. public function Button(){ } //Constructor
  23.  
  24. protected function setClickArea(pButtonArea:MovieClip):void{ initiateClickArea(pButtonArea); }
  25.  
  26. private function initiateClickArea(pButtonArea:MovieClip):void{
  27. cButtonArea = pButtonArea;
  28. cButtonArea.buttonMode = true;
  29. cButtonArea.addEventListener(MouseEvent.MOUSE_DOWN, buttonDown);
  30. cButtonArea.addEventListener(MouseEvent.MOUSE_UP, buttonUp);
  31. cButtonArea.addEventListener(MouseEvent.MOUSE_OVER, buttonOver);
  32. cButtonArea.addEventListener(MouseEvent.MOUSE_OUT, buttonOut);
  33. }
  34.  
  35. private function buttonDown(e:MouseEvent):void {
  36. stage.addEventListener(MouseEvent.MOUSE_UP, buttonUp);
  37. gotoAndStop('down');
  38. }
  39.  
  40. private function buttonUp(e:MouseEvent):void {
  41. stage.removeEventListener(MouseEvent.MOUSE_UP, buttonUp);
  42. if (e.currentTarget != cButtonArea) {//onreleaseoutside
  43. gotoAndStop('normal');
  44. } else {//onrelease
  45. buttonUpAction();
  46. }
  47. }
  48.  
  49. protected function buttonUpAction():void{ }
  50.  
  51. private function buttonOver(e:MouseEvent):void {
  52. if (e.buttonDown) {//ondragover
  53.  
  54. } else {//onrollover
  55.  
  56. }
  57. gotoAndStop('over');
  58. }
  59.  
  60. private function buttonOut(e:MouseEvent):void {
  61. //The user is dragging something from the target object
  62. if (e.buttonDown) {//ondragout
  63. gotoAndStop('down');
  64. } else {//onrollout
  65. gotoAndStop('normal');
  66. }
  67. }
  68.  
  69. }//end of class
  70. }//end of package
  71.  
  72.  
  73. /*
  74.  
  75. The Following is how you would extend that class
  76.  
  77. */
  78. package acrespo.classes.buttons{
  79.  
  80. //classes
  81. import acrespo.classes.templates.TemplateButton;
  82. import acrespo.classes.MainDocument;
  83.  
  84. public class UploadFileButton extends TemplateButton{
  85.  
  86. public function UploadFileButton():void{
  87. setClickArea(buttonarea); //function of parent class TemplateButton
  88. }
  89.  
  90. override protected function buttonUpAction():void{
  91. MainDocument(parent)._FileManager.uploadFiles();
  92. }//end of buttonUpAction
  93.  
  94. }//end of class
  95. }//end of package

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.