Question

I am using CCTouchTargetedDelegate with a Class subclassed by CCSprite. when defining delegate methods i am not being able to use "this" inside the functions.

as answered on previously asked questions I couldn't used name of class with the function using scope resolution , because it then gave me error of "Out-of-line definition of 'ccTouchBegan' does not match any declaration in 'mygames::DragSprite'"

I also tried to declare the function in .h file but nothing seems to work.

My code is as below :-

.h File

#pragma once
#include "cocos2d.h"




namespace mygames
{

    class DragSprite: public cocos2d::CCSprite, public cocos2d::CCTargetedTouchDelegate
    {
        public:
            DragSprite* createWithFile(const char *pszFileName);
            bool isTouchingOnSprite(cocos2d::CCPoint  touch);
            virtual bool init();
        bool ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent);
        static inline cocos2d::CCPoint ccpSub(const cocos2d::CCPoint v1, const cocos2d::CCPoint v2);
        private:
            bool isDrag;
            cocos2d::CCPoint whereTouch;
    };
}

.cpp File

#include "DragSprite.h"


using namespace mygames;


bool DragSprite::init()
{
    if (!CCSprite::init()) {
        return false;
    }

    whereTouch = cocos2d::CCPointZero;
    isDrag = false;

    return true;
}
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;
    }    

}
static inline cocos2d::CCPoint ccpSub(const cocos2d::CCPoint v1, const cocos2d::CCPoint v2)
{
    return ccp(v1.x-v2.x, v1.y-v2.y);

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

    cocos2d::CCPoint touchPoint = pTouch->getLocation();

    if (this->isTouchingOnSprite(touchPoint)) {
        this->whereTouch = ccpSub(this->position, touchPoint);
        return true;
    }
    return false;
}

The Error Screen Shot :-

What am i missing here ?

JUST OUT OF CURIOSITY

AS suggested in the answers, If i use

bool DragSprite::ccTouchBegan

So, Would it still call the delegete function ? or just the function from my DragSprite class. I mean, would the function be still overridden ? well... its the method declared in CCTargetedTouchDelegete. and i guess its an abstract function.

Was it helpful?

Solution

bool ccTouchBegan(

needs to be

bool DragSprite::ccTouchBegan(

You shouldnt need this in the first place.

OTHER TIPS

Why not define your function as

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

which is how you have it defined in the DragSprite class in your .h file.

This

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

defines a stand alone function. this can only be used within a class member function. To make it a class member as you have defined it, you need to qualify the name:

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

-=-=-=-=

I see everyone jumped in at once. The syntax of objective C is different. It uses

@implementation DragSprite
 . . . . 

@end 

to specify the class and requires a - to indicate a non static member function.

Another difference is that Objective C requires self referencing to call a member function

[self DragSprite] ;

C++ does not

DragSprite () ;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top