简体   繁体   English

将SDL2箭头/ WASD键转换为播放器移动的最佳/最可靠方法是什么?

[英]What is the best/most solid way to translate SDL2 arrow/WASD keys to player movement?

My question is what is the most solid way to implement directional input in SDL2. 我的问题是在SDL2中实现定向输入的最可靠方法是什么。

The problem with my current code is that say you wanted to move the player to the left. 我当前代码的问题是,您想将播放器向左移动。 Pressing a would decrease his x value and once you release the a key, it stops decreasing the x value. 按下a会减小其x值,一旦释放键,它就会停止减小x值。 however, if you wanted to move to the left and then immediately move to the right, the player would just stop for a period of time and then continue moving right. 但是,如果您想向左移动然后立即向右移动,则播放器只会停顿一段时间,然后继续向右移动。

Any advice would be very helpful. 任何建议都将非常有帮助。

My keyboard function: 我的键盘功能:

class KeyboardController : public Component {

public: 
TransformComponent* transform;

void init() override {
    transform = &entity->getComponent<TransformComponent>();
}

void update() override {

    if (MainGame::evnt.type == SDL_KEYDOWN) {
        switch (MainGame::evnt.key.keysym.sym)
        {
        case SDLK_w:
            transform->velocity.y = -1;

            break;
        case SDLK_a:
            transform->velocity.x = -1;

            break;
        case SDLK_s:
            transform->velocity.y = 1;

            break;
        case SDLK_d:
            transform->velocity.x = 1;

            break;
        }
    } 

    if (MainGame::evnt.type == SDL_KEYUP) {
        switch (MainGame::evnt.key.keysym.sym)
        {
        case SDLK_w:
            transform->velocity.y = 0;

            break;

        case SDLK_a:
            transform->velocity.x = 0;

            break;

        case SDLK_s:
            transform->velocity.y = 0;

            break;

        case SDLK_d:
            transform->velocity.x = 0;

            break;
        }
      }
   }
};

My velocity function: 我的速度函数:

/* class */

Vector2D position;
Vector2D velocity;

/* other code */

void update() override {
    position.x += velocity.x * speed; //speed = 3
    position.y += velocity.y * speed;
}

Instead of acting upon events to immediately change the velocity, consider adding additional state (an array) which remembers which keys are down at any given moment. 与其采取行动以立即改变速度,不如考虑添加其他状态(数组),该状态可以记住在任何给定时刻按下了哪些键。 Any time an input event is received, update the array and recalculate the velocity based on that. 每当接收到输入事件时,就更新数组并根据该值重新计算速度。

For example, depending on what game you're making, you may want to make the player stop moving if both left and right are pressed at the same time. 例如,根据您制作的游戏,如果同时按下向左和向右这两个方向,则可能需要使玩家停止移动。 Alternatively, a keydown event for a direction key would make the player immediately start moving in that direction, but you'll still need to keep track of when no more buttons are held down to know when the player should stop moving. 另外,方向键的按下事件会使玩家立即开始朝该方向移动,但是您仍然需要跟踪何时不再按住任何按钮才能知道玩家何时应该停止移动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM