CCResponder Class Reference

Inherits from NSObject
Declared in CCResponder.h

Overview

CCResponder is the base class for all nodes. It exposes the touch and mouse interface to any node, which enables user interaction. It is somewhat similar to UIResponder.

To make a responder react to user interaction, the touchesXXX / mouseXXX event must be overridden in your node subclass. If a class does not implement these selectors, the event will be passed on to the next node in the Cocos2D responder chain. To force the events to be passed to next responder, call the super implementation before returning from the event.

Subclassing Notes

You should not create subclasses of CCResponder. Instead subclass from CCNode if you need a plain basic node, or other node classes that best fit the purpose.

Note: To handle touch events, the touchBegan method must always be overridden (implemented) in your node’s subclass. It may remain empty. Otherwise the other touch events will not be received either if touchBegan is not implemented.

Enabling Input Events

  userInteractionEnabled

Enables user interaction on a node.

@property (nonatomic, assign, getter=isUserInteractionEnabled) BOOL userInteractionEnabled

Declared In

CCResponder.h

  multipleTouchEnabled

Enables multiple touches inside a single node.

@property (nonatomic, assign, getter=isMultipleTouchEnabled) BOOL multipleTouchEnabled

Declared In

CCResponder.h

Customize Event Behavior

  claimsUserInteraction

Continues to send touch events to the node that received the initial touchBegan, even when the touch has subsequently moved outside the node.

@property (nonatomic, assign) BOOL claimsUserInteraction

Discussion

If set to NO, touches will be cancelled if they move outside the node’s area. And if touches begin outside the node but subsequently move onto the node, the node will receive the touchBegan event.

Declared In

CCResponder.h

  exclusiveTouch

All other touches will be cancelled / ignored if a node with exclusive touch is active. Only one exclusive touch node can be active at a time.

@property (nonatomic, assign, getter=isExclusiveTouch) BOOL exclusiveTouch

Declared In

CCResponder.h

  hitAreaExpansion

Expands (or contracts) the hit area of the node. The expansion is calculated as a margin around the sprite, in points.

@property (nonatomic, assign) float hitAreaExpansion

Declared In

CCResponder.h

Performing Hit Tests

– hitTestWithWorldPos:

Check if a touch is inside the node. To allow custom touch detection, override this method in your node subclass.

- (BOOL)hitTestWithWorldPos:(CGPoint)pos

Parameters

pos

Position in scene (world) coordinates.

Return Value

Returns true if the position is inside the node.

Declared In

CCResponder.h

Touch Handling Methods

– touchBegan:withEvent:

Called when a touch began. Behavior notes:

- (void)touchBegan:(CCTouch *)touch withEvent:(CCTouchEvent *)event

Parameters

touch

Contains the touch.

event

Current event information.

Discussion

  • If a touch is dragged inside a node which does not claim user interaction, a touchBegan event will be generated.
  • If node has exclusive touch, all other ongoing touches will be cancelled.
  • If a node wants to handle any touch event, the touchBegan method must be overridden in the node subclass. Overriding just touchMoved or touchEnded does not suffice.
  • To pass the touch further down the Cocos2D responder chain, call the super implementation, ie [super touchBegan:withEvent:].

Declared In

CCResponder.h

– touchMoved:withEvent:

Called whan a touch moved.

- (void)touchMoved:(CCTouch *)touch withEvent:(CCTouchEvent *)event

Parameters

touch

Contains the touch.

event

Current event information.

Declared In

CCResponder.h

– touchEnded:withEvent:

Called when a touch ended.

- (void)touchEnded:(CCTouch *)touch withEvent:(CCTouchEvent *)event

Parameters

touch

Contains the touch.

event

Current event information.

Declared In

CCResponder.h

– touchCancelled:withEvent:

Called when a touch was cancelled.

- (void)touchCancelled:(CCTouch *)touch withEvent:(CCTouchEvent *)event

Parameters

touch

Contains the touch.

event

Current event information.

Discussion

If a touch is dragged outside a node which does not claim user interaction, touchCancelled will be called. If another node with exclusive touch is activated, touchCancelled will be called for all ongoing touches.

Declared In

CCResponder.h

Mouse Handling Methods

– mouseDown:

Called when left mouse button is pressed inside a node with userInteractionEnabled set to YES.

- (void)mouseDown:(NSEvent *)theEvent

Parameters

theEvent

The event created.

See Also

Declared In

CCResponder.h

– mouseDragged:

Called when left mouse button is held and mouse dragged for a node with userInteractionEnabled set to YES.

- (void)mouseDragged:(NSEvent *)theEvent

Parameters

theEvent

The event created.

See Also

Declared In

CCResponder.h

– mouseUp:

Called when left mouse button is released for a node with userInteractionEnabled set to YES.

- (void)mouseUp:(NSEvent *)theEvent

Parameters

theEvent

The event created.

See Also

Declared In

CCResponder.h

– rightMouseDown:

Called when right mouse button is pressed inside a node with userInteractionEnabled set to YES.

- (void)rightMouseDown:(NSEvent *)theEvent

Parameters

theEvent

The event created.

See Also

Declared In

CCResponder.h

– rightMouseDragged:

Called when right mouse button is held and mouse dragged for a node with userInteractionEnabled set to YES.

- (void)rightMouseDragged:(NSEvent *)theEvent

Parameters

theEvent

The event created.

See Also

Declared In

CCResponder.h

– rightMouseUp:

Called when right mouse button is released for a node with userInteractionEnabled set to YES.

- (void)rightMouseUp:(NSEvent *)theEvent

Parameters

theEvent

The event created.

See Also

Declared In

CCResponder.h

– otherMouseDown:

Called when middle mouse button is pressed inside a node with userInteractionEnabled set to YES.

- (void)otherMouseDown:(NSEvent *)theEvent

Parameters

theEvent

The event created.

See Also

Declared In

CCResponder.h

– otherMouseDragged:

Called when middle mouse button is held and mouse dragged for a node with userInteractionEnabled set to YES.

- (void)otherMouseDragged:(NSEvent *)theEvent

Parameters

theEvent

The event created.

See Also

Declared In

CCResponder.h

– otherMouseUp:

Called when middle mouse button is released for a node with userInteractionEnabled set to YES.

- (void)otherMouseUp:(NSEvent *)theEvent

Parameters

theEvent

The event created.

See Also

Declared In

CCResponder.h

– scrollWheel:

Called when scroll wheel moved while mouse cursor is inside a node with userInteractionEnabled set to YES.

- (void)scrollWheel:(NSEvent *)theEvent

Parameters

theEvent

The event created.

See Also

Declared In

CCResponder.h