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;
}
}
ここにバグがあります: レベルの終わり (固定幅) に到達したとき、右に移動して同時にジャンプしようとすると、プレイヤーはジャンプして空中にとどまります。スペースバーを放したときだけ、プレーヤーは地面に着きます。
どうすればこれを修正できますか?