如何在cocos2d-x中捕获按住键盘事件?

时间:2017-03-20 03:00:51

标签: cocos2d-x

我在这里有一个函数可以在每个按下的键上移动一个Sprite。现在我也希望将其移动到按键而不是反复按键,但我不知道该怎么做。请指导我,非常感谢你的帮助。

keyBoardListener->onKeyPressed = [](EventKeyboard::KeyCode keyCode, Event* event)
    {
        Vec2 location = event->getCurrentTarget()->getPosition();
        switch (keyCode)
        {
        case EventKeyboard::KeyCode::KEY_LEFT_ARROW:
        case EventKeyboard::KeyCode::KEY_A:
            event->getCurrentTarget()->setPosition(location.x - 10.0f, location.y);
            break;
        case EventKeyboard::KeyCode::KEY_RIGHT_ARROW:
        case EventKeyboard::KeyCode::KEY_D:
            event->getCurrentTarget()->setPosition(location.x + 10.0f, location.y);
            break;
        case EventKeyboard::KeyCode::KEY_UP_ARROW:
        case EventKeyboard::KeyCode::KEY_W:
            event->getCurrentTarget()->setPosition(location.x, location.y + 10.0f);
            break;
        case EventKeyboard::KeyCode::KEY_DOWN_ARROW:
        case EventKeyboard::KeyCode::KEY_S:
            event->getCurrentTarget()->setPosition(location.x, location.y - 10.0f);
            break;
        }
    };

1 个答案:

答案 0 :(得分:2)

当按下某个键时没有连续触发的事件,因此解决问题的一种方法是使用跟踪x轴上的移动的全局(或类或其他)变量,以及y的一个变量。轴。

要仅为每个键使用两个变量而不是单独的变量,可以使用2个整数,让我们说xMovement和yMovement,并根据按下的键将它们的值设置为-1,0或1。如果xMovement为-1​​,则将精灵向左移动,如果它向右移动它,如果它不移动它就不会移动它。 y轴也是一样。要实现这一目标,您应该像这样更改代码:

keyBoardListener->onKeyPressed = [](EventKeyboard::KeyCode keyCode, Event* event)
    {
        switch (keyCode)
        {
        case EventKeyboard::KeyCode::KEY_LEFT_ARROW:
        case EventKeyboard::KeyCode::KEY_A:
            xMovement--;
            break;
        case EventKeyboard::KeyCode::KEY_RIGHT_ARROW:
        case EventKeyboard::KeyCode::KEY_D:
            xMovement++;
            break;
        case EventKeyboard::KeyCode::KEY_UP_ARROW:
        case EventKeyboard::KeyCode::KEY_W:
            yMovement++;
            break;
        case EventKeyboard::KeyCode::KEY_DOWN_ARROW:
        case EventKeyboard::KeyCode::KEY_S:
            yMovement--;
            break;
        }
    };

现在你还应该添加一个密钥释放事件,其中递增/递减应该与按下的按键事件相反:

keyBoardListener->onKeyReleased = [](EventKeyboard::KeyCode keyCode, Event* event)
    {
        switch (keyCode)
        {
        case EventKeyboard::KeyCode::KEY_LEFT_ARROW:
        case EventKeyboard::KeyCode::KEY_A:
            xMovement++;
            break;
        case EventKeyboard::KeyCode::KEY_RIGHT_ARROW:
        case EventKeyboard::KeyCode::KEY_D:
            xMovement--;
            break;
        case EventKeyboard::KeyCode::KEY_UP_ARROW:
        case EventKeyboard::KeyCode::KEY_W:
            yMovement--;
            break;
        case EventKeyboard::KeyCode::KEY_DOWN_ARROW:
        case EventKeyboard::KeyCode::KEY_S:
            yMovement++;
            break;
        }
    };

现在,要实际移动你的精灵,只需在每个帧调用的更新函数中执行类似的操作:

float newPosX = sprite->getPositionX() + (xMovement * 10.f);
float newPosY = sprite->getPositionY() + (yMovement * 10.f);
sprite->setPosition(newPosX, newPosY);

您还需要一些机制来确保xMovement和yMovement保持在边界内(在我的示例中,如果您同时按下左箭头和' a'键,精灵将移动两倍的速度:P),但这只是一个粗略的例子来演示如何使用键盘事件和更新功能实现连续移动。