Tuesday, August 23, 2011

Adding Touch Events to a Custom Cocos2d Sprite

In creating our game Word Crank, we decided that we would make the clickable block objects handle the touch events themselves instead of using the CCScene object. In order to do that we had to implement the CCTargetedTouchDelegate.  A sample of the code can be found below:

Inside the Header File:


[sourcecode language="objc"]
@interface CustomSprite : CCSprite <CCTargetedTouchDelegate>
[/sourcecode]

Inside the Implementation File:


[sourcecode language="objc"]

- (void) onEnterTransitionDidFinish
{
[[CCTouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:1 swallowsTouches:YES];
}

- (void) onExit
{
[[CCTouchDispatcher sharedDispatcher] removeDelegate:self];
}
[/sourcecode]

No comments:

Post a Comment