2014-09-05 190 views
0

我对AS3非常陌生,现在我试图用汽车指示器按下按钮(使用形状工具在fla中制作)闪烁3次。我已将它转换为符号,并且正在尝试对类自身进行编程(我不确定是否要编码符号类或直接连接到fla的主类)。现在我有3级制造2个三角形(Rblinker和Lblinker)和“MAIN”。如何让鼠标点击时按钮闪烁/闪烁?

最大的问题atm是我似乎无法让按钮在鼠标单击时闪烁,任何身体都可以帮助吗?谢谢

现在我的代码为“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; 
     } 
    } 
} 

这两个blinker将具有相同的代码。也想只使用AC3语言只有

+0

初始化单击处理程序 – 2014-09-05 19:23:05

+0

好了,所以我删除了公共变种,并把他们在'公共功能Rblinker计时器()' 。 'this.addEventListener(MouseEvent.click,clickaction);'收到错误1119,我不明白这个错误。 – 2014-09-05 19:29:09

+0

在一个事件处理程序中,范围是不同的,所以'this'的意思是别的。对你来说最简单的解决方案不是在构造函数中嵌套处理程序。 – 2014-09-05 19:32:05

回答

0

试试这个:

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; 
    } 
} 
+0

感谢您的回答,但是在'this.addEventListener(MouseEvent.click,clickaction);有一个错误1119静态引用;'好像我不能将事件侦听器放在类本身中,或者我错了吗? @Marton_Pallagi在上面评论说,监听器不应该嵌套在构造函数中。 – 2014-09-06 07:46:33

+0

你的问题是“点击”应该全部大写。我只是复制并粘贴你的问题,直到现在才注意到。 – BadFeelingAboutThis 2014-09-06 19:04:31

+0

@NUBatcodes - 你有工作吗? – BadFeelingAboutThis 2014-09-12 20:01:29