2014-09-12 90 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; 
} 
相关问题