使用按钮增加和减少actionscript 3.0中的计时器

时间:2015-04-08 14:05:36

标签: actionscript-3 timer loader

我写了一个代码来添加两个按钮。它正确显示它们,但操作未正确执行。

public class butt extends Sprite {
    public function butt() {
        var delayGlobal:Number = 2000;
        var min1:Number =1000;
        var myTimer:Timer = new Timer(delayGlobal);
        myTimer.addEventListener(TimerEvent.TIMER,runMany);
        myTimer.start();
        // Button Event
        myButton1.addEventListener(MouseEvent.CLICK, myButton1Click);
        function myButton1Click(ev:MouseEvent):void {
            delayGlobal = delayGlobal- 1000;
            trace(delayGlobal);
        }
        myButton2.addEventListener(MouseEvent.CLICK, myButton2Click);
        function myButton2Click(ev:MouseEvent):void {
            delayGlobal = delayGlobal + 1000;
            trace(delayGlobal);
        }
        function runMany(e:TimerEvent):void {
            var loader:Loader=new Loader();
            var url:String= "http://google.com.example2";
            loader.load(new URLRequest(url));
            addChild(loader);
        }
    }  
}

显示计时器但不起作用

1 个答案:

答案 0 :(得分:1)

您的问题是您正在更新变量,但实际上根本没有更新计时器。

您需要明确告诉计时器更改延迟。 myTimer.delay = delayGlobal

以下是您的代码重新编写:

public class butt extends Sprite {
    private var min1:Number =1000; //assuming this is your minimum allowed delay?
    private var myTimer:Timer;

    public function butt() {
        myTimer = new Timer(2000);
        myTimer.addEventListener(TimerEvent.TIMER,runMany);
        myTimer.start();

        // Button Event
        myButton1.addEventListener(MouseEvent.CLICK, myButton1Click);
        myButton2.addEventListener(MouseEvent.CLICK, myButton2Click);
    }

    private function myButton1Click(ev:MouseEvent):void {
        myTimer.delay = Math.max(min1, myTimer.delay - 1000);  //This will assign whatever is bigger,  min1 or the timer delay less 1000 - ensuring that the timer doesn't drop below the value of min1
        trace(myTimer.delay);
    }

    private function myButton2Click(ev:MouseEvent):void {
        myTimer.delay = myTimer.delay + 1000;
        trace(myTimer.delay);
    }
    private function runMany(e:TimerEvent):void {
        var loader:Loader=new Loader();
        var url:String= "http://google.com.example2";
        loader.load(new URLRequest(url));
        addChild(loader);
    } 
}