cocos2d-x TouchEvent删除SameColor的Sprite

时间:2013-09-09 00:09:24

标签: iphone cocos2d-x

我现在正在创建一个点击游戏。 因此,目标精灵是随机颜色创建一个精灵。 我想要从随机放置8 x 8的精灵中擦除与目标颜色相同的精灵。 但是,当用户触摸精神时,如果精灵与目标精灵的颜色相同,我就无法获得。

是否可以确定是否点击了相同的颜色精灵和用户目标?我是怎么做的?

1 个答案:

答案 0 :(得分:0)

首先,你需要找到被点击的精灵。

如果你只为那个游戏制作一个sprite并将它们放在那个图层上,那么它就会变得更容易。

喜欢这个

CCSprite * GameScene::findSpriteWithPoint(CCPoint pos)
{
    CCArray *children = m_pGameLayer->getChildren(); 
    //make sure that there's only game sprites on the m_pGameLayer
    CCSprite *child;

    CCSprite *found = NULL;

    if (children) {
        CCARRAY_FOREACH(children, child)
        {
            if (child->boundingBox().containsPoint(pos) == true) {
                found = child;
                break;
            }
        }
    }

    return found;
}

此函数将返回找到的精灵或NULL。

请注意,在调用此功能之前,您需要将触摸位置转换为GL位置。

如下所示

CCPoint location = touch->getLocationInView();

location = CCDirector::sharedDirector()->convertToGL(location);
CCSprite * found = findSpriteWithPoint(location);

其次,只需将找到的精灵的颜色与目标精灵进行比较。

if (found != NULL) {
    ccColor3B color = found->getColor();
    ccColor3B target = m_pTarget->getColor();

    if (color.r == target.r && color.g == target.g && color.b == target.b ) {
        //The color is same
    }
}

比较方法仅在使用setColor函数制作彩色精灵时才有效。

因此,如果您想使用本机颜色的精灵,请使用CCSprite的用户数据成员作为颜色标记并比较它而不是比较颜色本身。