|
-
I am trying to create a game with a (13x15)maze which is made from an Array called board.
Here's an example:
board[0] = new Array(2,2,2,2,2,2,2,2,2,2,2,2,2,2,2);
board[1] = new Array(2,0,0,0,1,1,1,1,1,1,1,0,0,0,2);
board[2] = new Array(2,0,2,1,2,1,2,1,2,1,2,1,2,0,2);
.
.
.
board[12] = new Array(2,2,2,2,2,2,2,2,2,2,2,2,2,2,2);
// 2 means a indestructable wall, 1 means destructable wall,
// 0 means open space which player can walk on.
This works fine, no problem here. But now I am trying to add some bonusses
at random coordinates on places with a no.1 wall in the Array. I used this function:
funcion addBonus(){
bCounter=0;
i=100;
do{
brickRow = random(13)+1;
brickColumn = random(11)+1;
if(_root.board[brickRow][brickColumn]== 0){
// do nothing
}
else if(_root.board[brickRow][brickColumn]== 2){
// do nothing
}
else if(_root.board[brickRow][brickColumn]== 1){
// place a MC at the right place
root.attachMovie("bonus","bonus"+i,i);
setProperty("bonus"+i, _x, brickRow*tileLength+edge);
setProperty("bonus"+i, _y, brickColumn*tileHeight+edge);
bCounter++;
i++;
}
} while(bCounter<_root.amountOfBonusses)
}
This function "loops" until all bonusses are placed in the maze. It's works but
has ONE thing I'm not happy about.
Sometimes "brickRow"&"brickColumn" generate the same random coordinate
like the last loop. E.g. first loop :
brickRow = 5;
brickColumn = 9;
Then on the second or third loop again:
brickRow = 5;
brickColumn = 9;
Placing 2 bonusses at the same coordinate!
My question (finally!): How can I prevent 2 MC's getting the same position with random generated x and y?
it's kinda drivin' me crazy!!!
-
Didn't do it.
Hey, Fozzy. First off, let me direct you to the man to ask all of these types of questions to; Ed Mack, the moderator of the Games forum here at Flashkit, who practically wrote the book on tile-based games. I'd suggest you go post this same question in the Games forum, or visit his web site (ed.gamegeared.com) for more help.
That being said, let me make a suggestion for your game. Why not, as the bonuses are being placed, create an array and add the coordinates of each bonus to it as they are placed. Then simply have the code loop through the array after the random numbers are generated and check to see if one is already there. If not, place it. If so, generate new numbers.
Again, Ed Mack will be able to give you the best advice, but I hope this helped.
japangreg
-
Thanks, I'll do that right away!
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
|