 |
htrain26
Little Devil
Registered: Jan 2007
Local time: 10:47 AM
Location:
Posts: 12
|
| random images loading the same images... | Post #1 |
I posted a few weeks ago and received great help with a problem I had loading images randomly. here is the code I ended up with--
ActionScript:
pic_arr = ["images2/pic1", "images2/pic2", "images2/pic3", "images2/pic4", "images2/pic5", "images2/pic6", "images2/pic7", "images2/pic8"];
holder1_mc.onEnterFrame = function() {
var ranNum = Math.floor(Math.random()*pic_arr.length);
holder1_mc.loadMovie(pic_arr[ranNum]+".jpg");
};
holder2_mc.onEnterFrame = function() {
var ranNum = Math.floor(Math.random()*pic_arr.length);
holder2_mc.loadMovie(pic_arr[ranNum]+".jpg");
};
holder3_mc.onEnterFrame = function() {
var ranNum = Math.floor(Math.random()*pic_arr.length);
holder3_mc.loadMovie(pic_arr[ranNum]+".jpg");
};
|
The only problem is that with this code, the mc's can poteinailly load the same image in two or three of the mc's.
How can I code this to eliminate this issue. I tried if/else statement, it didn't work--chances are very high that I coded it wrong.
thanks,
hutch
*** edit: added as-tags. XemonerdX ***
Report this post to a moderator | IP: Logged
|
01-30-2007 05:31 PM |
|
|
|  |
 |
|  |
 |
soft_machine
Little Devil
Registered: May 2007
Local time: 04:47 PM
Location: london
Posts: 8
|
You need to shuffle your array ...
How is this implemented....does anyone know or have an example?
__________________
headlong into the past
Report this post to a moderator | IP: Logged
|
05-10-2007 10:56 AM |
|
|
|  |
 |
soft_machine
Little Devil
Registered: May 2007
Local time: 04:47 PM
Location: london
Posts: 8
|
Thanks.
I've tried the prototype script and discovered anomalies when trying to load a random set of 3 images.
So, I have 3 placeholders, abutton, and only 3 images.
With 3 images there are six possible 'random' arrays:
123
231
312
----
132
321
213
The 6 arrays comprise 2 unique sets Its only possibe to cycle through one set without repetition. Once you include an array from the other set you get repetition...a static image while 2 others change.
I'd appreciate any comments on this.
__________________
headlong into the past
Report this post to a moderator | IP: Logged
|
05-14-2007 09:58 AM |
|
|
|  |
 |
dankan775
Little Devil
Registered: May 2007
Local time: 04:47 PM
Location:
Posts: 1
|
Hi all,
my first post.....Hello World.....
I had the same problem and it did take a wile to figure out it was a stupid problem.....
first you are focusing on the problem to get the same picture in the same serie, is not that the problem it is that after one series you can gat one of the picture you just show.
so you have to first to sort the array problem
and it can be done putting all the picture in an array
then randomly take 4 picture and remove those from the array ( use splice )
ActionScript:
var myPicture:Array = new Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20);
var picMatrix:Array = new Array();
var i:Number = 0;
while (((myPicture.length)/4)>=1) {
var tempArray:Array = new Array();
for (var t:Number = 0; t<4; t++) {
var tempPushArray:Array = myPicture.splice(rand(myPicture.length-1), 1);
tempArray.push(tempPushArray[0]);
}
picMatrix.push(tempArray);
i++;
}
function rand(max:Number):Number {
var randomNum:Number = Math.floor(Math.random()*(max+1));
return (randomNum);
}
|
so now you get this ActionScript:
// --------------------------------------------------
picMatrix[0] = new Array(16,3,22,15);
picMatrix[1] = new Array(13,5,19,17);
picMatrix[2] = new Array(10,8,23,12);
picMatrix[3] = new Array(25,24,17,0);
picMatrix[4] = new Array(14,2,20,4);
picMatrix[5] = new Array(9,6,1,7);
picMatrix[6] = new Array(3,21,19,11);
picMatrix[7] = new Array(13,22,24,9);
picMatrix[8] = new Array(18,15,23,0);
picMatrix[9] = new Array(5,2,20,10);
// ------------------------------------------------------------
|
then you can randomly take a series from picMatrix,write samewhere this so then you can compare with the next seres if there is any of those picture that where in the old series in the new one if it true the pick another random series and make the control again till the moment the test pass
hope it was not to mess up
Bye
Report this post to a moderator | IP: Logged
|
05-14-2007 07:02 PM |
|
|
|  |
 |
soft_machine
Little Devil
Registered: May 2007
Local time: 04:47 PM
Location: london
Posts: 8
|
Thanks a lot!
That really helps me to understand the problem.
cool
__________________
headlong into the past
Report this post to a moderator | IP: Logged
|
05-15-2007 09:21 AM |
|
|
|  |
 |
soft_machine
Little Devil
Registered: May 2007
Local time: 04:47 PM
Location: london
Posts: 8
|
dankan775...I tried your code but had some problems.
Here is what I did (it's wrong!):
var myPicture:Array = new Array(0, 1, 2);
var picMatrix:Array = new Array();
var i:Number = 0;
while (((myPicture.length)/3)>=1) {
var tempArray:Array = new Array();
for (var t:Number = 0; t<3; t++) {
var tempPushArray:Array = myPicture.splice(rand(myPicture.length-1), 1);
tempArray.push(tempPushArray[0]);
}
picMatrix.push(tempArray);
i++;
}
function rand(max:Number):Number {
var randomNum:Number = Math.floor(Math.random()*(max+1));
return (randomNum);
}
one_btn.onRelease = function() {
var randomNum:Number = Math.floor(Math.random()*(max+1));
holder1_mc.loadMovie(myPicture[randomNum]+".jpg");
var randomNum:Number = Math.floor(Math.random()*(max+1));
holder2_mc.loadMovie(myPicture[randomNum]+".jpg");
var randomNum:Number = Math.floor(Math.random()*(max+1));
holder3_mc.loadMovie(myPicture[randomNum]+".jpg");
};
I'm getting "undefined.jpg" in the output.
Maybe you can see where i've gone wrong or if you have a .FLA that works I'd really like to see it.
Thanks again...
__________________
headlong into the past
Report this post to a moderator | IP: Logged
|
05-15-2007 12:25 PM |
|
|
|  |
 |
dankan775b
Little Devil
Registered: May 2007
Local time: 04:47 PM
Location:
Posts: 23
|
Hi,
well i can understand that you wish to have 3 pictures in different position in each cicle
if it is true this code is not for you but you can reuse couple of function
try this
ActionScript:
var myPicture:Array = new Array(0, 1, 2);
var picMatrix:Array = new Array();
var i:Number = 0;
var tempArray:Array = new Array();
for (var t:Number = 0; t<3; t++) {
var tempPushArray:Array = myPicture.splice(rand(myPicture.length-1), 1);
tempArray.push(tempPushArray[0]);
}
function rand(max:Number):Number {
var randomNum:Number = Math.floor(Math.random()*(max+1));
return (randomNum);
}
one_btn.onRelease = function() {
holder1_mc.loadMovie(tempArray.splice(rand(myPicture.length-1), 1)+".jpg");
holder2_mc.loadMovie(tempArray.splice(rand(myPicture.length-1), 1)+".jpg");
holder3_mc.loadMovie(tempArray.splice(rand(myPicture.length-1), 1)+".jpg");
};
|
Report this post to a moderator | IP: Logged
|
05-16-2007 02:17 PM |
|
|
|  |
 |
soft_machine
Little Devil
Registered: May 2007
Local time: 04:47 PM
Location: london
Posts: 8
|
Thanks a lot dude.
I tried this code out out.
The first set of 3 random images load but then nothing.
I put my test online here:
http://www.betabureau.com/test/array
This is the output:
Error opening URL "http://www.betabureau.com/test/array/.jpg"
Hmmmmm...any ideas?
__________________
headlong into the past
Report this post to a moderator | IP: Logged
|
05-16-2007 03:44 PM |
|
|
|  |
 |
dankan775b
Little Devil
Registered: May 2007
Local time: 04:47 PM
Location:
Posts: 23
|
ActionScript:
Array.prototype.matchTo = function(newArray:Array) {
if (newArray.length != this.length) {
return false;
}
var b:Number = (this.length-1);
for (var a:Number = 0; a<this.length; a++) {
if (this[a] == newArray[a]) {
b--;
}
}
if (b>0) {
return false;
} else {
return true;
}
};
Array.prototype.copyTo = function(destArray:Array) {
for (var a:Number = 0; a<this.length; a++) {
destArray[a] = this[a];
}
};
var current:Array = new Array();
function loadPic() {
var testPass:Boolean;
do {
testPass = true;
var myPicture:Array = new Array(0, 1, 2);
var picMatrix:Array = new Array();
var i:Number = 0;
var tempArray:Array = new Array();
for (var t:Number = 0; t<3; t++) {
var tempPushArray:Array = myPicture.splice(rand(myPicture.length-1), 1);
tempArray.push(tempPushArray[0]);
}
if (tempArray.matchTo(_root.current)) {
trace(" the array match ");
trace(current+" --> "+tempArray);
trace("--------------------------");
testPass = false;
}
} while (!testPass);
trace(current+" --> "+tempArray);
tempArray.copyTo(_root.current);
holder1_mc.loadMovie(tempArray[0]+".jpg");
holder2_mc.loadMovie(tempArray[1]+".jpg");
holder3_mc.loadMovie(tempArray[2]+".jpg");
}
function rand(max:Number):Number {
var randomNum:Number = Math.floor(Math.random()*(max+1));
return (randomNum);
}
one_btn.onRelease = function() {
loadPic();
};
|
it can be improved, it is just an idea of ....
Last edited by dankan775b on 05-16-2007 at 05:39 PM
Report this post to a moderator | IP: Logged
|
05-16-2007 05:33 PM |
|
|
|  |
 |
soft_machine
Little Devil
Registered: May 2007
Local time: 04:47 PM
Location: london
Posts: 8
|
Thank you very much.
Still get repetition, static images, but it's cool. I can work with this.

__________________
headlong into the past
Report this post to a moderator | IP: Logged
|
05-17-2007 02:06 PM |
|
|
|  |
 |
|  |
 |
soft_machine
Little Devil
Registered: May 2007
Local time: 04:47 PM
Location: london
Posts: 8
|
This is an interesting problem.
I have searched high and low for a workable solution but to no avail. I have yet to see a working solution to this problem anywhere, I've come across scripts that claim to randomise arrays and return unique values but in practice there always seems to be repeating sequences, especially when the array references only 3 images.
I challenge any ActionScripter to come up with a totally bulletproof, rock solid working model:
123
231
312
===
132
321
213
That's what you got;;;; 
__________________
headlong into the past
Report this post to a moderator | IP: Logged
|
06-01-2007 11:31 AM |
|
|
|  |
 |
dankan775b
Little Devil
Registered: May 2007
Local time: 04:47 PM
Location:
Posts: 23
|
check here it is javascript but you can use the logic and use it in actionscript, i'm using it at the moment in php...
sorry i did't do it....
but you got your answer solved
Report this post to a moderator | IP: Logged
|
06-01-2007 11:42 AM |
|
|
|  |
 |
| All times are GMT. The time now is 04:47 PM. |
 |
|
 |
|
|
|  |
Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
|
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is ON
|
|
|
|
|
< FlashDevils - Terms of use
>
Copyright, FlashDevils Community, 2002 - Flash Archive
|
|