Skip to main content
saratogacoach
Inspiring
October 16, 2012
Question

Shuffling Image Positions

  • October 16, 2012
  • 1 reply
  • 1213 views

If I have 20 images (cut jigsaw puzzle pieces) scattered around the top, sides and bottom of a stage, and want to shuffle their positions upon entering the frame (or using a button), what type of shuffle/swap array would I need to randomly swap their X Y positions? I've looked for examples in solitaire type games, but haven't found any so far.

Any help appreciated.

This topic has been closed for replies.

1 reply

Ned Murphy
Legend
October 16, 2012

If the positions are fixed, shuffle the order of the pieces and plant them per the fixed positions.  If you need to collect their positions first you should be able to loop thru them and collect that info into an aray and then loop thru that array and randomly pick which pieces get placed where.

saratogacoach
Inspiring
October 16, 2012

Hi Ned,

Thank you for this suggestion. Yes, they are fixed positions. Unfortunately, I can't get my brain around: "shuffle the order of the pieces and plant them per the fixed positions." I can hard-code the X and Y position of each piece, but not sure how to store X and Y in an array and then retrieve (maybe, not necessary?).

Ned Murphy
Legend
October 16, 2012

So far, I can, by adapting a script that I found, create an array of positions for the 20 pieces (currently named p1, p2, p3...p20), randomize the array and trace the output.

var positions:Array = [{x:28.85, y:21.85},{x:243.80, y:20.85},{x:450.80, y:17.85},{x:648.80, y:17.85},{x:850.30,y:17.85},{x:855.80,y:124.35},{x:852.80, y:269.35},{x:852.80, y:418.30},{x:855.80, y:597.30},{x:693.30, y:606.30},{x:604.80,y:612.30},{x:357.80,y:611.30},{x:194.80, y:608.30},{x:17.85, y:606.30},{x:853.30, y:491.30},{x:6.85, y:462.45},{x:10.85,y:216.50},{x:28.85,y:425.30},{x:236.80, y:609.45},{x:494.30, y:615.45}];

var len:int = positions.length;

var pos:int = 0;

for (var m:int = 0; m < len; m++)

{

   pos = int(Math.random() * positions.length);

   trace("Setting one clip to x: "+Object(positions[pos]).x+" and y: "+Object(positions[pos]).y);

   positions.splice(pos, 1);

}

What I need help with: even through I can set up a trace, I'm not sure how to now take the "traced" output and insert it into another array to position p1, p2, p3...p20?


I would have to think that by now kGlad has probably provided you with the shuffle function at least once.  You can use that same function to shuffle the array and then just loop thru it to place the pieces. 

You don't have any use for the trace other than to be able to see what it shows.  traces are for testing purposes to help you see what you do and don't have going on.

You didn't need to write out that array.  As I said, you should be able to loop thru all of the pieces where they lie at the start and create that array dynamically from recording the x and y locations of the pieces.  Then you take the array, shuffle it, and relocate all of the pieces by looping one of the arrays (either the pieces or the positions)... pieces.x = positions.x, etc....  This can all happen in the blink of an eye such that you would be unlikely to see the pieces moved from their original manually placed locations to their dynamically assigned ones.