PDA

View Full Version : AS3: Need help with random photo switcher.



FlashFox39
May 19th, 2009, 04:36 PM
Hi everyone...I'm still very much a newbie when it comes to AS3.

I need to create an external image scroller. That's simple enough, but these images are of people of different ethnics groups, roughly divided into four categories.

So, the pictures need to be randomized two-dimensionally. The first random factor is which of the four groups to choose from. The second random factor is which image in that group to display.

That way, no ethnic group is favored over another.

I've gotten fairly far...when I test my Flash file and click the Start button, you'll see the first random image. Wait a few seconds, and the second will appear. All well and good...but the logic for how to randomize and automatically display all 15 images eludes me, despite days of research.

Can anyone point me in the right direction? I tried to upload the test files but they're too big, so the code is below.

Thanks!

import flash.utils.*;

/* An array of four different groups of photos */


var myPhotos:Array = new Array();

myPhotos[0] = ["01.jpg","02.jpg","03.jpg","04.jpg","05.jpg"];

myPhotos[1] = ["06.jpg","07.jpg","08.jpg","09.jpg"];

myPhotos[2] = ["10.jpg","11.jpg","12.jpg"];

myPhotos[3] = ["13.jpg","14.jpg","15.jpg"];


/* Four functions that generate a random index from each array */

var max1:Number = myPhotos[0].length;
var min1:Number = 0;
var randomPhoto0:Number = Math.round(Math.random() * (max1 - min1) + min1);

var max2:Number = myPhotos[1].length;
var min2:Number = 0;
var randomPhoto1:Number = Math.round(Math.random() * (max2 - min2) + min2);

var max3:Number = myPhotos[2].length;
var min3:Number = 0;
var randomPhoto2:Number = Math.round(Math.random() * (max3 - min3) + min3);

var max4:Number = myPhotos[3].length;
var min4:Number = 0;
var randomPhoto3:Number = Math.round(Math.random() * (max4 - min4) + min4);

// Here's where it gets dicey. I want a function that loads a random image from any array.

// The next image must also be random, AND from a different array group.

// And so on...the intent is maintain the diversity of the pictures, so that

// no ethnic group gets precedence over another.

function switchPhotos(event:MouseEvent):void
{
var req:URLRequest = new URLRequest(myPhotos[0][randomPhoto0]);
var loader:Loader = new Loader();
loader.load(req);
addChild(loader);
}

function photoTwo(event:TimerEvent):void
{
var req:URLRequest = new URLRequest(myPhotos[1][randomPhoto1]);
var loader:Loader = new Loader();
loader.load(req);
addChild(loader);
}

var myTimer:Timer = new Timer(5000);
myTimer.addEventListener("timer", photoTwo);
myTimer.start();

// This button is only for testing. The actual movie should play and rotate the images automagically.

start_btn.addEventListener(MouseEvent.CLICK, switchPhotos);

.ral:cr
May 19th, 2009, 06:01 PM
you could join the 4 arrays into one, then sort it randomly. now you'll be able to display the 15 images one by one in a random order.

FlashFox39
May 19th, 2009, 10:41 PM
I could, but then I really wouldn't need four discreet arrays. Plus, if I did that, theoretically, I could get a run of random images from the same group, which would defeat the purpose.

That's what's stumping me...I need to load a random image from a random group.

So, for example:

Group A, Images 1, 2, 3
Group B, Images 1, 2, 3
Group C, Images 1, 2, 3
Group D, Images 1, 2, 3

The first image shown would be Group A, Image 3 (for example)
Then it could be Group C, image 1, and so on.

I'm having trouble with that logic...

.ral:cr
May 20th, 2009, 12:58 PM
then, sort each array randomly, then create a new array with [0,1,2,3] and sort it randomly too. this array will indicate from which array you'll get the photo. so for example you get the first photo from array 0 and position 0, then array 1 position 0, array 2 pos 0, array 3 pos 0, array 0 pos 1, ....
but you'll still see some photos from the same group if you don't have the same nr of photos.

i'd use the first approach, but you know better.

FlashFox39
May 20th, 2009, 11:28 PM
That could work...thanks! I'll give it a try tomorrow.