2014-05-03 76 views
2

我想子類CCSprite爲了添加拖動功能在精靈在COCOS2D-X,這就是爲什麼我想用它CCTouchDelegate如何在cocos2dx中使用CCTargetedTouchDelegate?

但我得到的錯誤是關於重新定義委託方法。

好......代表方法是要重新定義的,不是嗎?

我想我一定在某處做了某些事情,但是找不到它。

我的代碼如下。

.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 繼承即我的課的語法是 類DragSprite:公衆的cocos2d :: CCSprite,公衆的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) 
{ 

} 

所需的代表是否仍會被調用?

因爲我可以看到錯誤消失了,如果我這樣做。

+2

而不是screenshotting你的代碼,請在這裏複製並粘貼相關部分。這會讓我們更容易幫助你。 – computerfreaker

+0

我在Cocos2d-x論壇上回復了您的帖子。 – GameDeveloper

回答

1

如果您想要覆蓋DragSprite類中的方法,則需要使用該類的前綴。

而不是重新實現類的cocos2d的命名空間/類:

void cocos2d::CCTargetedTouchDel.... 

需要實現的方法在自己的類,像這樣:

void DragSprite::CCTargetedTouchDel.... 
+0

不能正常工作..我用bool DragSprite :: CCTargetedTouchDelegate :: ccTouchBegan(cocos2d :: CCTouch * pTouch,cocos2d :: CCEvent * pEvent)替換了我的.cpp文件中的方法,但它仍然給出了相同的錯誤 –

+0

我也改變了類語法FROM「class DragSprite:public cocos2d :: CCSprite,cocos2d :: CCTargetedTouchDelegate」TO「class DragSprite:public cocos2d :: CCSprite,public cocos2d :: CCTargetedTouchDelegatecocos2d :: CCTargetedTouchDelegate」 –

+0

我更新了我的問題,請看一看 –