如何在 AS3 中创建可重用的切换按钮?

发布于 2024-09-14 22:44:26 字数 1238 浏览 2 评论 0原文

我正在尝试使下面的代码可重用。我的 Flash 项目中需要多个切换按钮。现在,下面的代码可以在一个按钮上运行。如果我继续创建更多按钮,并遵循下面的格式,我将需要为每个按钮创建单独的功能。

我想将可重用代码放在单独的 ActionScript 文件中,而不是放在 FLA 文件中。我试图将 rolloverToggle、rolloverToggle 和toggleClick 放入我正在制作的类中。

// ///////////////////////////////////////////////////////////////////////

// ------- Need to make this code reusable -------

// ///////////////////////////////////////////////////////////////////////

// code on Frame 1

toggleButton.addEventListener(MouseEvent.MOUSE_OVER, rolloverToggle);
toggleButton.addEventListener(MouseEvent.MOUSE_OUT, rolloutToggle);
toggleButton.addEventListener(MouseEvent.CLICK, toggleClick);
toggleButton.buttonState = "off";

// function rolloverToggle
function rolloverToggle(event:MouseEvent) {
    toggleButton.gotoAndStop(toggleButton.buttonState+" over");
}

// function rolloutToggle
function rolloutToggle(event:MouseEvent) {
    toggleButton.gotoAndStop(toggleButton.buttonState);
}

// function toggleClick
function toggleClick(event:MouseEvent) {
    if (toggleButton.buttonState == "on") {
            toggleButton.buttonState = "off";
            toggleButton.gotoAndStop(1);
        } else {
            toggleButton.buttonState = "on";    
        }
}

I'm trying to make the code below reusable. I need multiple toggle buttons in my flash project. Right now the code below works on one button. If I continue and create more buttons, and follow the format below, I would need to create separate functions for each button.

I would like to put the reusable code in a separate ActionScript file and not in the FLA file. I am trying to put the rolloverToggle, rolloverToggle, and toggleClick in a class that I'm making.

// ///////////////////////////////////////////////////////////////////////

// ------- Need to make this code reusable -------

// ///////////////////////////////////////////////////////////////////////

// code on Frame 1

toggleButton.addEventListener(MouseEvent.MOUSE_OVER, rolloverToggle);
toggleButton.addEventListener(MouseEvent.MOUSE_OUT, rolloutToggle);
toggleButton.addEventListener(MouseEvent.CLICK, toggleClick);
toggleButton.buttonState = "off";

// function rolloverToggle
function rolloverToggle(event:MouseEvent) {
    toggleButton.gotoAndStop(toggleButton.buttonState+" over");
}

// function rolloutToggle
function rolloutToggle(event:MouseEvent) {
    toggleButton.gotoAndStop(toggleButton.buttonState);
}

// function toggleClick
function toggleClick(event:MouseEvent) {
    if (toggleButton.buttonState == "on") {
            toggleButton.buttonState = "off";
            toggleButton.gotoAndStop(1);
        } else {
            toggleButton.buttonState = "on";    
        }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏尔 2024-09-21 22:44:26

这很简单。创建一个新的通用按钮类,并在其中添加所有事件侦听器。对于要创建的每个新按钮,只需扩展通用按钮并在事件侦听器中填写所需的代码。:

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";
        }
    }
}

现在只需扩展该类并添加您的功能即可。

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

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

        // do magic for this button
    }

    // ETC
}

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
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文