Here's a modified version of your code that will do it. I was able to shorten your code a bit by using the same function for shuffling the cards to perform the initial placement.
code:
// Some Useful constants - better to encode these up here
// than to have them as literals inside your code...
kLeftMargin = 80;
kTopMargin = 80;
kCellWidth = 70;
kCellHeight = 70;
kNbrColumns = 4;
kNbrRows = 4;
kTotalCards = kNbrColumns*kNbrRows;
// The basic trick:
// You can convert a single number n to a pair of card indexes (x,y)
// using x = (n % kNbrColumns) and y = int(x/kNbrColumns)
// I use this technique a couple of times below.
// Function to place cards in random order
ShuffleAndDeal = function()
{
// create an array of card indexes
shuffleOrder = [];
for (var i = 0; i < kTotalCards; ++i) {
shuffleOrder[i] = i;
}
// shuffle the array using a random sort
shuffleOrder.sort(function(){ return random(2)? -1 : 1});
trace("Order: " + shuffleOrder);
// use the array to select cards
for (var i = 0; i < kTotalCards; ++i)
{
r = shuffleOrder[i];
// card index
// note: if your cards were numbered consecutively (0,1,2) then you
// could simply use 'r' and don't need to compute rx,ry
rx = r % kNbrColumns;
ry = int(r/kNbrColumns);
// position on board
ix = i % kNbrColumns;
iy = int(i/kNbrColumns);
piece = _root[ry+"-"+rx];
piece._x = kLeftMargin + iy*kCellWidth;
piece._y = kTopMargin + ix*kCellHeight;
}
}
your code for shuffling an array rocks!!! I have been working it for so long, yet cannot find an answer... but i modified your codes and place it into my application and it works!!! Cool !!!
Almost bang-on what I was looking for. My cards are 2 frame movieclips. Frame 1 is the "backing", Frame 2 is the "face". They start out face down and when clicked on, the cards switch to Frame 2 (showing their value).
With your code above, it rearranges the order of the cards (which is great) but it doesn't flip the ones showing Frame 2 back to Frame 1.