4 Replies Latest reply on Aug 23, 2010 1:02 PM by kglad

# How to create a pattern randomly without overlapping?

I got some movie clips with different shapes and I would like to make a pattern from those movie clips. I have assigned the x y co-ordinates of those movie clips with the Math.random function. However, the shapes overlap all the time, what can I do to avoid it? I have tried to do a hitTest, but then it doesn't work... thank you very much!=)

• ###### 1. Re: How to create a pattern randomly without overlapping?

why doesn't hitTest work?

• ###### 2. Re: How to create a pattern randomly without overlapping?

I have implemented three for loops for the hit Test and I have set the condition as "if the object has detected a collision against another object it will be shift to the right". However the overlapping still occurs... should I implement 7 for loops for it? Thank you

• ###### 3. Re: How to create a pattern randomly without overlapping?

I don't know what you are doing for whatever loops you implement, but I don't think quantity is the issue, moreso intelligence (or lack of)... nothing personal intended, I'm talking about the intelligence you build into the system.  You could move a planted piece in any direction and continue to have collisions as you move from atop one piece onto another.

What you might think about doing instead is making the system aware of where it can no longer plant objects based on where each item is planted and how much area each occupies.  So as you add items, you add to a list that identifies which areas (ranges of x and y locations) must be excluded from the random selections.

I haven't thought this thru beyond the concept, so I can't say it is without complications.

kGlad can likely provide you with a clever solution in any case.

• ###### 4. Re: How to create a pattern randomly without overlapping?

you only need to loop through each pair of movieclips so would use, at most, two nested for-loops.  in this situation, though you only need one for-loop:

var mcNum:Number = 11;  // number of non-overlapping movieclips to add

// code to add a randomly placed movieclip

mcA.push(the new movieclip);

}

while(mcA.length<mcNum){

for(var i:Number=0;i<mcA.length-2;i++){

// there's no checking to see if you're trying to do something stupid like trying something that's impossible

if(mcA[mcA.length-1].hitTest(mcA[i])){

mcA.pop();

}

}

}