【问题标题】:The CCTouch events for CCNode in cocos2d-Xcocos2d-X中CCNode的CCTouch事件
【发布时间】:2013-07-19 11:13:07
【问题描述】:

我试图搜索但找不到想要的答案,谁能告诉我 CCNode 的 CCTouch 事件是什么?因为我们为 CCLayer 提供了 CCTouchBegan、CCTouchMoved 和 CCTouchEnded

【问题讨论】:

  • 谁能推荐我了解cocos2d-x的网站博客?

标签: cocos2d-x ccnode


【解决方案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"); 
}

【讨论】:

    【解决方案2】:

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

    假设您的子类名称是 MyNode。 它必须执行——

    1. CCTouchOneByOneDelegate 方法来接收单点触摸事件。

    2. 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
    {
    
    }
    

    【讨论】:

      【解决方案3】:

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

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

      完成此操作后,您将收到回调

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

      【讨论】:

        【解决方案4】:

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

        你可以查看我的博客http://www.touchscreenstudio.com/,这是一个新开的博客,我将逐个介绍所有cocos2d-x的东西。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-01-14
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-11-09
          相关资源
          最近更新 更多