我正在创建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;
}
}
这里是错误:当我打的水平(其中有一个固定的宽度)的结束,如果我试图向右移动,并在同一时间跳,玩家跳跃,并保持在空气。只有当我释放空格键时,玩家才会落地。
我该如何解决这个问题?
一个建议:如果'是持久mov'不是必需的,只是把它定义在函数内部。 –