这篇教程是向大家介绍产生不重复的随机数的三种方法,通过一个Flash遮照实例来讲述,可以让风0到99的数排列,并不重复。这三种方法适用于在做多个随机方块遮照图片时和在播放动画时随机播放音乐,一起来看看吧!
方法一:
复制代码
代码如下:oldtime=getTimer();
my_array=new Array();
n=100;
for(var i=0;i my_array[i]=i;
}
for(var i=0;i var tmp1=random(n);
var tmp2=my_array[i];
my_array[i]=my_array[tmp1];
my_array[tmp1]=tmp2;
}
trace(my_array);
trace(getTimer()-oldtime);//程序运行时间
//----------------------------------------------------------
my_array=new Array();
n=100;
for(var i=0;i my_array[i]=i;
}
for(var i=0;i var tmp1=random(n);
var tmp2=my_array[i];
my_array[i]=my_array[tmp1];
my_array[tmp1]=tmp2;
}
trace(my_array);
trace(getTimer()-oldtime);//程序运行时间
//----------------------------------------------------------
方法二
复制代码
代码如下:n=getTimer();
Array.prototype.randomize = function() {
this.sort(function(a, b) { return random(2)>0 ? 1 : -1;});
}
var myArray = new Array();
for(var i=0;i<1000;i++){
myArray[i]=i;
}
myArray.randomize();
trace(myArray);
trace(getTimer()-n);
Array.prototype.randomize = function() {
this.sort(function(a, b) { return random(2)>0 ? 1 : -1;});
}
var myArray = new Array();
for(var i=0;i<1000;i++){
myArray[i]=i;
}
myArray.randomize();
trace(myArray);
trace(getTimer()-n);
方法三:
复制代码
代码如下:var num_arr:Array = new Array();
var j = 0;
var judge = 0;
do {
var temp_num = Math.round(Math.random()*100);
for (var i = 0; i var temp_n = num_arr[i];
if (temp_num == temp_n) {
judge = 1;
break;
} else {
judge = 0;
}
}
if (judge == 0) {
num_arr.push(temp_num);
j++;
_root["t"+j].text = temp_num;
}
} while (j<100);
trace(num_arr);
var j = 0;
var judge = 0;
do {
var temp_num = Math.round(Math.random()*100);
for (var i = 0; i var temp_n = num_arr[i];
if (temp_num == temp_n) {
judge = 1;
break;
} else {
judge = 0;
}
}
if (judge == 0) {
num_arr.push(temp_num);
j++;
_root["t"+j].text = temp_num;
}
} while (j<100);
trace(num_arr);
制作过程: 建两层,各放一图片,其中上层的图片定义实例名:pic_mc
然后画一矩形,转化为MC,定义ID名为 box,然后在帧上写入AS:
复制代码
代码如下:creatmask();
//建立遮照
newnum();
//产生随机数
var i = -1;
var n = 0;
_root.onEnterFrame = function() {
i++;
if (i<300) {
eval("_root.m1_mc.box"+my_array[n]).removeMovieClip();
//移除方块
n++;
}
if (i>=400 && i<700) {
m = my_array[i-400];
_root.m1_mc.attachMovie("box", "box"+m, m+10, {_x:int(m/15)*20, _y:m%15*20});
//加载方块
}
if (i>800) {
i = 0;
n = 0;
}
};
//-------------------------------------
function creatMask() {
_root.createEmptyMovieClip("m1_mc", 1);
m1_mc._x = 0;
mc_mc._y = 0;
for (var i = 0; i<300; i++) {
_root.m1_mc.attachMovie("box", "box"+i, i+10, {_x:int(i/15)*20, _y:i%15*20});
}
pic_mc.setMask(m1_mc);
}
function newnum() {
my_array = new Array();
n = 300;
for (var i = 0; i my_array[i] = i;
}
for (var i = 0; i var tmp1 = random(n);
var tmp2 = my_array[i];
my_array[i] = my_array[tmp1];
my_array[tmp1] = tmp2;
}
}
//建立遮照
newnum();
//产生随机数
var i = -1;
var n = 0;
_root.onEnterFrame = function() {
i++;
if (i<300) {
eval("_root.m1_mc.box"+my_array[n]).removeMovieClip();
//移除方块
n++;
}
if (i>=400 && i<700) {
m = my_array[i-400];
_root.m1_mc.attachMovie("box", "box"+m, m+10, {_x:int(m/15)*20, _y:m%15*20});
//加载方块
}
if (i>800) {
i = 0;
n = 0;
}
};
//-------------------------------------
function creatMask() {
_root.createEmptyMovieClip("m1_mc", 1);
m1_mc._x = 0;
mc_mc._y = 0;
for (var i = 0; i<300; i++) {
_root.m1_mc.attachMovie("box", "box"+i, i+10, {_x:int(i/15)*20, _y:i%15*20});
}
pic_mc.setMask(m1_mc);
}
function newnum() {
my_array = new Array();
n = 300;
for (var i = 0; i my_array[i] = i;
}
for (var i = 0; i var tmp1 = random(n);
var tmp2 = my_array[i];
my_array[i] = my_array[tmp1];
my_array[tmp1] = tmp2;
}
}
在上面的基础上有所改进
制作方法与上有点差别,就是在box元件中的方块转化为MC,然后编辑这个MC,在上面做点运动变形.
复制代码
代码如下:newnum();
//产生随机数
_root.createEmptyMovieClip("m1_mc", 1);
m1_mc._x = 0;
mc_mc._y = 0;
pic_mc.setMask(m1_mc);
for (var i = 0; i<300; i++) {
_root.m1_mc.attachMovie("box", "box"+i, i+100, {_x:int(i/15)*20, _y:i%15*20});
}
var i = -1;
_root.onEnterFrame = function() {
i++;
if (i<300) {
m = my_array[i];
eval("_root.m1_mc.box"+my_array[m]+".p").play();
}
if (i>=400 && i<700) {
m = my_array[i-400];
eval("_root.m1_mc.box"+my_array[m]+".p").play();
}
if (i>800) {
i = -1;
}
};
//-------------------------------------
function newnum() {
my_array = new Array();
n = 300;
for (var i = 0; i my_array[i] = i;
}
for (var i = 0; i var tmp1 = random(n);
var tmp2 = my_array[i];
my_array[i] = my_array[tmp1];
my_array[tmp1] = tmp2;
}
}
//产生随机数
_root.createEmptyMovieClip("m1_mc", 1);
m1_mc._x = 0;
mc_mc._y = 0;
pic_mc.setMask(m1_mc);
for (var i = 0; i<300; i++) {
_root.m1_mc.attachMovie("box", "box"+i, i+100, {_x:int(i/15)*20, _y:i%15*20});
}
var i = -1;
_root.onEnterFrame = function() {
i++;
if (i<300) {
m = my_array[i];
eval("_root.m1_mc.box"+my_array[m]+".p").play();
}
if (i>=400 && i<700) {
m = my_array[i-400];
eval("_root.m1_mc.box"+my_array[m]+".p").play();
}
if (i>800) {
i = -1;
}
};
//-------------------------------------
function newnum() {
my_array = new Array();
n = 300;
for (var i = 0; i my_array[i] = i;
}
for (var i = 0; i var tmp1 = random(n);
var tmp2 = my_array[i];
my_array[i] = my_array[tmp1];
my_array[tmp1] = tmp2;
}
}
教程结束,以上就是产生不重复的随机数的三种方法介绍,希望对大家有所帮助!