Skip to main content
March 9, 2011
Answered

Randomly arranged grid from single loop

  • March 9, 2011
  • 1 reply
  • 928 views

Hi there,

I have the following code with which I am creating a grid.. what I need to do is, either create the grid in a random order at the beginning, or 'shuffle' the grid once it has been created.. meaning each time I load the page the order is changed.. is this possible ?? Thanks in advance for any response

for (var i:uint = 0; i< boxNum; i++) {
                   
     var animatedButton = new AnimatedButton();
                   
     animatedButton.x = animatedButton.width * (i % cols);
     animatedButton.y = animatedButton.height * int(i / cols);
     grid.addChild(animatedButton).name = "m" + (i + 1);

}

This topic has been closed for replies.
Correct answer

Another problem is that if I create the array and splice the array within the loop it just adds then removes the same property.


here is an example:

import flash.display.MovieClip;
var cols:uint = 2;
var abArray:Array = [];

for (var i:uint = 0; i< 4; i++)
{
var myObj:Object = getDefinitionByName("P"+ (i) ) as Class;
var piece:MovieClip = new myObj();
abArray.push(piece);
//piece.addEventListener(MouseEvent.MOUSE_OVER, animatedButtonMouseOver);
//piece.addEventListener(MouseEvent.MOUSE_OUT, animatedButtonMouseOut);
//piece.addEventListener(MouseEvent.CLICK, animatedButtonClick);
addChild(piece);
}

for (var iii:uint = 0; iii< 4; iii++)
{
var remove:uint = Math.floor(Math.random() * abArray.length);
var mcm:MovieClip = MovieClip(abArray.splice(remove,1)[0]);
mcm.x = 200 * (iii % cols);
mcm.y = 200 * int(iii / cols);
}

1 reply

March 9, 2011

You have to use an array with  (0,1,2,....., boxNum-1) elements.

Then randomly pull an item from the array (boxNum-1) times. -- myArray.splice(Math.floor(Math.random()*myArray.length),1); ---

Each time that item will be your i variable.

March 9, 2011

Hi Hakan,

Thanks for the response.. I understand completey what you are telling me to do but I have a problem with the naming schema because I created the movieclips dynamically (which I wish I hadn't done now).. When I'm pushing the animatedButton to the array, even if I name it beforehand, my array only contains [object AnimatedButton] when I traceabArray..

I'm finding it difficult to add the 'piece' (see code) to the animatedButton without animatedButton having a name I can target properly via actionscript !!! Here is my revised code, for the moment I am not removing any item from the array, just creating it.. Is there a way I can resolve the naming issue ?

Thanks again

var abArray:Array = [];
           
for (var i:uint = 0; i< boxNum; i++) {
                   
     var animatedButton = new AnimatedButton();
               
     animatedButton.name = "m" + (i + 1);
     abArray.push(animatedButton);
                   
     animatedButton.x = animatedButton.width * (i % cols);
     animatedButton.y = animatedButton.height * int(i / cols);
     gamePage.grid.addChild(animatedButton);
               
     var myObj:Object = getDefinitionByName("P"+ (i + 1) ) as Class;
     var piece:MovieClip = new myObj();
               
     animatedButton.addChildAt(piece, 0).name = "p" + (i + 1);
               
     animatedButton.addEventListener(MouseEvent.MOUSE_OVER, animatedButtonMouseOver);
     animatedButton.addEventListener(MouseEvent.MOUSE_OUT, animatedButtonMouseOut);
     animatedButton.addEventListener(MouseEvent.CLICK, animatedButtonClick);

}

March 9, 2011

Another problem is that if I create the array and splice the array within the loop it just adds then removes the same property.