I am creating a 2D Mario game.
The following function is designed to update the playerβs position when a certain key is pressed. The player is allowed to move left and right, jump in the same place or jump left or right (form like an arc).
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 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; } }
Here's the mistake: when I hit the end of the level (which has a fixed width), and if I try to move to the right and jump at the same time , the player jumps and stays in the air. Only when I unlock the space does the player come to the ground.
How can i fix this?
source share