sharvey
sharvey

Reputation: 8145

Cocos2d iphone touch event

Is there a way to detect touch events from another class than the Layer itself. I'm trying to have a state controlling the interaction, adding listener to the layer itself if needed instead of have the layer call a function on the current state, which might be a noop. Is the a way to use such a thing?

Upvotes: 1

Views: 566

Answers (1)

pgb
pgb

Reputation: 25001

See the class CCTouchDispatcher (http://www.cocos2d-iphone.org/api-ref/latest-stable/interface_c_c_touch_dispatcher.html)

You can subscribe any class that implements CCStandardTouchDelegate or CCTargetedTouchDelegate to receive touch events, by calling:

[[CCTouchDispatcher sharedDispatcher] addStandardDelegate:self priority:1];

on your onEnter method (assuming your class is a CCNode), and then remove it from the dispatcher via:

[[CCTouchDispatcher sharedDispatcher] removeDelegate:self];

at your onExit method.

Upvotes: 2

Related Questions