Reputation: 536
Hey everyone I have seen some forums about this but can't seem to figure it out still.
So I have an Array called aClockArray
and this is a custom array which has 4 Movie clips inside of it. The nested Movie clips have a different color for each frame. I have the array set up like so in my constructor:
aClockArray = [playScreen.wire_5, playScreen.wire_6, playScreen.wire_7, playScreen.wire_8];
Then in another Function I have a for loop setup to iterate through all objects in array and have them gotoAndStop on a random Frame in their Nested Movie clip it goes from 2-7 that I have it randomize like so:
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);
}
}
Now this works perfect and I get random colors every time I restart. But What I would like to accomplish is for the colors not to repeat so not to have the nWire = randomNumber(2, 7);
2-7 numbers repeat. How would I go about doing this to have these numbers random generate and not repeat?
Also Here is my randomNumber
function:
//Generates a truly "random" number
function randomNumber(low:Number=0, high:Number=1):Number
{
return Math.floor(Math.random() * (1+high-low)) + low;
}
Thank you any help would be appreciated!
Tried something like this but still has duplicates :(
//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);
}
Upvotes: 1
Views: 1009
Reputation: 2223
This is an ideal candidate for prototyping:
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);
}
Thx to this prototype you can now call this method on any mocieclip and have them display a unique frame of their timeline until they have displayed them all and they start over.
mymovie.gotoRandomFrame();
The other answers are correct but they don't account for the multiple movieclip situation. It would be bad to have to create as many pieces of code and arrays as you have movieclips.
Upvotes: 1
Reputation: 1997
I would suggest something like:
For example:
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);
}
Upvotes: 3
Reputation: 269
I'm not going to give you the exact code, since I don't write in actionscript. But, you just need to save the last color you used and avoid it:
{next_color = randomNumber(2,7)} until next_color != last_color;
currentWires.gotoAndStop(next_color);
last_color = next_color;
Viola! Pseudo random colors.
Upvotes: -3