i have something that i don’t understand
i have simple extended CCSprite that is implementing CCTargetedTouchDelegate to capture touches
looks like this :
this is the game layer that holds the CCSprite
bool GameLayer::init()
{
//////////////////////////////
// 1. super init first
if ( !CCLayer::init() ) {
return false;
}
CCDirector* pDirector = CCDirector::sharedDirector();
pDirector->getTouchDispatcher()->addTargetedDelegate(this, 0, true);
this->setTouchEnabled(true);
return true;
}
void GameLayer::createGem(int row,int col)
{
GemType gemNum;
int gemSelect =(rand() % totalGemsAvailable) + 1;
gemNum =(GemType)gemSelect;
char spritename[20];
sprintf(spritename,"gem%i_tranc.png",gemNum);
Gem *thisGem = new Gem();
thisGem->initWithSpriteFrameName(spritename);
this->addChild(thisGem,kGem);
m_GemsInPlay->addObject(thisGem);
}
bool GameLayer::ccTouchBegan(CCTouch* touch, CCEvent* event) {
CCPoint location = touch->getLocationInView();
CCPoint convLoc = CCDirector::sharedDirector()->convertToGL(location);
if (touch) {
return true;
}
}
return false;
}
void GameLayer::ccTouchMoved(CCTouch* touch, CCEvent* event)
{
// Swipes are handled here.
}
void GameLayer::ccTouchEnded(CCTouch* touch, CCEvent* event)
{
}
And the CCSprite
class Gem :public CCSprite , public CCTargetedTouchDelegate
{
public:
Gem();
~Gem();
virtual bool init ();
CREATE_FUNC (Gem);
CCRect rect();
virtual void onEnter();
virtual void onExit();
bool containsTouchLocation(CCTouch* touch);
virtual bool ccTouchBegan(CCTouch* touch, CCEvent* event);
virtual void ccTouchMoved(CCTouch* touch, CCEvent* event);
virtual void ccTouchEnded(CCTouch* touch, CCEvent* event);
virtual void touchDelegateRetain();
virtual void touchDelegateRelease();
};
bool Gem::init () {
if (!CCSprite::init()) return false;
return true;
}
void Gem::onEnter()
{
CCDirector* pDirector = CCDirector::sharedDirector();
pDirector->getTouchDispatcher()->addTargetedDelegate(this, 0, true);
CCSprite::onEnter();
}
void Gem::onExit()
{
CCDirector* pDirector = CCDirector::sharedDirector();
pDirector->getTouchDispatcher()->removeDelegate(this);
CCSprite::onExit();
}
CCRect Gem::rect()
{
CCSize s = getTexture()->getContentSize();
return CCRectMake(-s.width / 2, -s.height / 2, s.width, s.height);
}
bool Gem::containsTouchLocation(CCTouch* touch)
{
return rect().containsPoint(convertTouchToNodeSpaceAR(touch));
}
bool Gem::ccTouchBegan(CCTouch* touch, CCEvent* event)
{
if ( !containsTouchLocation(touch) ) return false;
CCLOG("Gem Touched!");
return true;
}
void Gem::ccTouchMoved(CCTouch* touch, CCEvent* event)
{
CCPoint touchPoint = touch->getLocation();
CCLOG("Gem ccTouchMoved!");
}
void Gem::ccTouchEnded(CCTouch* touch, CCEvent* event)
{
CCLOG("Gem ccTouchEnded!");
}
void Gem::touchDelegateRetain()
{
this->retain();
}
void Gem::touchDelegateRelease()
{
this->release();
}
The problem is that the when i Touch on the CCLayer i the methods of the GameLayer do invoke , but when i Touch the Gem CCSprite only the methods of the Gem invoked
and i want that the CCLayer GameLayer will also be invoked when i Touch the Gem CCSprite
how can it be done ?