سؤال

I wanted to subclass CCSprite in order to add dragging functionality on sprites in COCOS2D-X, thats why i wanted to use CCTouchDelegate with it.

but the error i am getting is about redefinition of the delegate methods.

well.... the delegate method are meant to be redefined, aren't they?

I guess i must have done something wrong somewhere but couldn't find it.

My code is as follows.

.h file

.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 file

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 class

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 class

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

};

JUST OUT OF CURIOSITY :-

since i am already inheriting from CCTargetedTouchDelegate i.e my class syntax is class DragSprite: public cocos2d::CCSprite, public cocos2d::CCTargetedTouchDelegate { }

Do i still need to make use of scope resolution operator in the error generating code i.e

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)
{

}

can't i just write like the following ? is it gonna make any difference ?

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)
{

}

Are the required delegates still gonna be called ?

because i can see the errors gone , if i do this.

هل كانت مفيدة؟

المحلول

If you want to override the methods in your DragSprite class, you need to use that class' prefix.

Instead of re-implementing the classes in the cocos2d namespace/class:

void cocos2d::CCTargetedTouchDel....

you need to implement the methods in your own class like so:

void DragSprite::CCTargetedTouchDel....
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top