c – 游戏跳跃逻辑

c – 游戏跳跃逻辑,第1张

概述我正在创建一个2D马里奥游戏. 以下功能用于在按下特定键时更新玩家的位置.允许玩家左右移动,并跳到同一个地方,或者向左或向右跳(形成弧形). bool updatePlayerPosition(Movement* mov){ if (this->keyPressed(SDLK_RIGHT)) { mov->applyForce(1); // Changes 我正在创建一个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}
总结

以上是内存溢出为你收集整理的c – 游戏跳跃逻辑全部内容,希望文章能够帮你解决c – 游戏跳跃逻辑所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1230544.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存