|
-
Randomize button placement
I have an array of 10 buttons 2 rows of 5 buttons each.
I was wondering how I might randomize the button placement,each time a randomizeButton function is called.What I mean is keep the rows but how they are lined up and showing will be different each time.
Thanks
-
If I understood fine, you want to shift the placement of your ten buttons in a random order on the "kind of cells" you have now.
That's right?
One idea. One emotion. One project.

-
Sorry for the delay, but yes that is correct.
-
Well, there may be several ways to do it. What I'm thinking now is the next:
1. Give to each button the next instance names: "button1", "button2", etc.
2. Declare two variables, one for x value and an other one for y value. Also, determine the amount of separation between buttons. In the first frame, type:
Code:
stop();
xPosition = 0;
yPosition = 0;
space = 100;
3. In your main button, type the next randomizer and eliminator code:
Code:
on (release) {
// Make two arrays with the ten x and y positions.
colPositions = [1, 2, 3, 4, 5, 1, 2, 3, 4, 5];
rowPositions = [1, 1, 1, 1, 1, 2, 2, 2, 2, 2];
// Make a loop that randomizes and places from button10 to button1.
// The loop goes from 10 to 1.
for (turn=10; turn>0; turn--) {
// Randomize between the possibilities: 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9.
// In the next loop, it'll get only to 8, and so on.
randomNumber = Math.round(Math.random()*(turn-1));
// And here you have your core actions:
// setting each button in a random place,
// button 10 in the first loop, 9 in the second one and so on.
_root["button"+turn]._x = colPositions[randomNumber]*space;
_root["button"+turn]._y = rowPositions[randomNumber]*space;
// As you've "spent" one position, you have to eliminate it.
// splice eliminates that position in your two arrays.
colPositions.splice(randomNumber,1);
rowPositions.splice(randomNumber,1);
}
}
Each time you press and release the button, you'll see your ten buttons enter in the desired order.
I've made it and it works. Tell me if that goes to you.
À bien tôt.
Last edited by cancerbero; 11-23-2009 at 02:31 AM.
One idea. One emotion. One project.

Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|