flash动画制作不重复的随机函数技巧
在0到99的数排列中不重复。
flash动画制作过程:
建两个图层,各放一图片,其中上层的图片定义实例名: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<n; i++) {
my_array[i] = i;
}
for (var i = 0; i<n; i++) {
var tmp1 = random(n);
var tmp2 = my_array[i];
my_array[i] = my_array[tmp1];
my_array[tmp1] = tmp2;
}
}
flash动画制作方法一:
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<num_arr.length; 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);
flash动画制作方法二
oldtime=getTimer();
my_array=new Array();
n=100;
for(var i=0;i<n;i++){
my_array[i]=i;
}
for(var i=0;i<n;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);//程序运行时间
flash动画制作方法三:
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);