Book Image

Learning Cocos2d-x Game Development

By : Siddharth Shekar
Book Image

Learning Cocos2d-x Game Development

By: Siddharth Shekar

Overview of this book

Table of Contents (19 chapters)
Learning Cocos2d-x Game Development
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Movement with touches


Now, using touch, we move the player to the touched location on the screen.

To do this, we use CCActions. CCActions are predefined classes of Cocos2d-x that can be used to perform various functions on an object over a period of time.

We can also combine multiple actions together to perform actions one after the other using CCSequence. Let's first see how to make the player move from the initial position to the desired touch location. Also, to ensure that the character doesn't simply go from point A to B, we add an easing action that will slowly increase the speed of the character when starting. When reaching the destination, it will slowly decrease the speed and bring the character to a halt.

To do this, we have to get the location of the touches on the screen. So, in the TouchesBegan() function, we add the following lines of code:

    CCTouch *touch =(CCTouch*)pTouches->anyObject();

    CCPoint location = touch->getLocationInView();
    location = CCDirector::sharedDirector...