【问题标题】:Adding additional time to main timer from movieclip?从movieclip向主计时器添加额外的时间?
【发布时间】:2015-01-15 14:07:20
【问题描述】:

你好,是的,在主时间线上我有计时器

 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);
}

当您进入影片剪辑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);
}

【问题讨论】:

  • 你有什么问题? currentCount 将始终相同,除非您重置计时器。您是否希望每次单击都为计时器增加 2 秒,然后显示还剩多少时间?如果是这样,那么您就非常接近了。 (只需几个小调整即可)

标签: actionscript-3 flash timer flash-cs6


【解决方案1】:

您最好使用外部计数器来计算时间,而不是将其填充到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 并更新记分板。

【讨论】:

    【解决方案2】:

    我认为您要做的是在点击处理程序中为计时器添加 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
    });
    

    【讨论】:

    • 谢谢!我在您发布的第一个代码时遇到了一些麻烦(关于string buuuut 您的奖励代码工作得非常好,非常感谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-03
    • 2013-02-13
    • 2021-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多