Learn AS3 rookie take off.-Interval cycle
If you want the animation to be independent of the frame rate, you can use 3 methods.
1, Setlnterval () function
The function of the Setlnterval () function is to call functions at a certain time
The Setlnterval () function is in the form of:
Setlnterval (functions name, time interval, function parameter);
Function name is the name of the custom function, the time interval is milliseconds, and the parameter is a parameter of the custom function.
Let's make a timer:
//时间的初始值
var t:int;
//定义一个无参函数
function time()
{
//输出时间
trace(++t / 10);
}
//每隔0.1秒调用一次函数
setInterval(time,100);
Test the above code to see the time increment in 0.1 seconds in the output panel.
Tip: the Setlnterval () function calls a function every 0.1 seconds, and the variable t is incremented by 1, and t divided by 10 to indicate the time.
The following code calls a parameter function:
//定义一个有参函数
function test(message)
{
trace(message);
}
//每隔500毫秒调用一次函数,输出“调用函数”
setInterval(test,500,"调用函数");
Clears the call to SetInterval (), using the Clearinterval () function
Hint: The call to clear the setinterval () function in time will result in a duplicate call to the SetInterval () function.
2, settimeout () function
The settimeout () function is used in the same way as the Setlnterval () function, and it is called at a certain time.
The general form of the settimeout () function is:
settimeout (functions name, time interval, function parameter);
Call a parameterless function:
//自定义函数,输出信息
function test()
{
trace("函数调用");
}
//每隔1000毫秒调用一次test(),输出信息。
setTimeout(test,1000);
Clears the call to SetTimeout () and uses the Cleartimeout () function.