Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Now we add interactivity to our game and CCTouchesBegan. Here is the plan, we're going to grab the touch information, including the location information, loop through all of the moles and see if they touch touched one of the moles. If so, we're going to send a message to the Mole that it was tapped and handle it from there. So, inside CCTouchesBegan in the game class, reference to CCTouch, we'll call it *touch, and we're going to set it = typecasted (CCTouch *), that's going to pTouches any object.
Now a pTouches is a CCSet, which is similar to an array, it's a set of objects and so there are multiple touches inside of pTouches. This method is also called CCTouchesBegan implying that multiple touches could happen at one time. You can use this to handle multi-touch, but on Android I've seen it work very sporadically. It worked fine for me on iOS where I like to just loop through the CCSet, but on Android, multi touches will only work once in a while. So I chose not to use it in this case.
If you find a very effective way to handle multi-touch on android with Cocos2d-X, let me know on Twitter. Let's go to the next line and your CCpoint, this is going to be the location, and we are going to set it =touch-> locationInView. And then we are going to set location again, and this is going to be CCDirector::sharedDirector()-> convertToGL and then just pass in a (location). Now this is going to be very familiar, since we already handled Touches in the exact same way.
So now what we are going to do is loop through all of the moles, and to see if
this location overlaps any of the bounding boxes of the moles, and if so, we
will just tell that mole that it was tapped.
So, I create a for Loop, standard here, (int i=0) we are going to loop through
the count of the moles.
So, as long as (i)
So, we are going to have to typecast it, because it returns CCObject for each object in the array. So, it's going to be moles-> objectAtIndex pass in (i )and so there' is our mole. Now we want to see if the mole is up, so we're going to see if the mole is not up, and if it's not up, we are just going to continue to the next mole. So, mole->getIsUp, so I continue instead of that if () statement. And now we are going to create another if() statement. So we know now that the mole IsUp and we want to check to see if the touch is inside of the mole's bounding box and you may remember how we do that before.
So, we just do (CCRect:: CCRectContainsPoint and we passed in the rectangle, which is going to be the bounding box of the (mole). So, (mole->boundingBox) and the next parameter is the location. And then in here we just tell the mole that it was tapped and then we don't need to go through any other moles since we already have one, so we'll just break out of the loop. And now I am interested to find what happens on the mole side when it's tapped. So save and jump over to Mole.cpp and we will handle that there, scroll down in the Mole class to what is tapped, should be empty.
And in here what we going to do is just stop all the molesActions(), and then we'll run the animated actions have the mole animate down. So this run action we want to pass in (Utils::getAnimationWithFrames), when we pass in 21 and 31 that's the mole hit animation so it's going to turn red. And then finally, we are going to set isUp = false, so the mole is no longer counted as up, once it's been tapped. So, let's save the file and just test it out in the emulator.
So now hit play, and you'll see that Moles pop up and then I can tap them and they play a little animation and go down, excellent! So, as expected, when you tap on the Mole, the Mole animation plays and the Mole goes back down and has the opportunity come back up again, and now our game finally feels like a game.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 101639 Viewers
61 Video lessons · 88392 Viewers
71 Video lessons · 72231 Viewers
56 Video lessons · 103976 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.