我正在創建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'不是必需的,只是把它定義在函數內部。 –