Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
After someone is finished with the game, they may want to play again. Let's take a look at how to do that. So let's scroll down and find the winGame method. Now if you're ever looking at these methods in here, because we're going to have to tab in so much to organize the code, if the word wrap bothers you, then you can always turn it off. But if you still want it on anyway, but you want more space, you can always just click these Minimize buttons to minimize the panels, and double-click the Timeline to get a little bit more space.
So here, right below win.removeEventListener, I'm going to add win.addEventListener, capital E, capital L. This is going to be for a mouse click, and this is how we're going to reset the game. So we'll type MouseEvent capital M, capital E, .CLICK, in all caps, then a comma and space, and then clickWin capital W. So we'll have to define that method now. Right below the winGame method, make sure you're doing it below the right curly braces.
So you should have two extra ones at the bottom: one to close out the class and one to close out the package. So here, type function clickWin, capital W, in parentheses, type event:MouseEvent. After the parentheses, type colon void, add some curly braces. Then what we're going to do is remove that mouse click event listener in here. So highlight win.addEventListener, copy it, paste it into clickWin and change add to remove.
So we want to disable it after we've clicked it once. Then we're going to add an event listener to have the object animate downward. So actually, I'm going to copy win.removeEventListener, winGame right here. Copy that, Command+C or Ctrl+C and then paste it right below, win.removeEventListener(MouseEvent.CLICK). Here, I'm going to change removeEvetnListener to addEventListener. I'm going to leave Event.ENTER_FRAME. Then I'm going to change winGame to animateDown, with a capital D.
For animatingDown, I'm simply going to select and copy the winGame method, Command+C or Ctrl+C, and paste it right below clickWin. Change the method name in the pasted code to animateDown, capital D, and then change, win.y -= speed to win.y += speed. Then change the conditional statement to be if win.y is greater than or equal to the height of the stage, which can be accessed through stage.stageHeight.
When that happens, we'll set win.y equal to the height of the stage, which is stage.stageHeight. This is going to align the top of the win movie clip with the bottom of the stage, just like it is before you play the game. Then we're going to remove that event listener to animateDown. So I'm going to highlight win.addEventListener animateDown. That's on line 50 in my code. I'll copy that, and then I'll highlight win.remove EventListener and win.addEventListener in the animateDown method, and paste the code I just copied.
In the pasted code, I'll change addEventListener to removeEventListener. So basically, all that we've done so far is make it so that when you click the win movie clip, the win movie clip just animates right back down. So we haven't reset all the properties of the game yet. We'll do that in just a second. First, let's test to see if this animation works. Go to File > Save and test the movie. Drag all the objects into the right place. You should be an expert at this by now.
You get the win screen. If you click on it, it goes back down. So now all we have to do is reset all the objects. So let's close the SWF, and then go up to the clickWin method. At the bottom of the code, we're going to add a for loop to loop through all of the drag/drop objects. We've actually already written the for loop that goes through all the drag/drop objects at the top of this file. So let's just copy that. I'm going inside of the constructor method, and I'll copy every line from var currentObject:DragDrop to the end of the for loop.
Copy that with Command+C or Ctrl+C. Scroll down to clickWin, and still inside of the method, I'm going to paste the code with Command+V or Ctrl+V. Note that I have a variable called currentObject here. I'm also declaring it here, in the constructor. Whenever you create a variable inside of a function, that variable can only be used within that function. So I can't make a reference to currentObject outside of this function. If I define the variable in the class, outside of any method, then I can use it wherever I want in the file.
This existence of variables, like whether they can exist inside of a function or throughout all of your whole file, is called a variable's scope. So let's scroll down, and in clickWin, I'm going to change the line of code that says currentObject.target = getChildByName. I'm just going to erase everything before getChildByName in this line of code. So I'm referencing that currentObject's target.
I want to set every target object's alpha to zero. So right after the close parenthesis here, I'll type .alpha = 0. Then I want to show all the objects that you can drag and drop. So I'll go below that, and then type currentObject.visible = true. So I don't need to position them, because I already positioned them in the drag and drop class. Remember, they just go back to their original position, but they turn invisible.
So right below the for loop, I'm going to add the - one of the last lines of code to finish the game. I need to set the number of matches back to zero, because if I don't set it back, then it's going to keep going up every time, so I'm going to have a problem. So I'll type numOfMatches =0. Now let's save this file, and test the movie, and see what we get. Click and drag all the objects into place.
Look at the win screen. Now I want you to pay attention to what happens when I click the win screen. Watch where the drag and drop objects are. Notice that they all come up above that win movie clip. That's a little bit awkward and that's happening, because every time we drag an object, we bring it all the way to the front. So we can fix that by putting the win object in the front. So we can actually do that right here inside of clickWin. So I'll go down one more line, and type addChild, and in parentheses, pass in win.
Save this file and test it one more time. Drop all the objects into place. Watch the win screen. When you click the win screen, pay special attention to the drag drop area. So the win screen is now in front of all of those objects. Note that you can still see them because there is a little transparency. But they're not clearly in the front, fully opaque in front of the win object.
So now the game is completely finished, and the person playing can click to reset the game, and play as many times as they want.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 99447 Viewers
56 Video lessons · 112683 Viewers
71 Video lessons · 81502 Viewers
131 Video lessons · 39125 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.