Book Image

Cocos2d Game Development Essentials

Book Image

Cocos2d Game Development Essentials

Overview of this book

Table of Contents (13 chapters)

Detecting touches


In Cocos2D, every CCNode class and subclass can receive and handle touches. You just have to enable one property. This property is the userInteractionEnabled property and is written as follows:

- (id)init
{
    if (self = [super init])
    {
        // activate touches on this scene
        self.userInteractionEnabled = YES;
    }
    return self;
}

Enabling this property registers your node with the touch dispatcher. There are four types of touch events. These occur:

  • When touches begin

  • When touches end

  • When touches move

  • When touches are cancelled

Using these events allows you to track any touch as it moves around the screen. These events are passed to your node by implementing the touch delegate methods:

 (void)touchBegan:(UITouch *)touch withEvent:(UIEvent *)event(void)touchMoved:(UITouch *)touch withEvent:(UIEvent *)event(void)touchEnded:(UITouch *)touch withEvent:(UIEvent *)event
(void)touchCancelled:(UITouch *)touch withEvent:(UIEvent *)event

Multitouch can be enabled by setting...