迭代const std :: vector <cocos2d :: touch>&amp;触Cocos2dx </cocos2d的::触摸>

时间:2014-09-12 18:10:53

标签: android c++ cocos2d-x cocos2d-x-3.0

我想使用多点触控并编写所需的事件和听众。现在我想要触及精灵,为此,我应该获得触摸点的位置,但我不知道如何将其转换为下面的代码:< / p>

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

2 个答案:

答案 0 :(得分:0)

确保在添加侦听器时使用了场景图优先级

event-&gt; target()将为您提供精灵

答案 1 :(得分:0)

如果您的场景正在侦听该事件,那么event-&gt; target()将为您提供基本节点,而不是触及的节点。在这种情况下,您需要某种形式的触摸检测(通过检查rects,或通过执行radius&lt; =距离检查,或通过更复杂的算法 - 这一切都取决于您的情况。)

如果您的对象正在侦听事件,那么您可以使用event-&gt; 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;
}