O'Reilly logo

Cocos2d Cross-Platform Game Development Cookbook - Second Edition by Siddharth Shekar

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Moving objects with touchMoved

We will now move the object once the object is created in the touchBegan function.

Getting ready

Make the hero sprite a global variable instead of local by going to the MainScene.h file and adding an instance of it, as follows:

@interface MainScene :CCNode{

CCSprite* hero;

How to do it…

In the MainScene.m file, we will modify the touchBegan and touchMoved functions, as follows:

- (void)touchBegan:(CCTouch *)touch withEvent:(CCTouchEvent *)event { CCLOG(@"TOUCHES BEGAN"); CGPointtouchLocation = [touch locationInNode:self]; hero = [CCSpritespriteWithImageNamed:@"hero.png"]; [selfaddChild:hero]; hero.position = touchLocation; } - (void)touchMoved:(CCTouch *)touch withEvent:(CCTouchEvent *)event { CCLOG(@"TOUCHES MOVED"); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required