基本上我需要快速变化数字的“图像”。我正在计划将这些系列中的一系列,像矩阵(数字如何反复变化)。我希望他们基本上以相当快的速度生成0-9的数字(有点像秒表上的毫秒),直到我将它们淡出为止。Flash:随机生成0-9重复的数字
我是相当新的闪光,所以如果你们可以帮我一个代码,我将不胜感激!
基本上我需要快速变化数字的“图像”。我正在计划将这些系列中的一系列,像矩阵(数字如何反复变化)。我希望他们基本上以相当快的速度生成0-9的数字(有点像秒表上的毫秒),直到我将它们淡出为止。Flash:随机生成0-9重复的数字
我是相当新的闪光,所以如果你们可以帮我一个代码,我将不胜感激!
如前所述得到0到9之间的随机数,是的Math.random要走的路:
var n:int = Math.floor(Math.Random()*10);
但要解决第二个问题是如何得到它,所以它这样做的每毫秒
import flash.utils.setInterval;
import flash.utils.clearInterval;
//variable for the intervalID,
//and the variable that will be assigned the random number
var rnGenIID:uint, rn:int;
//function to update the rn variable
//to the newly generated random number
function updateRN():void{
rn = random0to9();
//as suggested, you could just use:
//rn = int(Math.random()*10);
//but I figured you might find having it as a function kind of useful,
//...
//the trace is here to show you the newly updated variable
trace(rn);
}
function random0to9():int{
//in AS3, when you type a function as an int or a uint,
//so instead of using:
//return Math.floor(Math.random()*10);
//or
//return int(Math.random()*10);
//we use:
return Math.random()*10;
}
//doing this assigns rnGenIID a number representing the interval's ID#
//and it set it up so that the function updateRN will be called every 1 ms
rnGenIID = setInterval(updateRN,1);
//to clear the interval
//clearInterval(rnGenIID);
只是一个快速提示:铸造号码(所述的Math.random()* 10)为一个int
int(n);
不一样
Math.floor(n);
,是方式更快。 我们可以通过添加0.5〜ň
int(n + .5);
和Math.ceil()加入1结果
int(n) + 1;
在这里得到一个Math.round()是一个循环来检查:
var n:Number;
var i:int;
var total:int = 100000;
for (i = 0; i < total; i++)
{
n = Math.random() * 10;
if (int(n) != Math.floor(n)) trace('error floor ', n);
if (int(n + .5) != Math.round(n)) trace('error round ', n);
if (int(n) + 1 != Math.ceil(n)) trace('error ceil ', n);
}
这一点,不应该跟踪什么:)
为什么你使用`setInterval`代替递归`Timer`? – 2011-01-31 23:03:43
@Matt McDonald,`setTimeout` /`setInterval`比`Timer`重量轻,因为它是一种语言结构。如果你知道你在做什么,他们可以创造奇迹。为了帮助新手使用闪光灯,我建议使用'计时器'。 – zzzzBov 2011-01-31 23:15:04