Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Now we'll look at how to pause the game, stop all the animation during the gameplay and have an option to resume or go to the main menu. So let's jump over to the Pause class and we'll start in the header file to just look at the properties and methods that exist for this class, and then go over to the Pause.cpp. In the init method we will give a value to pauseButton and we will set that equal to CCMenuItemSprite::create and then we need to pass in a sprite for the default sprite and then the secondary sprite.
The default sprite is going to be a CCSprite that we will create right here, this is going to be createWithSpriteFrameName and the FrameName is pause_button.png. Second parameter for CCMenuItemSprite:: create is the selected sprite which we'll pass in NULL, then the target which is this, and the selector we want to run. And remember you have to pass in a special format for that, that's' menu_selector and in the parentheses we pass in the selector which is Pause::pause.
Make sure you don't run the method here that you just reference it. Go to the next line and we will create a CCMenu and we will call this menu, set it equal to CCMenu::create and we will pass in pauseButton and then NULL, terminate the list of objects. Then we are going to set the position of the menu, so menu->setPosition and here I am not going to adjust the Anchor point, but I am going to put the pause button in the bottom right of the screen. The reason I don't want to adjust the anchor point is because we are controlling the position of the menu and the pause button, and they have relative coordinates to each other.
I have had a lot of problems with laying out menus and their individual buttons by using anchor points, so I always leave the default anchor points in the case of buttons and menu items. So we'll pass in the ccp here and then this is going to be Utils::s().width - pauseButton->getContentSize().width/2, and the Y position is just going to be half the pauseButton's height, which we will align it with the bottom of the screen.
So pauseButton->getContentSize().height/2. Then we are going to add it as a child of the pause layer, so this->addChild and then we will pass in menu and 1. So the Z position is going to be 1 and now we are going to give the value to the pop up object. So popup = Popup::popupWithTitle. We'll pass in a string for the title which is going to be PAUSED, and on the next line we will run addButtonWithText. so popup->addButtonWithText(), now we need to pass in the text for the button which is going to be RESUME.
Target is this, selector is menu_ selector and then we pass in the method we want to run, that's going to be Pause::resume. Then on the next line we will do the same thing, so I am going to copy and paste the previous line of code and change the text to MAIN to go the Main Menu, and the method that I want to run is Pause::mainMenu. Now you may wonder why I don't just run MainMenu, mainMenu since that already exists to take us to the Main menu.
That's because we are running an instance method here. We pass in this and that grabs the target and it runs a selector on the target, which is mainMenu. I will show you how I chose to work that below here. On the next line of code, add the popup as a child. So this->addChild, pass in the popup, z of 1. Remember you can share z values, it just puts the objects you add later in front of the earlier added objects. So in Pause::pause, I am going to run this->togglePause, pass in true and then we will just copy and paste this into resume, pass in false.
In mainMenu we will run MainMenu::mainMenu. Then we'll return to the MainMenu and in togglePause we will show the popup. It's popup->show, remember we need to pass in whether or not to show or hide the pop-up, so we just pass in that pause parameter. And if we want the game to pause, then we will show the popup, if not we won't show it. So the next line, Utils::gameLayer(), then we are going to setTouchEnabled to the opposite of paused, so !paused.
So that way if we paused the game, then Touch is disabled in the gameLayer and if we unpause the game then touch is enabled again. Go to the next line, and we are going to hide the pause button based on whether the game is paused. Pause Button->setVisible and we will pass in !paused. So again if the game is paused, we want to hide the pause button, and if the game is resuming we want to show the pause button. So let's Save and test this out in the emulator. Okay, I will click the Play button and then I should be able to pause the game when I click the Pause button.
(music playing) So the music stops as specified in the popup class, I can chose to resume the game or go to the Main Menu. (music playing) I resume and the animation continues. I can pause again that I can go to the main menu and there we are. So now if you ever want to pause your game, make sure to setTouchEnabled to false in the gameLayer and then you can use this popup class or create one of your own and use the similar method to stop audio and to pause the scheduler and actions.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 99270 Viewers
56 Video lessons · 112541 Viewers
71 Video lessons · 81345 Viewers
131 Video lessons · 39057 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.