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)
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.
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 :).
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();
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.