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!
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!