Seasonal Savings: 20% off selected memberships for a limited time. Give now

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

Laying out moles in the Game class

From: Building and Monetizing Game Apps for Android

Video: Laying out moles in the Game class

To lay out the Moles, we'll first define what happens when you initialize a mole. Remember that the mole is a subclass of CCSprite and I have some precreated properties and methods. The only ones that I've defined thus far are the getter and setter methods, so for the Mole it's only getIsUp. Let's go into the Moles init method right under where we run the super init method to initialize Sprite. And in here we'll set upTime equal to 2 .0f and we'll set isUp equal to false, and then we'll set the display frame of the mole, so this->setDisplayFrame.

Laying out moles in the Game class

To lay out the Moles, we'll first define what happens when you initialize a mole. Remember that the mole is a subclass of CCSprite and I have some precreated properties and methods. The only ones that I've defined thus far are the getter and setter methods, so for the Mole it's only getIsUp. Let's go into the Moles init method right under where we run the super init method to initialize Sprite. And in here we'll set upTime equal to 2 .0f and we'll set isUp equal to false, and then we'll set the display frame of the mole, so this->setDisplayFrame.

The DisplayFrame is the Sprite frame that will show for that that particular Sprite. So we'll set the DisplayFrame to CCSpriteFrameCache:: sharedSpriteFrameCache->spriteFrameByName, and the name is going to be the string a0001.png; save the file and go over to game.cpp. In the initializeGame method, at the top we're going to initialize the moles array; we'll set it equal to a CCArray create.

This will create an auto released CCArray; that means it will be deleted from memory if we don't choose to retain it. So let's delete those extra parentheses and go to the next line, and make sure to retain the moles array. So remember if you ever called retain on the object in cocos2d, make sure you also call an accompanying release like we did in onExit in this case. Go down a couple of lines, and then we're going to create a Mole. So we'll create mole called Mole set it equal to type cast it Mole, Mole::create.

Now we're creating the mole just so we can get its size data, and then we're going to delete it afterwards. So we'll create a float called hPad and we're going to set this equal to Utils::s. It give us the size of the screen .width/2 minus in parentheses mole->getContentSize.width times Utils getScale and after parentheses we're going to multiply that by 3.5f.

That number comes from the first position of the first mole, so we're taking the middle of the screen; we're shifting it to the left by 3.5 moles, relative to their scale. Let's go to the next line and we'll do the same thing for vertical padding. So we'll call this vPad and we'll set it equal to Utils::s.height/2 minus mole, getContentSize.height times Utils:: getScale, we'll multiply that by 2.1.

Now we don't actually want this to be in the center of the screen, I just made it shifted up and that's where that .1 comes in. Now go to the next line and just call delete and delete the mole. Now we'll create a nested for loop to create the moles. So in the outer loop create an integer called i, set it equal to 1. I'll run this as long as i is less than or equal to 4; and of course, i++, and the same thing for the inner loop except for we'll use j for the iterator. So integer called j equals 1, as long as j is less then or equal to 6, then j++.

We're going to have a total of 24 moles. Now in the loop, create a Mole, we'll call it mole and we'll set it equal to again a typecasted mole, run Mole:: create, and on the next line we'll set the scale of the mole, so mole->setScale and we will pass in Utils::getScale, and then on the next line we're going to set the position of the mole. So mole->setPosition pass in a ccp, remember that short for a CCPoint, and we passed in an XY coordinates.

So the x-coordinate is going to be j times mole->getContentSize.width times Utils::getScale+hPad. Now we'll do the same thing for the y position. So it's going to be i times mole->getContentSize.height times Utils::getScale+vPad, and then on the next line we're going to add the mole to the moles array, so moles->addObject, pass in mole, and then finally we'll have the gameLayer, add the mole as a child, so this->addChild mole z of 1.

So now you should be able to save and test this in the Emulator and once you click the Play button from the Main menu, you should see your screen look like this and all the moles are laid out and centered on the screen.

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 · 11747 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 preferences from 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.

Learn more, save more. Upgrade today!

Get our Annual Premium Membership at our best savings yet.

Upgrade to our Annual Premium Membership today and get even more value from your lynda.com subscription:

“In a way, I feel like you are rooting for me. Like you are really invested in my experience, and want me to get as much out of these courses as possible this is the best place to start on your journey to learning new material.”— Nadine H.

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.