shadedblue
06-13-2008, 02:48 PM
Hi all, I hope someone can help me with this.
I am creating a simple memory card game with 10 cards. For anyone who's unfamiliar with the concept, the user gets a quick look at the faces on the cards before the cards are flipped face down, and the user then has to try to find pairs of faces (10 cards = 5 pairs). In theory, each time the user plays the game, the face images are populated randomly so it's a new game each time. The problem I am having is that with the code I've written, some of the cards are coming up blank, which means not all 5 faces are represented on the cards. Below is the function I wrote to populate the card faces. I'm on a Mac so please bear with me if the spacing is a bit confusing:
_global.loadCards = function () {
trace ("you are accessing the loadCards function")
var randomUsed = false;
var listOfRandoms = new Array ();
for (i=1; i<=10; i++){
var newRandom = int(Math.random()*11);
if (newRandom == 0){
newRandom = 1;
/*for (j=1; j <= listOfRandoms.length; j++){
if (newRandom == listOfRandoms[j]){
randomUsed = true;
break;
} else {
randomUsed = false;
}
}*/
}
trace ("newRandom is " + newRandom);
listOfRandoms[i] = newRandom;
trace("List of randoms is "+listOfRandoms.length+" long");
trace(listOfRandoms);
/*for (j=1; j <= listOfRandoms.length; j++){
if (newRandom == listOfRandoms[j]){
randomUsed = true;
break;
} else {
randomUsed = false;
}
}*/
if (rabbitCounter < 2 && randomUsed == false){
_root["card"+newRandom].animalContainer.attachMovie("rabbitNodding", "animal", 1);
_root["card"+newRandom].clickValue = "rabbit";
rabbitCounter++;
trace ("rabbitCounter is " + rabbitCounter);
} else if (sheepCounter < 2 && randomUsed == false){
_root["card"+newRandom].animalContainer.attachMovie("sheepWalking", "animal", 1);
_root["card"+newRandom].clickValue = "sheep";
sheepCounter++;
trace ("sheepCounter is " + sheepCounter);
} else if (raccoonCounter < 2 && randomUsed == false){
_root["card"+newRandom].animalContainer.attachMovie("raccoonHopping", "animal", 1);
_root["card"+newRandom].clickValue = "raccoon";
raccoonCounter++;
trace ("raccoonCounter is " + raccoonCounter);
} else if (foxCounter < 2 && randomUsed == false){
_root["card"+newRandom].animalContainer.attachMovie("foxJumping", "animal", 1);
_root["card"+newRandom].clickValue = "fox";
foxCounter++;
trace ("foxCounter is " + foxCounter);
} else if (beaverCounter < 2 && randomUsed == false){
_root["card"+newRandom].animalContainer.attachMovie("beaverHovering", "animal", 1);
_root["card"+newRandom].clickValue = "beaver";
beaverCounter++;
trace ("beaverCounter is " + beaverCounter);
}
if (randomUsed == true){
i--;
}
}
}
You'll note that there is a second for loop nested within the first. Actually, it is in there twice, and I have it commented out in both places at the moment because I don't know where it is supposed to go, AND it tends to cause flash to crash entirely when I try to test the movie if I uncomment it in either spot. But a friend who's more familiar with AS than I am helped me with this code and he put it in there. I believe it (or something similar) needs to be there somewhere, as it is meant to check that the random number that is generated during an iteration of the first for loop hasn't been used before (which is what determines which card will be populated with which face), but I don't know how to fix it at all.
Any help with this would be much appreciated, as I have a deadline approaching :eek:
Thanks so much in advance...
I am creating a simple memory card game with 10 cards. For anyone who's unfamiliar with the concept, the user gets a quick look at the faces on the cards before the cards are flipped face down, and the user then has to try to find pairs of faces (10 cards = 5 pairs). In theory, each time the user plays the game, the face images are populated randomly so it's a new game each time. The problem I am having is that with the code I've written, some of the cards are coming up blank, which means not all 5 faces are represented on the cards. Below is the function I wrote to populate the card faces. I'm on a Mac so please bear with me if the spacing is a bit confusing:
_global.loadCards = function () {
trace ("you are accessing the loadCards function")
var randomUsed = false;
var listOfRandoms = new Array ();
for (i=1; i<=10; i++){
var newRandom = int(Math.random()*11);
if (newRandom == 0){
newRandom = 1;
/*for (j=1; j <= listOfRandoms.length; j++){
if (newRandom == listOfRandoms[j]){
randomUsed = true;
break;
} else {
randomUsed = false;
}
}*/
}
trace ("newRandom is " + newRandom);
listOfRandoms[i] = newRandom;
trace("List of randoms is "+listOfRandoms.length+" long");
trace(listOfRandoms);
/*for (j=1; j <= listOfRandoms.length; j++){
if (newRandom == listOfRandoms[j]){
randomUsed = true;
break;
} else {
randomUsed = false;
}
}*/
if (rabbitCounter < 2 && randomUsed == false){
_root["card"+newRandom].animalContainer.attachMovie("rabbitNodding", "animal", 1);
_root["card"+newRandom].clickValue = "rabbit";
rabbitCounter++;
trace ("rabbitCounter is " + rabbitCounter);
} else if (sheepCounter < 2 && randomUsed == false){
_root["card"+newRandom].animalContainer.attachMovie("sheepWalking", "animal", 1);
_root["card"+newRandom].clickValue = "sheep";
sheepCounter++;
trace ("sheepCounter is " + sheepCounter);
} else if (raccoonCounter < 2 && randomUsed == false){
_root["card"+newRandom].animalContainer.attachMovie("raccoonHopping", "animal", 1);
_root["card"+newRandom].clickValue = "raccoon";
raccoonCounter++;
trace ("raccoonCounter is " + raccoonCounter);
} else if (foxCounter < 2 && randomUsed == false){
_root["card"+newRandom].animalContainer.attachMovie("foxJumping", "animal", 1);
_root["card"+newRandom].clickValue = "fox";
foxCounter++;
trace ("foxCounter is " + foxCounter);
} else if (beaverCounter < 2 && randomUsed == false){
_root["card"+newRandom].animalContainer.attachMovie("beaverHovering", "animal", 1);
_root["card"+newRandom].clickValue = "beaver";
beaverCounter++;
trace ("beaverCounter is " + beaverCounter);
}
if (randomUsed == true){
i--;
}
}
}
You'll note that there is a second for loop nested within the first. Actually, it is in there twice, and I have it commented out in both places at the moment because I don't know where it is supposed to go, AND it tends to cause flash to crash entirely when I try to test the movie if I uncomment it in either spot. But a friend who's more familiar with AS than I am helped me with this code and he put it in there. I believe it (or something similar) needs to be there somewhere, as it is meant to check that the random number that is generated during an iteration of the first for loop hasn't been used before (which is what determines which card will be populated with which face), but I don't know how to fix it at all.
Any help with this would be much appreciated, as I have a deadline approaching :eek:
Thanks so much in advance...