I tried to search but couldn't find the desired answer, can anybody tell me what are the CCTouch events for CCNode? As we have CCTouchBegan, CCTouchMoved and CCTouchEnded for CCLayer
The CCTouch events for CCNode in cocos2d-X
2.1k views Asked by Zubair AtThere are 4 answers
A child class of CCNode can also receive touch events.
Say your child class name is MyNode. It has to implement--
CCTouchOneByOneDelegate methods to receive single-touch events.
CCTouchAllAtOnceDelegate to receives multi-touch events
Note:The layer you are going to add this Touch-Enabled subclass of CCNode,should not swallow touch while registering this layer with touch dispatcher.
Class Interface :
#import <Foundation/Foundation.h>
#import "cocos2d.h"
#import "CCProtocols.h"
@interface MyNode : CCNode <CCTouchOneByOneDelegate,CCTouchDelegate>//Implementing only for single touch events
{
@private CGSize winSize;
}
+(MyNode*) addMyNodeToParentClass:(CCNode*)parent;
Class Implementation:
#import "SettingsMenu.h"
@implementation SettingsMenu
+(MyNode*) addMyNodeToParentClass:(CCNode*)parent;
{
return [[self alloc]initWithParentNode:parent];
}
-(id)initWithParentNode:(CCNode*)parent
{
if(self=[super init])
{
winSize=[CCDirector sharedDirector].winSize;
//Registering MyNode with the TouchDispatcher
[self registerWithTouchDispatcher];
//adding a single sprite to check touch events
CCSprite *sprite=[CCSprite spriteWithFile:@"information.png"];
infoButton.position=ccp(winSize.width/2,winSize.height/2);
[self addChild:infoButton];
//adding this node to the parent node
[parent addChild:self];
}
return self;
}
#pragma function registering with Touch Dispatcher
-(void)registerWithTouchDispatcher
{
[[CCDirector sharedDirector].touchDispatcher addTargetedDelegate:self priority:INT_MIN+1 swallowsTouches:YES];//if any other node needs this touch do not swallow this touch
}
#pragma -CCTouchOneByOne delegate methods
-(BOOL)ccTouchBegan:(UITouch *)touch withEvent:(UIEvent *)event
{
CGPoint touchPoint=[[CCDirector sharedDirector] convertTouchToGL:touch];
if(CGRectContainsPoint(infoButton.boundingBox, touchPoint))
{
printf("\nTouch received on information button");
}
return YES;
}
-(void)ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
{
CGPoint movedTouchPoint=[[CCDirector sharedDirector] convertTouchToGL:touch];
//your code to handle touch-movement
}
-(void)ccTouchEnded:(UITouch *)touch withEvent:(UIEvent *)event
{
CGPoint touchEndPoint=[[CCDirector sharedDirector] convertTouchToGL:touch];
//your code to handle touch-end
}
-(void)ccTouchCancelled:(UITouch *)touch withEvent:(UIEvent *)event
{
//handle event for incoming SMS,Call ect.
}
#pragma -method to remove Touch Dispatcher
-(void)removeTouchDispatcher
{
[[CCDirector sharedDirector].touchDispatcher removeDelegate:self];
}
If MyNode needs to implement multi-touch events implement CCTouchAllAtOnceDelegate's delegate methods---
////////////////////////////////////////////////////////////////
-(void)ccTouchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
}
-(void)ccTouchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
}
-(void)ccTouchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
}
-(void)ccTouchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
}
You have to Inherit CCTouchDeligate class along with CCNode Look at CCLayer::registerWithTouchDispatcher() function in CCLayer.cpp You can add you CCNode to the CCTouchDispatcher
CCTouchDispatcher* pDispatcher = CCDirector::sharedDirector()->getTouchDispatcher();
pDispatcher->addStandardDelegate(this, 0);
Once you have done this you will get callbacks on
void ccTouchesBegan(...), ccTouchesMoved(...), ccTouchesEnded(...)
CCLayer is subclass of CCNode so you can use all same functions ;
something like this
HelloWorldScene.h
virtual bool ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent);
virtual void ccTouchMoved(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent);
virtual void ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent);
virtual void ccTouchCancelled(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent);
HelloWorldScene.cpp
bool HelloWorld::ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent){
printf("ccTouchBegan");
return true;
}
void HelloWorld::ccTouchMoved(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent){
printf("ccTouchMoved");
}
void HelloWorld::ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent){
printf("ccTouchEnded");
}
void HelloWorld::ccTouchCancelled(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent){
printf("ccTouchCancelled");
}
CCNode can not detect the touch events. Touch events are only detected by CCLayer, CCLayer is inherited from CCNode, so it has all the properties of CCNode and extra functions to detect the touch event.
you can check my blog http://www.touchscreenstudio.com/ , it is newly started blog and i will be covering all cocos2d-x things post by post.