1
編輯:好吧,我通過簡單地看着TouchesTest cocos2d-x樣本找到了解決方案。唯一缺少的東西是測試觸摸位置是否包含在精靈矩形中並聲稱觸摸。因此,我能夠用EDITcocos2d-x - ccTouchBegan如何工作?
的那一個bool Artifact::claimTouch(CCTouch* pTouch)
{
CCPoint touchLocation = pTouch->getLocation();
CCRect boundingBox = this->boundingBox();
return boundingBox.containsPoint(touchLocation);
}
bool Artifact::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)
{
if (claimTouch(pTouch))
{
CCLog("id:%i", this->id);
return true;
}
return false;
}
END
我試圖攔截我在現場增加了一個對象的特定觸摸來代替我以前的代碼。
添加兩個對象的代碼:
Artifact* artifact1 = new Artifact(1);
Artifact* artifact2 = new Artifact(2);
CCRect cropRect = CCRectZero;
cropRect.size = CCSize(50,50);
artifact1->initWithFile("rock_small.png", cropRect);
artifact1->setPosition(CCPoint(100, 100));
artifact2->initWithFile("grey_rock.jpg", cropRect);
artifact2->setPosition(CCPoint(300, 200));
這裏是我獲得我的模擬器
screenshot http://img62.imageshack.us/img62/3284/cctouchbeganissue.png
我的神器級
//.H代碼
class Artifact : public CCSprite, public CCTargetedTouchDelegate
{
public:
Artifact(int id) : id(id), pressed(false){};
virtual void onEnter();
virtual void onExit();
bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);
private:
int id;
bool pressed;
};
//.CPP
void Artifact::onEnter()
{
CCSprite::onEnter();
CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, true);
}
void Artifact::onExit()
{
CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);
CCSprite::onExit();
}
bool Artifact::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)
{
CCLog("id:%i", this->id);
return true;
}
我點擊屏幕(即使我不單擊兩個廣場之一),ccTouchBegan被稱爲第二工件上任意點(輸出爲「ID:2」 )。這就像我在最後一個位置添加的CCSprite(即頂部z座標)覆蓋了整個屏幕,並阻止我訪問它下面的元素。
任何想法可能是什麼原因?
嘿,我有同樣的問題,你有沒有找到解決這個問題嗎? 看到我的問題: http://stackoverflow.com/questions/18188095/created-many-ccsprits-but-when-triggering-cctouchbegan-gives-the-last-one-allway – user63898 2013-08-13 12:23:09