cocos2d-X中CCNode的CCTouch事件
我试图搜索,但找不到想要的答案,有人能告诉我CCNode的CCTouch事件是什么吗?正如我们已经为CCLayer创建了cctouchStart、CCTouchMoved和cctouchEnd,CCLayer是CCNode的子类,所以您可以使用所有相同的函数 像这样的cocos2d-X中CCNode的CCTouch事件,cocos2d-x,ccnode,Cocos2d X,Ccnode,我试图搜索,但找不到想要的答案,有人能告诉我CCNode的CCTouch事件是什么吗?正如我们已经为CCLayer创建了cctouchStart、CCTouchMoved和cctouchEnd,CCLayer是CCNode的子类,所以您可以使用所有相同的函数 像这样的 HelloWorldScene.h virtual bool ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent); virtual void ccTo
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无法检测触摸事件。触摸事件仅由CCLayer检测,CCLayer继承自CCNode,因此它具有CCNode的所有属性和检测触摸事件的额外功能
您可以查看我的博客,这是一个新开始的博客,我将一篇一篇地介绍所有cocos2d-x内容。您必须继承CCTouchDeligate类和CCNode 查看CCLayer.cpp中的CCLayer::registerWithTouchDispatcher()函数 您可以将CCNode添加到CCTouchDispatcher
CCTouchDispatcher* pDispatcher = CCDirector::sharedDirector()->getTouchDispatcher();
pDispatcher->addStandardDelegate(this, 0);
完成此操作后,您将收到对的回调
void ccTouchesBegan(...), ccTouchesMoved(...), ccTouchesEnded(...)
CCNode的子类也可以接收触摸事件。 假设您的子类名称为MyNode。 它必须实现--
#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;
如果MyNode需要实现多点触控事件,请实现CCTouchAllAtOnceDelegate的委托方法---
有人能给我推荐一个大家都知道cocos2d-x的博客吗?
#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];
}
////////////////////////////////////////////////////////////////
-(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
{
}