2012-06-21 172 views
6

我正在使用Cocos2d-x並試圖在我的HelloWorld項目中檢測觸摸。雖然我沒有運氣。檢測觸摸Cocos2d-x

.H

class HelloWorld : public CCLayer{ 

private: 
    CCSpriteBatchNode * _batchNode; 
    CCSprite *_turkey; 
    virtual void ccTouchesBegan(cocos2d::CCSet* touches, cocos2d::CCEvent* event); 

.ccp

void HelloWorld::ccTouchesBegan(cocos2d::CCSet* touches, cocos2d::CCEvent* event){ 
    CCLog("this"); 
} 

但事情是,當我點擊屏幕 '這個' 從來沒有在日誌中顯示出來。我在這裏錯過了什麼?

謝謝!

編輯,

我正在使用本教程。 http://www.raywenderlich.com/11338/cocos2d-x-for-ios-and-android-space-game

回答

20

你必須以接收觸摸與CCTouchDispatcher註冊:

寫在你的init()方法,以便接收觸摸:

CCTouchDispatcher::sharedDispatcher()->addStandardDelegate(this, 0); 

此外,我建議你通過接收觸摸事件有針對性的觸摸代表方法:

virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent); 
virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent); 
virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *pEvent); 
virtual void ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent); 

爲了使這些方法成爲c alled你有聯繫調度員註冊一個有點不同:

CCTouchDispatcher::sharedDispatcher()->addTargetedDelegate(this, 0, true); 

編輯

在新科科斯版本CCTouchDispatcher位於CCDirector

它應該是這個樣子:

CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, true); 
+0

嗯我得到的錯誤'不memeber名爲「sharedDispatcher''在‘的cocos2d :: CCTouchDispatcher’時,我的廣告是我的init(),在HelloWorld.cpp –

+0

@JamesDunay:你用什麼科科斯版本? – Andrew

+0

@JamesDunay:請看到我的編輯 – Andrew

7

那麼超級簡單的東西,只是加了

this->setIsTouchEnabled(true);

我的init();功能。

2
'this' never shows up in the log 

提示你可能會使用不同版本的cocos2d庫。請在您的項目上登錄cocos2d.h並確認。 (樣本寫在1.0.1上)。如果您的版本不同,(猜測)您可能必須使用不同的ccTouchesBegan簽名和/或修復不止setIsTouchEnabled才能使其工作。我剛剛下載了示例,並且ccTouchesBegan調用完美 - 沒有任何更改。

-1

this->setTouchEnabled(true);更好地工作比CCTouchDispatcher::sharedDispatcher()->addTargetedDelegate(this, 0, true);不幸的是我ccTouchMoved不撿東西了... :(

-1

對了cocos2d-X 3.0 ..

寫在你的」。H '文件

{bool onTouchBegan (cocos2d::Touch * touch, cocos2d::Event * event);} 

寫在你的 'init()' function ..

{ 
auto listner = EventListenerTouchOneByOne::create(); 

listner->setSwallowTouches(true);  

listner->onTouchBegan = CC_CALLBACK_2(Gameplay::onTouchBegan, this); 

_eventDispatcher->addEventListenerWithSceneGraphPriority(listner, this); 
} 

和' 的.cpp' 文件寫的..

bool "YOURCLASSNAME"::onTouchBegan(cocos2d::Touch* touch, cocos2dEvent* event) 
{ 
     CCLOG("this"); 
      return true; 
} 
0

在這裏,在下面的方法,我申請觸摸Sprite,如果要在TextField,Node,Background或任何組件上應用觸摸事件,只需將該ComponentType傳遞到此方法中,並且它將起作用....

OK LETS BEGIN !!!!

void YourClassName::YourListnerMethodName(cocos2d::Sprite* object) 
{ 
    auto listener = cocos2d::EventListenerTouchOneByOne::create(); 
    listener->setSwallowTouches(false); 

    listener->onTouchBegan = [=](cocos2d::Touch* touch, cocos2d::Event* event) 
    { 
     auto target = event->getCurrentTarget(); 
     Point locationInNode = target->convertToNodeSpace(touch->getLocation()); 

     // Suppose your sprite or any component is inside in any parent object then use this line instead of above line ... 
     //just uncomment below line and it will work fine in this case 
     //Point locationInNode = target->getParent()->convertToNodeSpace(touch->getLocation()); 

     if (target->getBoundingBox().containsPoint(locationInNode)) { 

      // CODE FOR RESPONSE AFTER TOUCH 

      return true; 
     } 
     return false; 
    }; 

    _eventDispatcher->addEventListenerWithSceneGraphPriority(listener, object); 
} 

這裏的目標是你的組件,要在其上應用觸摸

只是不要忘記調用根據您的要求,從構造函數這種方法或任何