如何在cocos2dx中使用CCTargetedTouchDelegate?

时间:2014-05-03 08:05:56

标签: c++ ios cocos2d-x

我想继承CCSprite,以便在COCOS2D-X,中为精灵添加拖动功能,这就是为什么我想用它CCTouchDelegate

但我得到的错误是重新定义委托方法。

嗯......委托方法是要重新定义的,不是吗?

我想我肯定在某个地方做错了什么但是找不到它。

我的代码如下。

.h文件

.h file

namespace mygames
{

    class DragSprite: public cocos2d::CCSprite, cocos2d::CCTargetedTouchDelegate
    {
        public:
        DragSprite* createWithFile(const char *pszFileName);
        bool isTouchingOnSprite(cocos2d::CCPoint  touch);

        private :
        bool isDrag;
        cocos2d::CCPoint whereTouch;
    };
}

.cpp文件

enter image description here

#include "DragSprite.h"
using namespace mygames;

DragSprite* DragSprite::createWithFile(const char *pszFileName)
{
    DragSprite *pSprite = new DragSprite();
    if (pSprite&&pSprite->initWithFile(pszFileName))
    {
    cocos2d::CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(pSprite, 0, true);
        pSprite->autorelease();
        return pSprite;
    }
    CC_SAFE_DELETE(pSprite);
    return NULL;
    }

    bool DragSprite::isTouchingOnSprite(cocos2d::CCPoint  touch)
    {

        if (this->boundingBox().containsPoint(touch)) {
            return true;
        }
        else
        {
        return false;
        }

    }
    bool cocos2d::CCTargetedTouchDelegate::ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
    {

    }
    void cocos2d::CCTargetedTouchDelegate::ccTouchMoved(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
    {

    }
    void cocos2d::CCTargetedTouchDelegate::ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
    {

    }
    void cocos2d::CCTargetedTouchDelegate::ccTouchCancelled(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
    {

    }

CCTargetedTouchDelegate类

CCTargetedTouchDelegate

class CC_DLL CCTargetedTouchDelegate : public CCTouchDelegate
{
    public:
     /** Return YES to claim the touch.
      @since v0
     */
     virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent) { CC_UNUSED_PARAM(pTouch); CC_UNUSED_PARAM(pEvent);return false;};

     // optional
     virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent) {CC_UNUSED_PARAM(pTouch); CC_UNUSED_PARAM(pEvent);}
     virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent) {CC_UNUSED_PARAM(pTouch); CC_UNUSED_PARAM(pEvent);}
     virtual void ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent) {CC_UNUSED_PARAM(pTouch); CC_UNUSED_PARAM(pEvent);}
 };

CCTouchDelegate类

CCTouchDelegate

class CC_DLL CCTouchDelegate
{
    public:

    CCTouchDelegate() {}

    virtual ~CCTouchDelegate()
    {
    }

    virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent) {CC_UNUSED_PARAM(pTouch); CC_UNUSED_PARAM(pEvent); return false;};
// optional

    virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent) {CC_UNUSED_PARAM(pTouch); CC_UNUSED_PARAM(pEvent);}
    virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent) {CC_UNUSED_PARAM(pTouch); CC_UNUSED_PARAM(pEvent);}
    virtual void ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent) {CC_UNUSED_PARAM(pTouch); CC_UNUSED_PARAM(pEvent);}

    // optional
     virtual void ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent) {CC_UNUSED_PARAM(pTouches); CC_UNUSED_PARAM(pEvent);}
     virtual void ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent) {CC_UNUSED_PARAM(pTouches); CC_UNUSED_PARAM(pEvent);}
     virtual void ccTouchesEnded(CCSet *pTouches, CCEvent *pEvent) {CC_UNUSED_PARAM(pTouches); CC_UNUSED_PARAM(pEvent);}
     virtual void ccTouchesCancelled(CCSet *pTouches, CCEvent *pEvent) {CC_UNUSED_PARAM(pTouches); CC_UNUSED_PARAM(pEvent);}

};

刚刚好奇: -

因为我已经从CCTargetedTouchDelegate继承了 即我的类语法是     class DragSprite:public cocos2d :: CCSprite,public cocos2d :: CCTargetedTouchDelegate     {     }

我是否仍需要在错误生成代码中使用范围解析运算符 即

bool cocos2d::CCTargetedTouchDelegate::ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
{

}
void cocos2d::CCTargetedTouchDelegate::ccTouchMoved(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
{

}
void cocos2d::CCTargetedTouchDelegate::ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
{

}
void cocos2d::CCTargetedTouchDelegate::ccTouchCancelled(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
{

}

我不能像下面那样写吗?会有什么不同吗?

bool ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
{

}
void ccTouchMoved(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
{

}
void ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
{

}
void ccTouchCancelled(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
{

}

是否仍会要求所需的代表?

因为我可以看到错误消失,如果我这样做。

1 个答案:

答案 0 :(得分:1)

如果要覆盖DragSprite类中的方法,则需要使用该类的前缀。

而不是在cocos2d namespace / class中重新实现类:

void cocos2d::CCTargetedTouchDel....

你需要在你自己的类中实现这样的方法:

void DragSprite::CCTargetedTouchDel....