PDA

View Full Version : Shuffle?



XwhyZ
November 28th, 2005, 09:33 AM
I think this should be easy but do not know how to randomly shuffle this array.
Lets say I have 4 movie clips, but I just need to shuffle their order,by pressing a button, How???
Thanks for any help in advance!
Cheers!!!

Barn
November 28th, 2005, 09:41 AM
You can shuffle the order of items in an array like the followings (but I'm not sure what you mean by shuffling the order of the movieclips -- may have to target their depths for that if you mean their stacking order).

I've used strings as the elements of the array here, for demonstration purposes, as the code will fail without actual movieclips matching the references in the array:


function shuffle() {
return Math.floor(Math.random()*3)-1;
}
rraValue = ["stgValue0", "stgValue1", "stgValue2", "stgValue3"];
trace("rraValue: "+rraValue);
rraValue.sort(shuffle);
trace("rraValue: "+rraValue);
//

XwhyZ
November 28th, 2005, 11:05 AM
You can shuffle the order of items in an array like the followings (but I'm not sure what you mean by shuffling the order of the movieclips -- may have to target their depths for that if you mean their stacking order).

I've used strings as the elements of the array here, for demonstration purposes, as the code will fail without actual movieclips matching the references in the array:


function shuffle() {
return Math.floor(Math.random()*3)-1;
}
rraValue = ["stgValue0", "stgValue1", "stgValue2", "stgValue3"];
trace("rraValue: "+rraValue);
rraValue.sort(shuffle);
trace("rraValue: "+rraValue);
//


No nothing so deep...no stacking order, just need the clips to exchange places on the table similar to moving cards on a table but using the same positions or x and y co-ordinates.
Thanx for replying...
Cheers!!!

Barn
November 28th, 2005, 11:10 AM
In that case, you can use two arrays -- one containing the movieclip references and one containing the X,Y coordinates. And shuffle either one (but not both).

XwhyZ
November 28th, 2005, 11:16 AM
In that case, you can use two arrays -- one containing the movieclip references and one containing the X,Y coordinates. And shuffle either one (but not both).
Thats what I too am thinking but not to sure how to code it ....can you run that by me it will be a big help.
Thanx again! will keep trying till then!!
Cheers!!!

Barn
November 28th, 2005, 11:27 AM
Well, that depends on whether you are dynamically placing these movieclips, initially, or whether you are placing them in the authoring environment.

If you're placing them in the authoring environment, you'll want to have the function extract the array of coordinates from their current respective positions.

If you're placing them dynamically, you already HAVE the coordinates (presumably in an array?).

G
November 28th, 2005, 11:35 AM
ok I've never done this in flash but it sounds like you want to use an ADT, in which case the ones that spring to mind area queue, stack or a linked list.

If you search in google for "Queue ADT" etc you will find out how each work and their logic.

XwhyZ
November 28th, 2005, 11:37 AM
Well, that depends on whether you are dynamically placing these movieclips, initially, or whether you are placing them in the authoring environment.

If you're placing them in the authoring environment, you'll want to have the function extract the array of coordinates from their current respective positions.

If you're placing them dynamically, you already HAVE the coordinates (presumably in an array?).
At the moment they are put in at authoring time and just to keep it simple so I can figure it out all I need to do is switch the X values, at the moment (50, 150, 250, 350)
Thanx for taking the time.
Cheers!!!

stringy
November 28th, 2005, 11:44 AM
At the moment they are put in at authoring time and just to keep it simple so I can figure it out all I need to do is switch the X values, at the moment (50, 150, 250, 350)
Thanx for taking the time.
Cheers!!!
have a look at the files here
http://www.kirupa.com/forum/showthread.php?t=150139

edit/its a very similar method to that described by Barn-i think he has been copying ;)

Barn
November 28th, 2005, 11:55 AM
Okay, well let's say you have four movieclips named mvcItem0 through mvcItem3 and another movieclip to act as a button to trigger the shuffling, called mvcBtn all on the same timeline.


function shuffle() {
return Math.floor(Math.random()*3)-1;
}
rraObjects = [mvcItem0, mvcItem1, mvcItem2, mvcItem3];
fncReArrange = function (rfcRra) {
var rraPos = new Array();
for (icrIdx=0; icrIdx<rfcRra.length; icrIdx++){
rraPos[icrIdx] = [rfcRra[icrIdx]._x, rfcRra[icrIdx]._y];
}
rraPos.sort(shuffle);
for (icrIdx=0; icrIdx<rfcRra.length; icrIdx++){
rfcRra[icrIdx]._x = rraPos[icrIdx][0];
rfcRra[icrIdx]._y = rraPos[icrIdx][1];
}
};
mvcBtn.onPress = function(){
fncReArrange(rraObjects);
}
//

Note: with this scenario you could have another button that passes a different array, as a parameter, to the function, thereby shuffling a different set.

Note also that this scenario will not, of itself, guarantee a different order every time, as it is perfectly possible for true random selection to generate exactly the same order as the previous time. If you wanted "pseudo random" (mandating no repeating order), you'd have to store the prior order, shuffle, and then compare, continuing to shuffle until you had a different outcome.

XwhyZ
November 28th, 2005, 12:41 PM
Okay, well let's say you have four movieclips named mvcItem0 through mvcItem3 and another movieclip to act as a button to trigger the shuffling, called mvcBtn all on the same timeline.


function shuffle() {
return Math.floor(Math.random()*3)-1;
}
rraObjects = [mvcItem0, mvcItem1, mvcItem2, mvcItem3];
fncReArrange = function (rfcRra) {
var rraPos = new Array();
for (icrIdx=0; icrIdx<rfcRra.length; icrIdx++){
rraPos[icrIdx] = [rfcRra[icrIdx]._x, rfcRra[icrIdx]._y];
}
rraPos.sort(shuffle);
for (icrIdx=0; icrIdx<rfcRra.length; icrIdx++){
rfcRra[icrIdx]._x = rraPos[icrIdx][0];
rfcRra[icrIdx]._y = rraPos[icrIdx][1];
}
};
mvcBtn.onPress = function(){
fncReArrange(rraObjects);
}
//

Note: with this scenario you could have another button that passes a different array, as a parameter, to the function, thereby shuffling a different set.

Note also that this scenario will not, of itself, guarantee a different order every time, as it is perfectly possible for true random selection to generate exactly the same order as the previous time. If you wanted "pseudo random" (mandating no repeating order), you'd have to store the prior order, shuffle, and then compare, continuing to shuffle until you had a different outcome.

Thanx a lot works like a charm will study that and improvise in the direction I need to go along.

Also Thanx to Stringy will go thru that will surely help me in what I want to do and also G for dropping in.

Thanx Guys you'll take the cake!!!
Cheers!!!

Barn
November 28th, 2005, 12:57 PM
have a look at the files here
http://www.kirupa.com/forum/showthread.php?t=150139

edit/its a very similar method to that described by Barn-i think he has been copying ;)
Actually, no -- mine's coded from scratch. Besides, the shuffle function you provided in that thread is incomplete with the only possible returns of the shuffle being 0 and 1 -- needs to be a negative 1 in there too.

stringy
November 28th, 2005, 01:14 PM
Actually, no -- mine's coded from scratch. Besides, the shuffle function you provided in that thread is incomplete with the only possible returns of the shuffle being 0 and 1 -- needs to be a negative 1 in there too.

yes i know you coded from scratch and i also knew the shuffle function you are using is better(i only remarked a couple of weeks ago that i had "pinched" it from you on MM forums) - was just ment in fun.

Barn
November 28th, 2005, 01:15 PM
yes i know you coded from scratch and i also knew the shuffle function you are using is better(i only remarked a couple of weeks ago that i had "pinched" it from you on MM forums) - was just ment in fun.
Guess I should have known -- what with the winking smiley and all.