Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Now that we can handle tapping the moles, we will look at giving the player a point each time the player taps on a mole. So go into ccTouchesBegan and inside of the CCRectContainsPoint if statement right under mole->wasTapped let's tell the game that the players scored. We will do that by communicating to the hudLayer. Utils::hudLayer()->didScore. Save the file and go over to HUD.h. Here are the properties and methods for the HUD class. Let's go into HUD.cpp and you will see that it's pretty much blank.
In the init method, we will set score to 0 and we will create a float called padding and set it equal to 10, and that we will define the score label. scoreLabel = CCLabelTTF::create and we will start by passing in the string 0, it's what's going to be inside of the text field. Next parameter is going to be a string, so CCString and we will run it's createWithFormat method, and first we will pass in %s and then .ttf. And we are going to grab that font after the Comma (,) by using our constant value FONT_MAIN.
After that close parentheses, we will run getCString and then a comma after that and the last parameter is the size of the text field. So it's going to be 24*Utils::getArtScaleFactor. Go to the next line; here we are going to set the anchor point, that's the XY origin. scoreLabel->setAnchorPoint and this is based on a percentage, so if you want to have it be in the center it's 0.5, 0.5.
So there is the X origin and the Y origin that we pass in as the ccp. What we wanted to do is stick at the top left of the screen, so we are going to use a top left XY origin. So X origin of 0 which represents the left edge of the object and a Y origin of one, which represents the top edge. Now we do this because we have less math to calculate if we give it a registration point that's appropriate for its position. Let's go down to the next line and set the scale.
So scoreLabel->setScale and then we pass in Utils::getScale. On the next line we are going to set its position. So scoreLabel->setPosition and the position is going to be ccp of course, and the X position is going to be padding. Remember we have that left edge of the stage as a registration point, so it's going to be 10 points from the left edge of the screen and then for its Y position, we are going to get the height of the screen, so Utils::s().height-padding.
So it will be 10 points from the top of the screen. Now we want to add it as a child of the hud, so this->addChild, pass in scoreLabel and then the Z position which we will set at 1, and then scroll down to didScore; remember this is going to run once the mole was tapped as we defined in the game class. So we are just going to increment score by 1 and then set the text to the scoreLabel. So scoreLabel->setString and then we are going to pass in the string which is going to be a formatted CCString.
So CCString::createWithFormat and that's going to be just the number of the score. So %d and then after the close quotes of the string, score after a comma and then we are going to run getCString and that's it, just pass in the score of the string, it's going to increment every time we tap a mole, so hit Save and test this out in the Emulator. So now I will run the app in the Emulator and when I hit Play you will see the moles moving up and down as they should and when I click on them, you will see the score going up.
Now I will go back into the code, and now we have a system to calculate the player's progress as they play through the game.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 101837 Viewers
61 Video lessons · 88587 Viewers
71 Video lessons · 72405 Viewers
56 Video lessons · 104101 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.
Your file was successfully uploaded.