我试图搜索但找不到所需的答案,谁能告诉我 CCNode 的 CCTouch 事件是什么?因为我们有 CCTouchBegan、CCTouchMoved 和 CCTouchEnded 用于 CCLayer
4 回答
CCLayer 是 CCNode 的子类,因此您可以使用所有相同的功能;
像这样的东西
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 的子类也可以接收触摸事件。
假设您的子类名称是 MyNode。它必须实施——
CCTouchOneByOneDelegate 方法接收单点触摸事件。
CCTouchAllAtOnceDelegate 接收多点触控事件
注意:要添加 CCNode 的 Touch-Enabled 子类的层,在向触摸调度程序注册该层时不应吞下触摸。
类接口:
#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;
类实现:
#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];
}
如果MyNode需要实现多点触控事件,实现CCTouchAllAtOnceDelegate的委托方法---
////////////////////////////////////////////////////////////////
-(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
{
}
您必须与 CCNode 一起继承 CCTouchDeligate 类 查看 CCLayer.cpp 中的 CCLayer::registerWithTouchDispatcher() 函数您可以将 CCNode 添加到 CCTouchDispatcher
CCTouchDispatcher* pDispatcher = CCDirector::sharedDirector()->getTouchDispatcher();
pDispatcher->addStandardDelegate(this, 0);
完成此操作后,您将收到回调
void ccTouchesBegan(...), ccTouchesMoved(...), ccTouchesEnded(...)
CCNode 无法检测到触摸事件。触摸事件仅由 CCLayer 检测,CCLayer 继承自 CCNode,因此它具有 CCNode 的所有属性和额外的检测触摸事件的功能。
你可以查看我的博客http://www.touchscreenstudio.com/,这是一个新开始的博客,我将逐篇介绍所有 cocos2d-x 的内容。