AS2 to AS3 time function
I use to do this on AS2 code.
this.pause3Seconds = function () {
trace(_global.something(_root.somthing));
clearInterval(myInt);
};
var myInt = setInterval(this, "pause3Seconds", 3000);
Now trying to worked out this into a class.as file got all type of migration errors and warning.
So here I'm. anybody know how this can be done within a class.as AS3 way file ?
I'm 开发者_运维百科not working from timelines. ( frames )
John
Your AS2 code doesn't actually pause the player (enterFrame
listeners and mouse/key listeners will be executed during that three seconds). It just makes sure that the method pause3Seconds
will be called three seconds later. You can achieve a similar functionality in AS3 using the Timer
class.
var timer:Timer = new Timer(3000, 1);
timer.addEventListener(TimerEvent.TIMER, onTimerTick);
function onTimerTick(e:TimerEvent = null):void
{
if(e)
{
trace("3 seconds completed");
Timer(e.target).removeEventListener(TimerEvent.TIMER, onTimerTick);
}
}
var startTime = getTimer();
while (true)
{
if (getTimer() - startTime >= sleepTime)
{
//do something
break;
}
}
http://www.kirupa.com/forum/showthread.php?t=232714 - secocular's post
@Allan: That will make your flash code to throw runtime error (Script is taking longer than expected). It's always a bad idea to sleep inside a function.
@jon: This is somehwat like 'your way' solution :)
import flash.utils.*;//for setInterval and clearInterval
public class YourClass {
private var myInt:uint;
public function YourClass():void { //YourClass is the name of your class
myInt = setInterval(pause3Seconds, 3000);
}
public function pause3Seconds() {
trace("Whatever you want after 3 seconds");
clearInterval(myInt);
}
}
-bhups
精彩评论