PDA

View Full Version : Random Image script... Need to add Links


keith1995
10-12-2005, 03:23 PM
I found the below script that create an array of images and then displays them randomly, fading them into each other. This works perfectly but I'd like to make each random image a link to a specific page (each image will link to a different URL).

Could someone examine the code below and let me know what needs to be added to get this to work?

Code in "Prototype" layer:
Array.prototype.distinctShuffle = function()
{
result = [];
for (posArray = [], i = 0; i < this.length; posArray[i] = i++)
{
}
for (last = this.length - 1; last >= 0; last--)
{
selected = this[last];
rand = random(posArray.length - 1);
lastPos = posArray.getPos(last);
if (lastPos == null)
{
result[posArray[rand]] = selected;
posArray.splice(rand, 1);
}
else
{
posArray.splice(lastPos, 1);
result[posArray[rand]] = selected;
posArray.splice(rand, 1);
posArray.push(last);
}
}
return result;
};

Array.prototype.getPos = function(image)
{
for (i = 0; i < this.length; ++i)
{
if (this[i] == image)
{
return i;
}
}
return null;
};

Code in "Action" layer:
_global.image_arr = new Array("image1.jpg", "image2.jpg", "image3.jpg", "image4.jpg");
_global.temp_arr = _global.image_arr.distinctShuffle();

trace("original array : " + _global.image_arr);
trace("shuffled array : " + _global.temp_arr);

_global.count = 0;

this.createEmptyMovieClip("holder0_mc", 1000);
this.holder0_mc._x = 0;
this.holder0_mc._y = 0;

this.createEmptyMovieClip("holder1_mc", 1100);
this.holder1_mc._x = 0;
this.holder1_mc._y = 0;

loadImage(this);

function loadImage(loc)
{
this = loc;
_global.count++;
clearInterval(rotate_iv);

if (_global.temp_arr.length < 1)
{
trace("We are at the end of the temp_arr...");
trace("We need to add new items into the array again...");
_global.temp_arr = _global.image_arr.distinctShuffle();
}

selectedItem = _global.temp_arr.shift();
trace(selectedItem + " " + _global.temp_arr.length);

if (_global.count % 2 == 1)
{
toLoad = 1;
toUnload = 0;
}
else if (_global.count % 2 == 0)
{
toLoad = 0;
toUnload = 1;
}
// trace("toLoad = " + toLoad + " toUnload = " + toUnload + " this = " + this);

this["holder" + toLoad + "_mc"]._x = 0;
this["holder" + toLoad + "_mc"]._alpha = 0;
this["holder" + toLoad + "_mc"].loadMovie(selectedItem);

this["holder" + toUnLoad + "_mc"].onEnterFrame = function ()
{
if (this._parent["holder" + toLoad + "_mc"]._width > 1)
{
this._parent["holder" + toUnLoad + "_mc"].swapDepth(9999);
this._parent["holder" + toLoad + "_mc"].swapDelth(8888);
this._parent["holder" + toLoad + "_mc"]._x = 0;
if (this._parent["holder" + toLoad + "_mc"]._alpha < 100)
{
this._parent["holder" + toUnLoad + "_mc"]._alpha -= 5;
this._parent["holder" + toLoad + "_mc"]._alpha += 5;
}
else
{
this._parent["holder" + toLoad + "_mc"]._alpha = 100;
delete this.onEnterFrame;
rotate_iv = setInterval(loadImage, 5000, loc);
}
}
}
}

If there is an easier way to do this, by all means point it out to me.

Thanks!

keith1995
10-14-2005, 06:52 PM
anyone got any ideas here?

CyanBlue
10-14-2005, 07:03 PM
How about the link to the thread where you've got that script??? I think there's file attached in that thread... That's got to be whole lot easier to play with than just the source script...

keith1995
10-18-2005, 04:09 AM
CyanBlue,

Unfortunately I can't find where I originally found the code from above. I can send you the .FLA file with this code in it if you have time to play with it?

Let me know