Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
In this movie we'll learn how to randomly place our cards. If you're following along we're in Memory.fla, Card.as and MemoryGame.as. All these files can be found in the 9-11 folder inside of the chapter 09 folder along with the other exercise files. Let's go to MemoryGame.as, scroll down to the area where we created variables. After we created the variable called _snow, press Enter and type private var _cards. The datatype of the cards will be an array so type :Array, with a capital A, and a semicolon.
What we're going to do is use an array to hold all of our cards and then use a loop to randomly place the cards in the array. Scroll down a little bit to our constructor function after the open curly brace, press Return or Enter on your keyboard and type _cards = new Array(); Se we've created a new array and now what we need to do is add to the array every time we create a card. So scroll down in our code.
Find the createCcards method. Inside of that method find the first for loop. The variable should be called i. Scroll to the right of your code if you have to, after the event listener we added, and then press Enter on your keyboard. You may want to scroll back a little bit, and then type _cards.push(_card this one's not plural, ); This will add the card to the top of the array.
Let's copy this line of code by selecting it and pressing Cmd or Ctrl+C on your keyboard. Scroll down. Let's paste this line of code for the j loop, after the MouseEvent.CLICK, when we added the event listener to card on line 61. Scroll down to the k loop. Click after add event listener on line 74, press Return or Enter on your keyboard then press Cmd or Ctrl+V to paste the code again and scroll down to the l for loop.
Click after the add event listener line and press Return or Enter again and then paste by pressing Cmd or Ctrl+V one more time. So everyone of our cards will be inside of our array. Now we'll write a function to randomly place these cards. Scroll down to the very bottom of your code. You'll see one close curly brace, and then to the right of that there's another close curly brace. To the right of that there's another close curly brace. So after the third close curly brace from the left, press Enter on your keyboard twice.
So we'll write this code underneath the checkCards event handler. Type private function randomizeCards with a capital C. Type (cards:Array with a capital A, this will receive our cards array so we can randomize them, ):void Go to the next line, open curly brace, next line, close curly brace, up arrow, Enter Now what we'll do is we'll create variables to hold two random cards and the random x and y values of the first card.
Type var randomCard with a capital C, 1:Number; What we're going to do is we're going to take two random cards and then swap the x and y positions. We're going to put this in a loop and do that multiple times and that will give us random placement of cards. Press Enter or Return. Go to the next line. Create a new variable called randomCard with a capital C, 2. That will also be a Number, then type a semicolon. Go to the next line. Create a new variable. We'll call this card1X: it'll also be a number.
This will stand for the original x position of card1. Then type a semicolon. Now we'll create a variable to hold the card1's y position, so type var card1Y:Number; Press Enter or Return twice to go down two lines. That'll give us some space, then type for(var i:Number = 0; i < We want this code to loop as many times as we want. What it's going to do is swap the position of two cards, however many times you think will randomize your cards enough.
I'm going to type 10. Then type a semicolon, then type a space type i++) Go to the next line by pressing Return or Enter on your keyboard. Open curly brace, next line, close curly brace, up arrow, Enter or Return to get within the curly braces. Now what we'll do is we'll generate a random card by getting a random index inside of our array. So type randomCard with a capital C, 1 = Math with a capital M, .floor. Remember Math.floor rounds down to the floor.
(Math with a capital M, .random() * cards.length And this will give us a random index of our array. Type a close parenthesis then a semicolon. Let's select this whole line of code, press Cmd or Ctrl+C on your keyboard to copy it After the semicolon, press Return or Enter to go to the next line. Press Cmd or Ctrl+V to paste the code. Change randomCard1 to randomCard2 There every time the loop runs we'll have a new random card1 and a new random card2.
Now let's write the code to swap these cards. After the semicolon on the randomCard2 line press Return or Enter twice to go down two lines. To swap them we'll get randomCard1's original x value and original y value and swap it with randomCard2's y values and x values. Now type card1X = cards[ Now we'll reference the randomCard1 index of the array. So type randomCard with a capital C, 1].x; Let's copy this line of code by selecting it and pressing Cmd or Ctrl+C on your keyboard, clicking after the semicolon, press Enter or Return to go to the next line. Paste the code using Cmd or Ctrl+V.
Change card1X to card1Y, capital Y, and then the.x to .y. So now we have the initial x and y values of the first card. Now we'll go to the next line and set the randomCard1's x and y's equal to the randomCard2's x and y's and set the randomCard2 x and y coordinates equal to our card1 x and our card1 y variables. So now type cards[randomCard with a capital C, 1].x = Let's just copy everything before the equals sign by selecting it and pressing Ctrl+C or Cmd+C on the Mac.
We'll click after equal sign to paste the code by pressing Cmd or Ctrl+V and then we'll replace randomCard1 with randomCard2 and that will give the x position in randomCard1 to randomCard2. After that type a semicolon, select the line of code, press Cmd or Ctrl+C on your keyboard to copy it. After the semicolon go to the next line. Press Cmd or Ctrl+V to paste that code. Change the x's to y's. Now I'll go to the next line.
Now we'll set the randomCard2 positioning. So type cards[randomCard2].x = card1X; That value is coming from the original x position, like we said it above, the original x position of the randomCard1 x. Let's copy that line of code by pressing the Cmd+C or Ctrl+C after we've selected it.
Click after the semicolon. Press Return or Enter to go to the next line. Paste the code using Cmd or Ctrl+V. Change the x's to y's, make sure card1Y is uppercase, and there is our randomize cards function. All we have to do is call it. Let's go up to our create cards method. After the l for loop from line 78 to line 89, click after the close curly brace.
Press Return or Enter on your keyboard and then call the randomized cards function. Type randomizeCards with a capital C, open parenthesis. Now all we need to do is pass in our card's array. So type _cards); The array holding all of our cards will then be sent in to the randomized cards method as the array and the cards will come out random. Go to File, Save and then test the movie by pressing Cmd+Return or Ctrl+Enter on your keyboard.
Flip over a card and we have a blue board there. Click the card to the right. It was wrong but we didn't get to see what card it was. Let's move the Flash player with the left a little bit. Click one card, there's the mountains. And then click another card and there's the snowboarder. All the cards have been randomized. And that is how to randomize your cards and that should give you a complete Memory Game.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 101182 Viewers
61 Video lessons · 87935 Viewers
71 Video lessons · 71827 Viewers
56 Video lessons · 103675 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.