Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
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.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 99477 Viewers
56 Video lessons · 112710 Viewers
71 Video lessons · 81525 Viewers
131 Video lessons · 39136 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.