cocos2d-X中CCNode的CCTouch事件

时间:2013-07-19 11:13:07

标签: cocos2d-x ccnode

我试图搜索但找不到想要的答案,有人能告诉我CCNode的CCTouch事件是什么吗?我们有CCTouchBegan,CCTouchMoved和CCTouchEnded用于CCLayer

4 个答案:

答案 0 :(得分:1)

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"); 
}

答案 1 :(得分:1)

CCNode的子类也可以接收触摸事件。

假设您的子类名是MyNode。 它必须实现 -

  1. CCTouchOneByOneDelegate接收单点触控事件的方法。

  2. CCTouchAllAtOnceDelegate接收多点触控事件

  3. 注意:您要添加此触控启用的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
    {
    
    }
    

答案 2 :(得分:0)

您必须与CCNode一起继承CCTouchDeligate类 查看CCLayer.cpp中的CCLayer :: registerWithTouchDispatcher()函数 您可以将CCNode添加到CCTouchDispatcher

CCTouchDispatcher* pDispatcher = CCDirector::sharedDirector()->getTouchDispatcher();
pDispatcher->addStandardDelegate(this, 0);

完成此操作后,您将获得回调

void ccTouchesBegan(...), ccTouchesMoved(...), ccTouchesEnded(...)

答案 3 :(得分:-2)

CCNode无法检测到触摸事件。触摸事件仅由CCLayer检测,CCLayer继承自CCNode,因此它具有CCNode的所有属性和检测触摸事件的额外功能。

你可以查看我的博客http://www.touchscreenstudio.com/,它是新开始的博客,我将通过邮寄方式覆盖所有cocos2d-x的内容。