嘿,大家,我看过一些论坛,但似乎还没有弄清楚。
所以我有一个叫做aClockArray的数组,这是一个自定义数组,里面有4个电影剪辑。嵌套的电影剪辑对每个帧都有不同的颜色。在构造函数中,数组的设置类似于这样:
aClockArray = [playScreen.wire_5, playScreen.wire_6, playScreen.wire_7, playScreen.wire_8];然后,在另一个函数中,我有一个for循环设置来迭代数组中的所有对象,并让它们在嵌套的电影剪辑中的一个随机帧上进行gotoAndStop,它从2-7开始随机化,如下所示:
private function randomColorGenerator():void
{
//Loop through wires and make them randomn generate color
for (var i:int = 0; i < aClockArray.length; i++)
{
var currentWires = aClockArray[i];
nWire = randomNumber(2, 7);
currentWires.gotoAndStop(nWire);
}
}现在,这是完美的工作,我得到随机颜色,每次我重新启动。但是我想要完成的是,颜色不要重复,所以不要让nWire = randomNumber(2, 7); 2-7数字重复。我要怎么做才能让这些数字随机生成而不重复呢?
这里还有我的randomNumber函数:
//Generates a truly "random" number
function randomNumber(low:Number=0, high:Number=1):Number
{
return Math.floor(Math.random() * (1+high-low)) + low;
}感谢您的任何帮助,将不胜感激!
尝试过这样的方法,但仍然有重复的:
//Loop through wires and make them randomn generate color
for (var i:int = 0; i < aClockArray.length; i++)
{
var currentWires = aClockArray[i];
var frames:Array = [2, 3, 4, 5, 6, 7, 8];
var randomFrame:uint = frames.splice(Math.floor(Math.random() * frames.length), 1);
currentWires.gotoAndStop(randomFrame);
}发布于 2015-09-21 17:54:38
创建一个唯一可能的结果数组:
var frames:Array = [2, 3, 4, 5, 6, 7];然后用splice随机索引从该数组中删除
var randomFrame:uint = frames.splice(Math.floor(Math.random() * frames.length), 1);发布于 2015-09-21 18:31:51
我建议如下:
例如:
import flash.display.MovieClip;
function randomizeSort(obj1:Boolean, obj2:Object):int
{
var randNum:int = -1 + Math.floor((Math.random() * 3));
return randNum;
}
var framesList:Vector.<int> = Vector.<int>([2, 3, 4, 5, 6, 7])
framesList.sort(randomizeSort);
trace(framesList);
var tempColor:int;
var clipsList:Vector.<MovieClip> = Vector.<MovieClip>([playScreen.wire_5, playScreen.wire_6, playScreen.wire_7, playScreen.wire_8]);
var clipsCount:int = clipsList.length;
for (var clipIndex:int = 0; clipIndex < clipsCount; clipIndex++)
{
tempColor = framesList.shift();
trace(tempColor);
}发布于 2015-09-21 19:17:52
这是一个理想的原型开发候选:
MovieClip.prototype.gotoRandomFrame = function():void
{
if(!this.frameReferences)
{
this.frameReferences = [];
for(var i:int = 0; i < this.totalFrames; i++)
{
this.frameReferences.push(i + 1);
}
}
var index:int = this.frameReferences.splice(Math.floor(Math.random() * this.frameReferences.length), 1);
if(!this.frameReferences.length)
{
this.frameReferences = null;
}
this.gotoAndStop(index);
}对于这个原型,您现在可以在任何mocieclip上调用此方法,并让它们显示其时间线的唯一框架,直到它们全部显示并重新开始。
mymovie.gotoRandomFrame();其他的答案是正确的,但他们没有考虑到多重电影的情况。如果必须创建与movieclips一样多的代码和数组,那就太糟糕了。
https://stackoverflow.com/questions/32701461
复制相似问题