9

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.

Pawan Joshi
  • 1,581
  • 3
  • 20
  • 40

3 Answers3

22
bool ccTouchBegan(

needs to be

bool DragSprite::ccTouchBegan(

You shouldnt need this in the first place.

Karthik T
  • 31,456
  • 5
  • 68
  • 87
  • its the method declared in CCTargetedTouchDelegete. and i guess its an abstract function. So, if i do as you suggested? Would it still call the delegete function ? or just the function from my DragSprite class. I mean, would the function be still overridden ? – Pawan Joshi May 05 '14 at 06:33
  • 1
    @DemonSOCKET hmm good question, I think your porting is missing one more element. In C++ virtual functions are opt-in, unlike obj-c. You want to use `virtual` to make this method a "virtual" function, then it will work as in obj-c. http://en.wikipedia.org/wiki/Virtual_function for more info. Just add `virtual` before the function definition – Karthik T May 05 '14 at 07:21
  • OK.. Well, since it is already declared in CCTargetedTouchDelegate class from which i am inheriting, as of like the following virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent) { CC_UNUSED_PARAM(pTouch); CC_UNUSED_PARAM(pEvent); return false; }; Do i still need to declare this in .h file ? if yes then with or without virtual keyword ? – Pawan Joshi May 05 '14 at 07:52
  • @DemonSOCKET the `virtual` needs to be in the class definition, in the `.h` file – Karthik T May 05 '14 at 07:55
0

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.

Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
  • its the method declared in CCTargetedTouchDelegete. and i guess its an abstract function. So, if i do as you suggested? Would it still call the delegete function ? or just the function from my DragSprite class. I mean, would the function be still overridden ? – Pawan Joshi May 05 '14 at 06:46
  • I'd answer this question, but since you've also specifically asked the other two people who answered your question also I'll give them a shot first. :-) – Michael Dautermann May 05 '14 at 06:50
  • well... i am not getting any satisfactory answer with those comments. so would you please help me out here ? – Pawan Joshi May 05 '14 at 07:02
0

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 () ;
user3344003
  • 20,574
  • 3
  • 26
  • 62
  • its the method declared in CCTargetedTouchDelegete. and i guess its an abstract function. So, if i do as you suggested? Would it still call the delegete function ? or just the function from my DragSprite class. I mean, would the function be still overridden ? – Pawan Joshi May 05 '14 at 06:43
  • The next question is what are you porting? Is the code calling the delegate going to be in C++ too? If it's objective C, that's going to be a problem. – user3344003 May 05 '14 at 06:49
  • well.. no its not in the objective-C. by porting i mean i am just using the logic here. because cocos2d-x and cocos2d-iPhone are mostly same. Apologies, if i caused you any confusions. – Pawan Joshi May 05 '14 at 06:55
  • also .. i am very much new to cocos2dx and c++. so just for a hint, is it a must to declare any virtual function(not pure) of base class in .h file too ? – Pawan Joshi May 05 '14 at 07:00
  • Yes. This is another difference between objective C and C++. It's larger. All member functions must be declared in the header. Objective C allows members to be defined in the body without a declaration. – user3344003 May 05 '14 at 07:11