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}
原文地址:https://www.cnblogs.com/cnsoft/p/2853951.html