从movieclip添加主计时器的额外时间?

时间:2014-11-18 11:38:15

标签: actionscript-3 flash timer flash-cs6

嗨,所以是的,在主时间轴上我有计时器

 var count:Number = 300;//Count down from 300
 var myTimer:Timer = new Timer(1000,count);
myTimer.addEventListener(TimerEvent.TIMER, sayHello);
function sayHello(e:TimerEvent):void
{
trace("Current Count: " + myTimer.currentCount);
}

当你进入movieclip reimoi_mc并单击useplush按钮时,我希望能够在计时器上添加额外的秒数。以下是reimoi_mc剪辑中的代码,但是我真的不知道如何使这项工作,请帮助; 0; (我必须使用MovieClip(root)从动画片段中的主时间轴访问正在运行的计时器)

import flash.events.MouseEvent;
import flash.utils.Timer;
import flash.utils.getTimer;

stop();
useplush.addEventListener(MouseEvent.CLICK, addtime);
function addtime(e:MouseEvent):void
{
            MovieClip(root).count += 2;
            MovieClip(root).myTimer.repeatCount += MovieClip(root).count; //add time to the timer

            trace("new time " + myTimer.currentCount);
}

2 个答案:

答案 0 :(得分:0)

您最好使用外部计数器来计算时间,而不是将其填充到Timer对象中。然后,您需要计时器来测量延迟,并且需要听众来计算它们。

var myTimer:Timer=new Timer(1000); // no second parameter
public var secondsLeft:int=300; // former "count"
myTimer.addEventListener(TimerEvent.TIMER, sayHello);
function sayHello(e:TimerEvent):void {
    secondsLeft--;
    trace("Seconds left:", secondsLeft);
    if (secondsLeft<=0) {
        myTimer.stop();
        myTimer.reset();
        // whatever else to trigger when time runs out
    }
}

然后您只需添加到secondsLeft并更新记分板。

答案 1 :(得分:0)

我认为你要做的是在点击处理程序中为计时器添加2秒,然后显示还剩多少时间?如果是这样,只需要进行一些调整:

function sayHello(e:TimerEvent):void {
    trace("Time Left: " + myTimer.repeatCount - myTimer.currentCount);  //time left is the repeat count - the current count
}

function addtime(e:MouseEvent):void {
    MovieClip(root).myTimer.repeatCount += 2 //add 2 more ticks to the timer (currentCount will always remain the same unless the timer is reset)
    trace("new time remaining: " + MovieClip(root).myTimer.repeatCount - MovieClip(root).myTimer.currentCount);
}

奖金代码!

如果您想让它与定时器延迟无关(假设您希望它更快地更新1秒),您可以这样做:

var startingTime:Number = 20; //the initial time in seconds
var myTimer:Timer = new Timer(200); //your timer and how often to have it tick (let's say 5 times a second)

myTimer.repeatCount = startingTime * Math.ceil(1000 / myTimer.delay); //set the initial repeat count
myTimer.addEventListener(TimerEvent.TIMER, sayHello);
myTimer.start();

function sayHello(e:Event):void {
     trace("Time Left: " + ((((myTimer.repeatCount - myTimer.currentCount) * myTimer.delay) / 1000)) + "seconds");
}

在你的另一个对象中:

stage.addEventListener(MouseEvent.CLICK, function(e:Event){
    myTimer.repeatCount += Math.ceil(2000 / myTimer.delay); //add 2000 milliseconds to the timer
});