【问题标题】:How to make a button blink/flash on mouse click?如何在鼠标单击时使按钮闪烁/闪烁?
【发布时间】:2014-09-05 19:19:59
【问题描述】:

我对 AS3 很陌生,现在我正在尝试制作一个按钮(使用形状工具在 fla 中制作)在按下时像汽车指示灯一样闪烁 3 次。我已将其转换为符号,目前正在尝试对类本身进行编程(我不确定我是要编写符号类还是直接连接到 fla 的主类)。现在我有 3 类 2 个三角形(Rblinker 和 Lblinker)和“MAIN”。

最大的问题是我似乎无法让按钮在鼠标点击时闪烁,任何人都可以帮忙吗?谢谢你

现在我的“Rblinker”代码如下所示。

package  {

import flash.display.MovieClip;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.events.MouseEvent

public class Rblinker extends MovieClip {

    public var timer:Timer =  new Timer(1000,3);
    public var blink:Boolean = true;
    timer.start();


    public function Rblinker() {
        this.addEventListener(MouseEvent.click, clickaction);


        function clickaction(e:MouseEvent):void{
            timer.addEventListener(TimerEvent.TIMER, timerAction);
            this.alpha = 1;
    }


    function timerAction(e:TimerEvent):void
        {
            if (!blink){
                this.alpha = 1;
                } 
            else{
                this.alpha = 0;
                }

        blink = !blink;
        }
    }
}

两个闪光灯将具有相同的代码。也想只使用 AC3 语言

【问题讨论】:

  • 在点击处理程序中初始化计时器
  • 好的,所以我删除了公共变量并将它们放在public function Rblinker() 中。 this.addEventListener(MouseEvent.click, clickaction);收到错误1119,我不明白这个错误。
  • 在事件处理程序中,作用域是不同的,所以this 意味着别的东西。最简单的解决方案是不要将处理程序嵌套在构造函数中。

标签: actionscript-3 flash button


【解决方案1】:

试试这个:

public class Rblinker extends MovieClip {

    public var timer:Timer =  new Timer(1000,3);//tick every second, 3 times total


    public function Rblinker() {
        this.addEventListener(MouseEvent.CLICK, clickaction);

        //add listeners in the constructor
        timer.addEventListener(TimerEvent.TIMER, timerAction);
        timer.addEventListener(TimerEvent.TIMER_COMPLETE, timerComplete);
    }

    function clickaction(e:Event):void {
        //on the click, hide it to start, then start the timer
        this.visible = false;
        timer.reset(); //reset first so it always goes 3 times even if it's clicked again before finishing
        timer.start();
    }


    function timerAction(e:TimerEvent):void
    {
       //toggle the visibility of this sprite every timer tick
       this.visible = !this.visible;

       //or, if you want to keep using alpha (so the button can still be clicked when not visible)
       this.alpha = alpha > 0 ? 0 : 1;   //this is an inline if statment
    }

    function timerComplete(e:TimerEvent):void {
        //now that the timer is totally done, make sure the item is visible again
        this.visible = true;
    }
}

【讨论】:

  • 感谢您的回答,但是this.addEventListener(MouseEvent.click, clickaction); 处有一个错误 1119 静态引用似乎我不能将事件侦听器放在类本身中还是我错了? @Marton_Pallagi 在上面评论说监听器不应该嵌套在构造函数中。
  • 您的问题是“点击”应该全部大写。我只是从您的问题中复制并粘贴,直到现在才注意到。
猜你喜欢
  • 1970-01-01
  • 2018-01-30
  • 2018-05-26
  • 1970-01-01
  • 1970-01-01
  • 2014-10-21
  • 2015-05-15
  • 2018-04-23
  • 1970-01-01
相关资源
最近更新 更多