검색을 시도했지만 원하는 답을 찾을 수 없었습니다. CCNode에 대한 CCTouch 이벤트는 누구에게 말해 줄 수 있습니까? CCTouchBegan, CCTouchMoved 및 CCTouchEnded for CCLayerCCNode for cocos2d-X
답변
CCNode 같은
뭔가 터치 이벤트를 감지 할 수 없습니다. 터치 이벤트는 CCLayer에서만 감지되며 CCLayer는 CCNode에서 상속되므로 CCNode의 모든 속성과 터치 이벤트를 감지하는 추가 기능이 있습니다.
당신은 내 블로그 http://www.touchscreenstudio.com/를 확인할 수 있습니다, 그것은 블로그를 새로 시작했고 나는 모든 게시물을 게시하여 모든 cocos2d-x 것들을 덮을 것입니다.
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");
}
당신은 당신이 당신 것 이런 짓을하면 당신은 CCTouchDispatcher
CCTouchDispatcher* pDispatcher = CCDirector::sharedDirector()->getTouchDispatcher();
pDispatcher->addStandardDelegate(this, 0);
에 CCNode CCLayer :: registerWithTouchDispatcher() 당신은 추가 할 수 있습니다 CCLayer.cpp 의 기능에 CCNode 봐와 함께 CCTouchDeligate 클래스를 상속해야 또한 터치 이벤트를 수신 할 수 CCNode의
void ccTouchesBegan(...), ccTouchesMoved(...), ccTouchesEnded(...)
아이 클래스에 콜백을 얻는다.
자녀 클래스 이름이 MyNode라고 말하십시오. 은 싱글 터치 이벤트를 수신 할
CCTouchOneByOneDelegate 방법을 implement--한다. 에
CCTouchAllAtOnceDelegate 멀티 터치 이벤트를
주를 수신 : 터치 디스패처이 계층을 등록하는 동안 당신은 CCNode의 터치 지원 서브 클래스를 추가하려고하는 층은 터치를 삼키지한다.
클래스 인터페이스 :
#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
{
}
은 아무도 추천 할 수 나 사람들이 cocos2d-x에 대해 알고있는 웹 사이트의 블로그? – Zubair