Flash:随机重复生成0-9个数字
基本上我需要快速变化的数字的“图像”。我正计划把一系列的数字排成一行,有点像矩阵(数字是如何反复变化的)。我希望它们能很快地一次又一次地生成数字0-9(有点像秒表上的毫秒),直到它们消失Flash:随机重复生成0-9个数字,flash,actionscript-3,Flash,Actionscript 3,基本上我需要快速变化的数字的“图像”。我正计划把一系列的数字排成一行,有点像矩阵(数字是如何反复变化的)。我希望它们能很快地一次又一次地生成数字0-9(有点像秒表上的毫秒),直到它们消失 我对flash还比较陌生,所以如果你们能帮我写一段代码,我将非常感激 如前所述,要得到一个介于0和9之间的随机数,Math.random是一种方法: var n:int = Math.floor(Math.Random()*10); 但是为了解决你的第二个问题,如何得到它,它每毫秒做一次 import fla
我对flash还比较陌生,所以如果你们能帮我写一段代码,我将非常感激 如前所述,要得到一个介于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);
如前所述,要获得介于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( n );
与
Math.floor( n );
而且要快得多。
我们可以通过将.5和n相加得到Math.round()
int( n + .5 );
和一个Math.ceil(),方法是将1添加到结果中
int( n ) + 1;
下面是要检查的循环:
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 );
}
var n:编号;
变量i:int;
风险价值总额:int=100000;
对于(i=0;i
这不应该跟踪任何东西:)只是一个简单的提示:将数字(Math.random()*10)转换成整数
int( n );
与
Math.floor( n );
而且要快得多。
我们可以通过将.5和n相加得到Math.round()
int( n + .5 );
和一个Math.ceil(),方法是将1添加到结果中
int( n ) + 1;
下面是要检查的循环:
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 );
}
var n:编号;
变量i:int;
风险价值总额:int=100000;
对于(i=0;i
这不应该跟踪任何东西:)为什么要使用
setInterval
而不是递归的Timer
?@Matt McDonald,setTimeout
/setInterval
比Timer
更轻,因为它是一种语言结构。如果你知道你在做什么,他们可以创造奇迹。为了帮助新手使用flash,我建议使用Timer
。好吧,我会用一个问题来回答这个问题?随机数的顺序有关系吗?如果不是的话,我会避开定时器类。“我还试图让他和其他人看这篇文章时更容易,这可能会被用来写javascript。”“安东尼·佩斯,为什么还要麻烦使用计时器呢?”?你可能应该在舞台上附加一个ENTER\u FRAME
事件,并让它作为你的更新周期。你不想使用ENTER\u FRAME事件,因为他希望它每1毫秒更新一次,你最多可以使用ENTER FRAME事件1000/121(因为我认为FPS的限制是121);然而,即便如此,如果swf打算做其他事情,比如在舞台上画画,你也不太可能做到这一点。你为什么要用setInterval
而不是递归Timer
?@Matt McDonald,setTimeout
setInterval比Timer
更轻,因为它是一种语言结构。如果你知道你在做什么,他们可以创造奇迹。为了帮助新手使用flash,我建议使用Timer
。好吧,我会用一个问题来回答这个问题?随机数的顺序有关系吗?如果不是的话,我会避开定时器类。“我还试图让他和其他人看这篇文章时更容易,这可能会被用来写javascript。”“安东尼·佩斯,为什么还要麻烦使用计时器呢?”?你可能应该在舞台上附加一个ENTER\u FRAME
事件,并让它作为你的更新周期。你不想使用ENTER\u FRAME事件,因为他希望它每1毫秒更新一次,你最多可以使用ENTER FRAME事件1000/121(因为我认为FPS的限制是121);然而,即便如此,如果swf还打算做其他事情,比如在舞台上画画,那么你也不太可能做到这一点。如何将其与实例名称一起使用?(实际数字)不确定我是否理解:var newName:String=instance.name+''+int(Math.random()*10.toString();返回一个字符串,该字符串由实例名称、可读性分隔符“”和数字组成。如果实例名为“abc”,它将给出字符串“abc_X”,其中X是数字。如何将其与实例名一起使用?(实际数字)不确定我是否理解:var newName:String=instance.name+''+int(Math.random()*10.toString();返回一个字符串,该字符串由实例名称、可读性分隔符“”和数字组成。如果您的实例被称为“abc”,它将给出字符串“abc_X”,其中X是数字。