Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8714905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T05:43:06+00:00 2026-06-13T05:43:06+00:00

I am creating a 2D Mario game. The following function is intended to update

  • 0

I am creating a 2D Mario game.

The following function is intended to update a player’s position when a particular key is pressed. The player is allowed to move left and right, and jump in the same place, or jump to the left or to the right (to 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'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;
        }
    }

Here is the bug: when I hit the end of the level (which has a fixed width), and if I try to move right and jump at the same time, the player jumps and stays in the air. Only when I release the space bar does the Player come to the ground.

How can I fix this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T05:43:07+00:00Added an answer on June 13, 2026 at 5:43 am

    For your game, I think you only want the player to jump whenever space is pressed and when the player is on the floor. You must then check if the player is on the floor to have the desired behavior.

    I suggest you device a mechanism like this:

    if (this->keyPressed(SDLK_SPACE) && this->isOnTheFloor()) {
                                     ^^^^^^^^^^^^^^^^^^^^^^^
       mov->jump();        // Changes the velocity in Y
    }    
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating game mario like in win32 GDI . I've implemented the new loop
Creating a pacman-like game, where a player eats objects. When a MovieClip (box) eats
Creating a simple RPG game, first time using XNA. Trying to get my character
Creating a mobiscroll instance using the following, when I scroll the year wheel it
Creating a view with following code. - (void)loadView { paintView=[[UIView alloc]initWithFrame:CGRectMake(0, 50, 320, 430)];
Creating Traversals for Binary Search Tree with Recursion. void inOrder(void (*inOrderPtr)(T&)) { if(this->left !=
creating users through mysql admin, but unable to login mysql command line following on-line
Creating an item(Under the key) is easy,but how to add subitems(Value)? listView1.Columns.Add(Key); listView1.Columns.Add(Value); listView1.Items.Add(sdasdasdasd);
Creating a DIV that uses CSS to draw a triangle to the left. Trying
Creating a class at runtime is done as follows: klass = Class.new superclass, &block

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.