How to create a reusable toggle button in AS3?

好久不见. 提交于 2019-12-08 18:25:34

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.:

class GenericToggleButton extends Button
{
    public GenericToggleButton()
    {
        this.addEventListener(MouseEvent.MOUSE_OVER, rolloverToggle);
        this.addEventListener(MouseEvent.MOUSE_OUT, rolloutToggle);
        this.addEventListener(MouseEvent.MOUSE_CLICK, toggleClick);
    }

    protected function rolloverToggle(event:MouseEvent):void
    {
         this.gotoAndStop(this.buttonState+" over");
    }

    protected function rolloutToggle(event:MouseEvent):void
    {
        this.gotoAndStop(this.buttonState);
    }

    protected function 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.

class NewButton extends GenericToggleButton
{
    public NewButton()
    {
        super();
    }

    override protected function toggleClick(event:MouseEvent):void
    { 
        super.toggleClick(event);

        // do magic for this button
    }

    // ETC
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!