set do something when touch CCSprite

set do something when touch CCSprite
0.0 0


Hi all !

i create 1 class extend CCSprite use like Button

GVButtonSprite* GVButtonSprite::createWithSpriteFrameName(const char fileName,const charnameFramePress) {
GVButtonSprite* pGVButtonSprite = new GVButtonSprite();
isTwoFrame = true;
frameNormal = fileName;
framePressed = nameFramePress;
return pGVButtonSprite;

i have 1 CCCalFunc to do action for this button .

class GVButtonSprite : public CCSprite,public CCTargetedTouchDelegate {
CCCallFunc **onTouchHandler;
// some code
and 1 function to set do something / set CCCalFunc for this button
void GVButtonSprite::setOnTouch {
onTouchHandler = action;
in touchEnd of this button
void GVButtonSprite::ccTouchEnded {
// other code
onTouchAction = CCSequence::create;
// other code
My problem is :

  1. I create 1 CCCalFunc in CCLayer and setTouch it for Button QuitGame , but when i touch it , it’s error :);


  1. I use this class for 5 another button :
    example :

CCTexture2D** playTexture = CCTextureCache::sharedTextureCache()->addImage(“button/play.png”);
BtnPlay = GVButtonSprite::paddleWithTexture(playTexture);

buton when i touch Button Play ( not setTouch ) , it’s error too , i think when i setTouch for BtnQuit , BtnPlay be setTouch too :frowning: .

Someone help me please , i newbie in C++ :frowning: .


at the momment i call CCCallFunc *onTouchHandler; in .cpp but it still error when i setTouch and touch any button :frowning:


my problem is solved , i set CCObject and SEL_Func and create CCCallFunc in ButtonSprite , it’s work .

But i still don’t understand why i only setTouch for Button quit , another button get this action too :frowning:


First of all - it looks as you CCCallFunc* might have gotten autoreleased. You should have called retain(); on it.

Now - if I understand correctly : you assign an action to one of the buttons, but other buttons also have it? Could you post your Button class and code where you create this buttons and assign their actions?