Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Once a game is finished, we will show a screen indicating the game has been won. This screen is inside of a movie clip that I called 'win.' So if you look at the Properties panel you see it's just win, all lowercase, and there's really nothing special to it. There is a button in there, but the interactivity is all going to be on the whole movie clip, and not just on that button. So I am not going to go inside the movie clip for now. So if you don't have the exercise files, you can just put anything you want inside of a movie clip.
But it should take up the full width and height of the stage. So let's go over to Map.as. In here, we will create some new properties under the dragdrops property. Create one called numOfMatches, capital O, capital M. This is going to represent how many matches the person got when they are playing the game. And so we will use that to count against the number of items inside of the dragdrops Array.
So if they have a number of matches that is the same as the total number of objects in the dragdrops array, that means they dropped everything in the right spot. So in that case, they have won the game, so we want to show the winning screen. So let's datatype this to a uint, and we will set it equal to zero. Then we will cerate a variable or property called speed right under that. That's going to be a number. I will give that a default value of 25.
Now speed is going to represent how fast the win screen comes up, because we are going to have it animate using ActionScript. Now scroll down to the match function. In here, type numOfMatches, capital O capital M, plus, plus. Remember, the plus plus operator increments a value by one. So it adds one to the current value of it. So each time it get a match, we are going to add to the value of numOfMatches.
And just like you would expect, minus minus decrements a value by one. So it takes one away from the current value. But for now, we just need the plus plus operator. So go to the next line of code and create an if statement, and here we are going to check to see if they won the game. So they won the game when numOfMatches is equal to the total objects inside of the dragdrops array. So inside the parentheses here, we will type numOfMatches, capital O, capital M, space, equals, equals, space.
Remember that two equal signs means 'is equal to.' That's the comparison operator that you use when you are using an if statement - different from the single equal sign which is used to set a value in ActionScript. So then type dragdrops.length. That signifies the amount of items that are inside of the dragdrops array, which is every drag-and-dropable object. So when we have a match for every drag- and-dropable object, then we won the game. So we are going to put the winning code right in this if statement here.
So type win.addEventListener, capital E, capital L, put in some parenthesis and a semicolon, and here I am going to type Event, capital E .ENTER_FRAME all caps. This is going to run with every frame of the movie. So it's going to run 24, 30, 60 times a second, depending on your frame rate. So comma and space, and then we'll run a method called winGame, capital G, so we will define that method right now.
Right below the Match method, create a new method. This time we can just type function, we don't have to type the word 'public.' So we will call it winGame, capital G, and in the parentheses, that will receive an event with the datatype of Event. Notice this parameter is blue saying it is a built-in key word in Flash, but I am using it anyway as my custom parameter, and in this case, it's actually okay. You will notice that if you had a code snippet that has an event handler, Flash will actually use this word Event.
So if you don't like that, you can just simply type evt, but personally I mostly use the word event here, so your call. Colon, void, and some curly braces, and here we are going to have the win movie clip animate. So we will type win.y -= speed; so each frame, the win movie clip is going to move up - that's what the minus says. Remember, the top edge of the stage is the 0,0 position.
That's going to move about 25 pixels per frame. So that's how it's going to animate. Then what we are going to do is wait until the win movie clip gets all the way to the top of the stage, and then stop it. So type if and some parenthesis and some curly braces. We are going to check to see if the win object's position is less than or equal to zero. So if win.y is less than, so less than sign, or equal to, so equal sign 0, then we are going to set its position to zero.
So win.y = 0, and then we are going to stop running that inter-frame command. Just remember that takes up a lot of memory running a command in multiple times per second. So let's just highlight win.addEventListener(Event.ENTER_FRAME and copy it with Command+C or Ctrl+C. And right below where we set win's y position to 0, we will paste that code and then change 'add' in addEventListener to 'remove.' So we are going to take away that EventListener.
So we are just disabling that block of code, so winGame won't continue to run after the win movie clip is in the top position. All right, so let's see this in action. But remember, before you test it, you need to save the ActionScript, or else it won't work. File > Save and then test the movie and then put all the objects in the right place. So this goes here. Look at that.
All right, now there is the good job screen. You can click it and nothing happens. There we go. We successfully detected whether the player won the game. So remember, if you ever want to animate object vertically, you can add an inter-frame listener, but just make sure to remove it once the object is in place.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 105295 Viewers
56 Video lessons · 117045 Viewers
71 Video lessons · 86250 Viewers
131 Video lessons · 41228 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.