7 Replies Latest reply on May 29, 2008 6:15 AM by j9lemmon

Prevent two movie clips from occupying same position

As part of creating a game, I need to use actionscript to randomly place movie clips, on a grid spaced 50 pixels apart. This all works, but occasionally two movie clips are place at the same location. I want to prevent that. I'm guessing that I need a 'for' loop or an array. I'm thinking something like, but this is probably wrong:
function putCollectiblesOnStage() {
for (k=1; k<15;++)
for (m=1; m<15;++) {
if ((collectible_k._x = collectible_k.x) and (collectible_m._y = collectible_m._y)) {
okToPlaceCollectible = false;
}
}
}
Thanks for any help
John

• 1. Re: Prevent two movie clips from occupying same position
well you 'kind of' have the right idea here - but there's alot more to it than that i afraid. first of all what is the size of your grid - so many columns by so many rows? this will be the determining factor in how you store the information.

secondly, yeah you want to use an array to store item locations, even if there isn't one present, and then you check a 2 dimensional array to find the correct location and see if there is an item currently stored at that location. and if you do use an array in this way - you will not need to iterate through the array to find an item - you can 'point' to the location by using a little math to find the position in relation to the given input.

also, i'm afraid the syntax above isn't quite right :( just fyi.
• 2. Re: Prevent two movie clips from occupying same position
If all the x and y locations are placed into a two dimensional object array, the array can be shuffled and then just place the objects in consecutive order to the x,y positions in the shuffled array since they will then be random.
• 3. Re: Prevent two movie clips from occupying same position
Just an example. This will place images randomly and will guarantee you never have a duplicate and avoids all that messy if..else stuff.
• 4. Re: Prevent two movie clips from occupying same position
These comments are all very helpful. Something I forgot to mention is that there are certain places on the grid where I don't want to place a movie clip. Is there a way for an array to do this?
• 5. Re: Prevent two movie clips from occupying same position
I used nested for loops to define the grid positions because it was easy. The downside is that it creates every spot on the grid. At the end of the day, what you want is a two dimensional array containing the x and y coordinates of all the grid points that you would like to place an object on. There are a few different ways to do that. The first would be probably the simplest and just push all the points onto the array that are valid. Another way would be to put in some conditionals in the for loops that don't push the point if it is a certain point you don't want. Yet another way is to make the full array and then later cut out individual points of the array.

However you decide to get there, you should end up with the same result. Personally I would go with option one. It might take more lines of script but it involves no logic so is easy to do.

Once that array is created with all the valid x and y points, shuffle the array and start placing objects.

So in that script I posted, only lines 2 thru 6 would need adjustment.
• 6. Re: Prevent two movie clips from occupying same position
And here is a simple example
• 7. Re: Prevent two movie clips from occupying same position
Thanks for all the help!