【问题标题】:Iterating through const std::vector<cocos2d::Touch>& touch-Cocos2dx遍历 const std::vector<cocos2d::Touch>& touch-Cocos2dx
【发布时间】:2014-09-12 18:10:53
【问题描述】:
我想使用多点触控并编写所需的事件和侦听器。现在我想触摸精灵,为此,我应该获取触摸点的位置,但我不知道如何将其放入以下代码:
void Break::onTouchesBegan(const std::vector<Touch*>& touch, Event* event)
{
What should I write here for getting touched sprite?
}
【问题讨论】:
标签:
android
c++
cocos2d-x
cocos2d-x-3.0
【解决方案1】:
确保在添加监听器时使用了场景图优先级
event->target() 会给你精灵
【解决方案2】:
如果您的场景正在侦听事件,那么 event->target() 将为您提供基本节点,而不是被触摸的节点。在这种情况下,您将需要某种形式的触摸检测(通过检查矩形,或通过执行半径
如果您的对象正在监听事件,那么您可以使用 event->target(),但您仍然需要某种形式的碰撞检测,以便触摸事件系统知道触摸是否成功。据我所知,cocos 不会为您执行这些检查。
只是一个例子(你的逻辑可能不同 - 例如,如果至少有一个触摸与它的边界框或其他东西发生冲突,你可能会认为触摸是在触摸你的对象):
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;
}