简体   繁体   中英

How to decrement Timer variable - AS3

Well, I have two TIMER type variables in my code AS3, but there comes a certain part of my game, I have to decrement the value of them.

var tempo1:Timer = new Timer(4000);
var tParada:Timer = new Timer(2000, 1);

I wonder how can I do to go decrementing these values, starting from an external class ...

Thank U.

Just decriment the delay every time the timer fires.

var tempo1:Timer = new Timer(4000);
tempo1.addEventListener(TimerEvent.TIMER, tick);
var minValue:int = 1000;

tempo1.start();

function tick(e:TimerEvent):void {
    if(tempo1.delay - 100 >= minValue){
        tempo1.delay -= 100;
    }
}

Or, if wanted it smoother, you could do something like this:

import flash.events.TimerEvent;
import flash.utils.Timer;
import fl.transitions.Tween;
import fl.transitions.easing.*;

var tempo1:Timer = new Timer(33); //30 times a seconds or so
    tempo1.addEventListener(TimerEvent.TIMER, tick);
    var curTickTime:int = 4000;

    tempo1.start();

    function tick(e:TimerEvent):void {
        if(tempo1.delay * tempo1.currentCount >= curTickTime){
            trace("tick"); //this should effectively be a tick
            tempo1.reset();
            tempo1.start();

            //do whatever you do on a tick
        }
    }

//tween the tick delay from the starting value to 100ms over a period of 5 seconds
var tween:Tween = new Tween(this, "curTickTime", Strong.easeOut, curTickTime, 100, 5, true);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM