Spielsprunglogik
Ich erstelle ein 2D-Mario-Spiel.
Die folgende Funktion soll die Position eines Spielers aktualisieren, wenn eine bestimmte Taste gedrückt wird. Der Spieler darf sich nach links und rechts bewegen und an derselben Stelle springen oder nach links oder rechts springen (um sich wie ein Bogen zu formen).
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;
}
}
Hier ist der Fehler: Wenn ich das Ende des Levels (das eine feste Breite hat) erreicht habe und versuche, mich nach rechts zu bewegen und zu springenzur selben Zeitspringt der Spieler und bleibt in der Luft. Erst wenn ich die Leertaste loslasse, kommt der Player zu Boden.
Wie kann ich das beheben?