How to create a reusable toggle button in AS3?

Let me show you ,

How to create a reusable toggle button in AS3?

This is quite Simple. Create a new generic button class, and add all of your event listeners within. For each new button you want to create, just extend your generic button and fill in the required code within the event listeners.:

classGenericToggleButtonextendsButton{publicGenericToggleButton(){this.addEventListener(MouseEvent.MOUSE_OVER, rolloverToggle);this.addEventListener(MouseEvent.MOUSE_OUT, rolloutToggle);this.addEventListener(MouseEvent.MOUSE_CLICK, toggleClick);}protectedfunction rolloverToggle(event:MouseEvent):void{this.gotoAndStop(this.buttonState+" over");}protectedfunction rolloutToggle(event:MouseEvent):void{this.gotoAndStop(this.buttonState);}protectedfunction toggleClick(event:MouseEvent):void{if(this.buttonState =="on"){this.buttonState ="off";this.gotoAndStop(1);}else{this.buttonState ="on";}}}

Now just extend that class and add your functionality.

classNewButtonextendsGenericToggleButton{publicNewButton(){super();}overrideprotectedfunction toggleClick(event:MouseEvent):void{super.toggleClick(event);// do magic for this button}// ETC}
posted @ 2013-01-10 02:01  小糊涂的超级blog  阅读(316)  评论(1编辑  收藏  举报