Actionscript 3 从movieclip向主计时器添加额外时间?
嗨,是的,在主时间线上我有计时器Actionscript 3 从movieclip向主计时器添加额外时间?,actionscript-3,flash,timer,flash-cs6,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); }
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);
}
当你进入movieclipreimoi_mc
并点击useplush
按钮时,我希望能够在计时器上增加额外的秒数。以下是reimoi_mc
剪辑中的代码,但是的,我真的不知道如何使这项工作,请帮助;0; (我必须使用MovieClip(root)
从MovieClip内的主时间线访问正在运行的计时器)
你最好使用一个外部计数器来计算时间,而不是把它塞进一个
计时器
对象中。然后需要计时器来测量延迟,需要侦听器来计算延迟
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
}
}
var myTimer:Timer=new Timer(1000);//没有第二个参数
公共变量secondsLeft:int=300;//前“伯爵”
myTimer.addEventListener(TimerEvent.TIMER,sayHello);
函数sayHello(e:TimerEvent):void{
第二步--;
跟踪(“剩余秒数:”,秒数英尺);
如果(secondsLeft我认为您要做的是在单击处理程序中向计时器添加2秒,然后显示剩余时间?如果是,只需进行几次调整即可:
function sayHello(e:TimerEvent):void {
trace("Time Left: " + myTimer.repeatCount - myTimer.currentCount); //time left is the repeat count - the current count
}
奖金代码!
如果您希望使其不受计时器延迟的影响(例如,您希望其更新速度快于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
});
你所拥有的有什么问题吗?currentCount
将始终保持不变,除非你重置计时器。你是否希望每次单击都在计时器上增加2秒,然后显示剩余时间?如果是这样,你就非常接近了。(只需稍微调整一下即可)谢谢!你发布的第一个代码让我有些麻烦(关于string
buu您的奖金代码工作得非常好,非常感谢
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
});