I'm looking to display three images, chosen at random on load, throughout the duration of a banner ad. Basically, these images are health tips displayed on post it's - as it plays, the post-it's fly off, revealing the next tip. I also need ensure that the images don't repeat. I'm mostly a timeline guy, but I've been learning AS and have no problem getting one image to display randomly - however the three separate ones with no repeat is throwing me off. My thoughts are to are to either a) Put all the images on separate frame within 3 MC's, and somehow display a random frame within that clip on load, ensuring no repeat or b) Load the images into 3 separate MC's through and XML doc, and pick one at random. Any thoughts or points in the right direction would be much appreciated. EDIT - I should've clarified... The images only appear one at a time - so I 开发者_开发问答think each MC would only contain one of the 3 random images selected. So is it possible to load a random image into a MC, and then duplicate that MC without a chance of the image repeating?
Well, one way of doing this following your thoughts is: Put the images in one mc. Create an array containing the frame number corresponding to each image. Shuffle that array to have a random order. Play image frame using the frame from the shuffled array.
To shuffle the array you could use something like this:
function shuffleArray(a:Array):Array {
var copy:Array = a.concat()
var length:int = copy.length;
var result:Array = new Array(length);
for(var i:int = 0; i<length; i++)
{
result[i] = copy.splice(int(Math.random() * (length - i)), 1)[0];
}
return result;
}
to use the method, simply:
var imgFrames:Array = [1,2,3];
trace( shuffleArray(imgFrames) )
trace( shuffleArray(imgFrames) )
trace( shuffleArray(imgFrames) )
精彩评论