Create an Account!

Adobe Flash Community Forum for all your Flash, Actionscript and Swift3D Support

Search:
FlashDevils Community
 Recommend Us
 About FlashDevils Community
 Can i become a moderator?
 How do i add Flash to my post?
 
Flash Community Register Flash Forum Control Panel Calendar Member List FAQ Search FLA Files
FlashDevils Community FlashDevils Community > Board > Flash > Flash ActionScript > random images loading the same images...


  Last Thread   Next Thread
Author
Thread Post New Thread    Post A Reply
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

Old Post 01-30-2007 05:31 PM
htrain26 is offline Click Here to See the Profile for htrain26 Click here to Send htrain26 a Private Message Click Here to Email htrain26 Visit htrain26's homepage! Find more posts by htrain26 Add htrain26 to your buddy list Edit/Delete Message Reply w/Quote
XemonerdX
veganXcodeXwarrior (StaffMember)

Registered: Apr 2002
Local time: 04:47 PM
Location: The Netherlands
Posts: 4818

Post #2

You need to shuffle your array and then take the first (or last) 3 elements. See here for some great ways. Then just use 'pic_arr[0]' for 'holder1_mc', 'pic_arr[1]' for 'holder2_mc' and 'pic_arr[2]' for 'holder3_mc'.

__________________
.:: PoeticTerror.Com ::.
.:: FlashDevils ::.
.:: FlashFocus ::.
.:: GalaxyGoo ::.
.:: OrganicFlash ::.



Report this post to a moderator | IP: Logged

Old Post 01-31-2007 08:22 AM
XemonerdX is offline Click Here to See the Profile for XemonerdX Click here to Send XemonerdX a Private Message Click Here to Email XemonerdX Visit XemonerdX's homepage! Find more posts by XemonerdX Add XemonerdX to your buddy list Edit/Delete Message Reply w/Quote
soft_machine
Little Devil

Registered: May 2007
Local time: 04:47 PM
Location: london
Posts: 8

Post #3

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

Old Post 05-10-2007 10:56 AM
soft_machine is offline Click Here to See the Profile for soft_machine Click here to Send soft_machine a Private Message Click Here to Email soft_machine Find more posts by soft_machine Add soft_machine to your buddy list Edit/Delete Message Reply w/Quote
XemonerdX
veganXcodeXwarrior (StaffMember)

Registered: Apr 2002
Local time: 04:47 PM
Location: The Netherlands
Posts: 4818

Post #4

The yellow 'here' in my reply is a link to exactly what yer asking.

__________________
.:: PoeticTerror.Com ::.
.:: FlashDevils ::.
.:: FlashFocus ::.
.:: GalaxyGoo ::.
.:: OrganicFlash ::.



Report this post to a moderator | IP: Logged

Old Post 05-11-2007 09:21 AM
XemonerdX is offline Click Here to See the Profile for XemonerdX Click here to Send XemonerdX a Private Message Click Here to Email XemonerdX Visit XemonerdX's homepage! Find more posts by XemonerdX Add XemonerdX to your buddy list Edit/Delete Message Reply w/Quote
soft_machine
Little Devil

Registered: May 2007
Local time: 04:47 PM
Location: london
Posts: 8

Post #5

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

Old Post 05-14-2007 09:58 AM
soft_machine is offline Click Here to See the Profile for soft_machine Click here to Send soft_machine a Private Message Click Here to Email soft_machine Find more posts by soft_machine Add soft_machine to your buddy list Edit/Delete Message Reply w/Quote
dankan775
Little Devil

Registered: May 2007
Local time: 04:47 PM
Location:
Posts: 1

Post #6

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

Old Post 05-14-2007 07:02 PM
dankan775 is offline Click Here to See the Profile for dankan775 Click here to Send dankan775 a Private Message Click Here to Email dankan775 Find more posts by dankan775 Add dankan775 to your buddy list Edit/Delete Message Reply w/Quote
soft_machine
Little Devil

Registered: May 2007
Local time: 04:47 PM
Location: london
Posts: 8

Random ImagesPost #7

Thanks a lot!
That really helps me to understand the problem.

cool

__________________
headlong into the past



Report this post to a moderator | IP: Logged

Old Post 05-15-2007 09:21 AM
soft_machine is offline Click Here to See the Profile for soft_machine Click here to Send soft_machine a Private Message Click Here to Email soft_machine Find more posts by soft_machine Add soft_machine to your buddy list Edit/Delete Message Reply w/Quote
soft_machine
Little Devil

Registered: May 2007
Local time: 04:47 PM
Location: london
Posts: 8

Post #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

Old Post 05-15-2007 12:25 PM
soft_machine is offline Click Here to See the Profile for soft_machine Click here to Send soft_machine a Private Message Click Here to Email soft_machine Find more posts by soft_machine Add soft_machine to your buddy list Edit/Delete Message Reply w/Quote
dankan775b
Little Devil

Registered: May 2007
Local time: 04:47 PM
Location:
Posts: 23

Post #9

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

Old Post 05-16-2007 02:17 PM
dankan775b is offline Click Here to See the Profile for dankan775b Click here to Send dankan775b a Private Message Find more posts by dankan775b Add dankan775b to your buddy list Edit/Delete Message Reply w/Quote
soft_machine
Little Devil

Registered: May 2007
Local time: 04:47 PM
Location: london
Posts: 8

Post #10

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

Old Post 05-16-2007 03:44 PM
soft_machine is offline Click Here to See the Profile for soft_machine Click here to Send soft_machine a Private Message Click Here to Email soft_machine Find more posts by soft_machine Add soft_machine to your buddy list Edit/Delete Message Reply w/Quote
dankan775b
Little Devil

Registered: May 2007
Local time: 04:47 PM
Location:
Posts: 23

Post #11

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

Old Post 05-16-2007 05:33 PM
dankan775b is offline Click Here to See the Profile for dankan775b Click here to Send dankan775b a Private Message Find more posts by dankan775b Add dankan775b to your buddy list Edit/Delete Message Reply w/Quote
soft_machine
Little Devil

Registered: May 2007
Local time: 04:47 PM
Location: london
Posts: 8

dankan775bPost #12

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

Old Post 05-17-2007 02:06 PM
soft_machine is offline Click Here to See the Profile for soft_machine Click here to Send soft_machine a Private Message Click Here to Email soft_machine Find more posts by soft_machine Add soft_machine to your buddy list Edit/Delete Message Reply w/Quote
XemonerdX
veganXcodeXwarrior (StaffMember)

Registered: Apr 2002
Local time: 04:47 PM
Location: The Netherlands
Posts: 4818

Post #13

dankan, a small remark. This line:

ActionScript:
var randomNum:Number = Math.floor(Math.random()*(max+1));



is the same as:
ActionScript:
var randomNum:Number = Math.ceil(Math.random()*max);



It's a bit cleaner. Otherwise, great help! Keep it up!

__________________
.:: PoeticTerror.Com ::.
.:: FlashDevils ::.
.:: FlashFocus ::.
.:: GalaxyGoo ::.
.:: OrganicFlash ::.



Report this post to a moderator | IP: Logged

Old Post 06-01-2007 09:52 AM
XemonerdX is offline Click Here to See the Profile for XemonerdX Click here to Send XemonerdX a Private Message Click Here to Email XemonerdX Visit XemonerdX's homepage! Find more posts by XemonerdX Add XemonerdX to your buddy list Edit/Delete Message Reply w/Quote
soft_machine
Little Devil

Registered: May 2007
Local time: 04:47 PM
Location: london
Posts: 8

Post #14

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

Old Post 06-01-2007 11:31 AM
soft_machine is offline Click Here to See the Profile for soft_machine Click here to Send soft_machine a Private Message Click Here to Email soft_machine Find more posts by soft_machine Add soft_machine to your buddy list Edit/Delete Message Reply w/Quote
dankan775b
Little Devil

Registered: May 2007
Local time: 04:47 PM
Location:
Posts: 23

Post #15

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

Old Post 06-01-2007 11:42 AM
dankan775b is offline Click Here to See the Profile for dankan775b Click here to Send dankan775b a Private Message Find more posts by dankan775b Add dankan775b to your buddy list Edit/Delete Message Reply w/Quote
All times are GMT. The time now is 04:47 PM. Post New Thread    Post A Reply
  Last Thread   Next Thread
Advertising
Show Printable Version | Email this Page | Unsubscribe from this thread | Download thread

Forum Jump:
Rate This Thread:

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

Links
Books