Problems subclassing CCSprite

Hi
I’m trying to subclass CCSprite so I can detect touch.
I am however getting duplicate symbols errors.
Can anyone help me understand what I’m doing wrong pls???
Here is the code:
(.h)

#ifndef tictactoeTicTacToeBoard*_
#definetictactoeTicTacToeBoard*_

#include
#include “cocos2d.h”

using namespace cocos2d;

class TicTacToeBoard : public CCSprite, CCTargetedTouchDelegate
{

public:

//TicTacToeBoard(void);
//~TicTacToeBoard(void);

virtual void onEnter();
virtual void onExit();

/*
virtual bool ccTouchBegan(CCTouch* touch, CCEvent * event);
virtual void ccTouchMoved(CCTouch* touch, CCEvent * event);
virtual void ccTouchEnded(CCTouch* touch, CCEvent * event);
*/

};

#endif

here is the .cpp

#include “TicTacToeBoard.h”

void TicTacToeBoard::onEnter()
{
CCSprite::onEnter();
}

void TicTacToeBoard::onExit()
{
CCSprite::onExit();
}

You might need to change your header guards:

#ifndef tictactoeTicTacToeBoard
#define tictactoe
TicTacToeBoard

If you have this same guard someplace else then that is why

Also, people say it is best not to have your sprite detect the touch, but rather the layer and then take the touch coordinates and decide if a sprite was touched and then act upon it.

There is no need to subclass CCSprite, use CCMenu and CCMenuItemSprite instead, it can detect touches.

Hi

The header guard is automatically generated. What should I do with it to get it to work?

I’m subclassing CCSprite and CCTargetedTouchDelegate so I can detect the touch position of the touch using ccTouchBegan, etc.

Thank you

You basically should just make sure none of your other header files use the guard “tictactoe__TicTacToeBoard”. Though, I suspect that this isn’t the case since it is auto-generating and your file name is ‘TicTacToeBoard.h’. It should tell you which symbol is duplicated. That information can help a bit.

Though, like the others, I’d suggest not trying to do touch on the sprite. I would instead of the board layer with touch enabled with a child sprite that contains the board picture :).

Thank you everyone who tried to help.

I foudn the problem.
I was meant to include the .h file but for some reason autocompletion included the .cpp file.

If subclassing CCSprite works, howcome everyone advises against it?

Thanks

putting touch on each and every sprite doesn’t seem to scale well, I would say. You can get it to work, others have.

Imagine a game with hundreds of sprites…

I am sure someone will chime in with a very technical response, maybe one of the developers. I just know from my own experience and other conversations along this nature.

Ok…
I’m actually having another problem with subclassing CCSprite.
To enable touch,
I added this line:
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, true);

to enable touch detection.
I get a EXEC_BAD_ACCESS error and it stops at the code below of cocos2dx
@

void CCObject::retain(void)
{
CCAssert(m_uReference > 0, “reference count should greater than 0”);

++m_uReference;
}

@
I then went to my onEnter method and checked the retain count of “this” and it is greater than 0.
e.g.
@
void TicTacToeBoard::onEnter()
{
CCSize screenSize = CCDirector::sharedDirector()->getWinSize();

CCLOG ("%i", this->retainCount());

CCDirector::sharedDirector()->getTouchDispatcher()>addTargetedDelegate;
CCLOG ("%f %f", this->getPosition().x, this
>getPosition().y);
CCSprite::onEnter();
}
@

What am I doing wrong??

I’m only adding touch to this one sprite…. thank you….

Hi all

OK……found th eproblem

I forgot the “public” keyword

so instead of:
class TicTacToeBoard : public cocos2d::CCSprite, cocos2d::CCTargetedTouchDelegate

it should be:
class TicTacToeBoard : public cocos2d::CCSprite, public cocos2d::CCTargetedTouchDelegate

Ok…just wnated to post this so other ppl can benefit from it if ever they encounter the same problem.
Thank you all :slight_smile:

If subclassing CCSprite works, howcome everyone advises against it?

Like Jason mentioned, there might be some performance or technical reason why you’d not want to do it (the dispatcher goes through every delegate and will send them the touch that occurred). Though with this simple game (from what it appears) or even more complicated games it may not be noticeable (computers are pretty fast these days). I’d say the biggest reason why you shouldn’t try to do it this way is that it’s going against the grain. You’re having to make it work whereas enabling touch on a layer to just works and then you’d have the layer handle which sprite was touched.

I suppose so long as it works and you prefer this design then go for it, it is your code :P.

Thank you very much.

Yes just for this tic tac toe game I stuck with subclassing CCSprite.

Thanks alot, I think I will avoid this in the future on bigger projects….