1

我的主屏幕是 Main.ccbi,它包含 3 个 CCSprite。现在我希望当用户点击 CCSprite 时我应该知道他点击了哪个?我想在 CCMotionBegin 方法中计算 CCSprite 用户点击的那个。

4

1 回答 1

2

首先将您创建的精灵存储在CCArraysay中mSpriteArray,然后您可以执行以下操作

bool MyClass :: ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
{
    CCPoint currentTouchLocation = pTouch->getLocationInView();
    currentTouchLocation = CCDirector::sharedDirector()->convertToGL(currentTouchLocation);
    currentTouchLocation = this->convertToNodeSpace(currentTouchLocation);

    CCSprite *selectedSprite = getSpriteAtPosition(currentTouchLocation);

    return true;
}

CCSprite* MyClass :: getSpriteAtPosition(CCPoint inTouchPosition)
{
    CCObject *object;
    CCARRAY_FOREACH(mSpriteArray, object)
    {
        CCSprite *sprite = (CCSprite*)sprite;

        if (sprite->boundingBox().containsPoint(inTouchPosition))
        {
            return sprite;
        }
    }

    return NULL;
}

希望这可以帮助。

于 2013-07-23T12:12:13.583 回答