Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Enabling a pause feature

From: Building and Monetizing Game Apps for Android

Video: Enabling a pause feature

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.

Enabling a pause feature

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.

Show transcript

This video is part of

Image for Building and Monetizing Game Apps for Android
Building and Monetizing Game Apps for Android

55 video lessons · 10575 viewers

Todd Perkins
Author

 
Expand all | Collapse all
  1. 5m 1s
    1. Welcome
      59s
    2. Using the exercise files
      2m 36s
    3. What you should know before starting this course
      25s
    4. Viewing the finished game
      1m 1s
  2. 19m 49s
    1. Understanding and downloading Cocos2d-x
      2m 16s
    2. Downloading the Android NDK
      1m 49s
    3. Creating a Cocos2d-x project template for Eclipse
      4m 51s
    4. Finishing the project template
      1m 4s
    5. Preparing Eclipse to use C++
      1m 9s
    6. Importing the project into Eclipse
      3m 48s
    7. Adding GLES 2.0 support to your emulator
      3m 45s
    8. Fixing additional compiler issues
      1m 7s
  3. 52m 7s
    1. Understanding scenes, layers, and sprites
      2m 16s
    2. Using scenes, layers, and sprites
      3m 58s
    3. Positioning sprites
      1m 4s
    4. Adjusting basic sprite properties
      5m 32s
    5. Handling touches
      3m 54s
    6. Accessing accelerometer data
      6m 7s
    7. Understanding Cocos2d actions
      2m 37s
    8. Working with sprite sheets
      4m 28s
    9. Creating simple frame animations
      3m 44s
    10. Setting a game to display in portrait mode
      54s
    11. Designing for multiple screens
      3m 20s
    12. Running Java code from C++
      5m 21s
    13. Running C++ code from Java
      3m 31s
    14. Using buttons
      3m 34s
    15. Playing audio
      1m 47s
  4. 17m 0s
    1. Viewing a flowchart of the game's core classes
      1m 25s
    2. Creating constants and editing the AppDelegate and Main classes
      4m 8s
    3. Building the GameButton class
      3m 38s
    4. Constructing the custom pop-up menu utility
      2m 52s
    5. Setting up the code in the MainMenu class
      2m 10s
    6. Making the Utils class
      2m 47s
  5. 53m 13s
    1. Adding the game layers and the background
      5m 41s
    2. Laying out moles in the Game class
      4m 45s
    3. Animating the moles
      5m 44s
    4. Displaying moles at a specified time interval
      4m 39s
    5. Handling touches in the game
      4m 11s
    6. Displaying the player's score
      4m 2s
    7. Handling misses
      6m 27s
    8. Controlling the number of moles on the screen
      2m 25s
    9. Adding sound
      2m 31s
    10. Enabling a pause feature
      5m 36s
    11. Displaying the game-over screen
      3m 13s
    12. Saving the player's high score
      3m 59s
  6. 6m 53s
    1. Preparing your game to support Google ads
      3m 14s
    2. Implementing Google ads in your game
      3m 39s
  7. 18m 9s
    1. Handling skins in your game code
      4m 55s
    2. Setting up in-app purchases (IAP) for an Amazon Appstore app
      3m 1s
    3. Implementing Amazon IAP code
      4m 11s
    4. Setting up IAP for Google Play
      2m 38s
    5. Implementing Google Play IAP code
      3m 24s
  8. 2m 56s
    1. Using the Cocos2d-x website
      1m 13s
    2. Viewing additional Android game development resources
      1m 43s
  9. 29s
    1. Goodbye
      29s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed Building and Monetizing Game Apps for Android.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

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.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.