2014-09-12 44 views
0

我想使用多點觸控並編寫所需的事件和偵聽器。現在我想要觸摸精靈,爲此,我應該獲取觸摸位置點,但我不知道如何獲取到下面的代碼:通過const std :: vector迭代<cocos2d :: Touch>&touch-Cocos2dx

void Break::onTouchesBegan(const std::vector<Touch*>& touch, Event* event) 
{ 
    What should I write here for getting touched sprite? 
} 

回答

0

確保您添加監聽

事件 - >目標(當使用場景圖的優先級)會給你的精靈

0

如果你的場景正在監聽事件,那麼event-> target()會給你基礎點頭e,而不是那個被觸動的人。在這種情況下,您需要某種形式的觸摸檢測(通過檢查矩形,或執行半徑< =距離檢查,或通過更復雜的算法 - 這一切都取決於您的情況)。

如果您的對象正在偵聽事件,那麼您可以使用event-> target(),但仍需要某種形式的碰撞檢測,以便觸摸事件系統知道觸摸是否成功。據我所知,科科斯不會爲你執行這些檢查。 只是一個例子(你的邏輯可能會有所不同 - 例如,你可以考慮觸摸被觸碰你的對象,如果觸摸的至少一個碰撞與它的邊框或諸如此類的東西):

void Break::onTouchesBegan(const std::vector<Touch*>& touch, Event* event) 
{ 
    //If at least one touch doesn't touch the object - then there is no touch 
    for(auto& t : touch){ 
     if(!COLLISION_CHECK){ 
      return false; 
     } 
    } 
    return true; 
    //OR 
    //If at least one touch touches the object, then there is a touch 
    for(auto& t : touch){ 
     if(COLLISION_CHECK){ 
      return true; 
     } 
    } 
    return false; 
} 
相關問題