加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 游戏跳跃逻辑

发布时间:2020-12-16 07:32:59 所属栏目:百科 来源:网络整理
导读:我正在创建一个2D马里奥游戏. 以下功能用于在按下特定键时更新玩家的位置.允许玩家左右移动,并跳到同一个地方,或者向左或向右跳(形成弧形). bool updatePlayerPosition(Movement* mov){ if (this-keyPressed(SDLK_RIGHT)) { mov-applyForce(1); // Changes t
我正在创建一个2D马里奥游戏.

以下功能用于在按下特定键时更新玩家的位置.允许玩家左右移动,并跳到同一个地方,或者向左或向右跳(形成弧形).

bool updatePlayerPosition(Movement* mov){
        if (this->keyPressed(SDLK_RIGHT)) {
            mov->applyForce(1); // Changes the velocity in X
        }   
        if (this->keyPressed(SDLK_LEFT)) {
            mov->applyForce(-1);  // Changes the velocity in X
        }           
        if (this->keyPressed(SDLK_SPACE)) {
            mov->jump();        // Changes the velocity in Y
        }       
        if (this->keyPressed(SDLK_DOWN)) {
            mov->fallDown();   // Changes the velocity in X and Y
        }

        Point* pos = mov->getPosition();

        // Check whether the position is out of bounds
        if(Level::allowsMove(pos)){
              // If it is not,I update the player's current position
              position->x = pos->x;
              position->y = pos->y;
              return true;
        }
        // If the movement is not allowed,I don't change the position
        else {
              mov->setPosition(*position);
              return false;
        }
    }

这是一个错误:当我到达关卡的末端(有一个固定的宽度)时,如果我试图向右移动并同时跳跃,则玩家会跳起并停留在空中.只有当我释放空格键时,玩家才会到达地面.

我怎样才能解决这个问题?

解决方法

对于你的游戏,我认为你只希望玩家在按下空间和玩家在场时跳跃.然后,您必须检查玩家是否在场上以获得所需的行为.

我建议你设置一个这样的机制:

if (this->keyPressed(SDLK_SPACE) && this->isOnTheFloor()) {
                                 ^^^^^^^^^^^^^^^^^^^^^^^
   mov->jump();        // Changes the velocity in Y
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读