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 8896515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T00:05:36+00:00 2026-06-15T00:05:36+00:00

I am making a 2D game where we are supposed to control the character

  • 0

I am making a 2D game where we are supposed to control the character through arrow keys.

        if((win.GetInput().IsKeyDown(sf::Key::Down)))
        {
            y = y + Speed;
        }

        if((win.GetInput().IsKeyDown(sf::Key::Left)))
        {
            x = x - Speed;
        }

I have set Speed to 10. And then a i use the Sprite.SetPosition(x,y) to actually animate my character.

Everything works fine. But the problem is whenever i press an arrow key, the character moves for 1/2 seconds, stops for about 1/2 seconds and then moves again smoothly. This happens whenever i press any arrow key.

And yes, i am using a while loop on top to handle multiple events simultaneously.

I hope my question was clear enough. Please help me out!

Thanks.

  • 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-15T00:05:39+00:00Added an answer on June 15, 2026 at 12:05 am

    I think you’re not handling events the right way. What you’re doing here is checking on each event (which could be keyboard input or not) whether the sf::Key::Down key is pressed (and the same for sf::Key::Left).

    Firstly, it’s not effective, because you don’t get the result you want.
    Secondly, it performs useless checks admitting that the events could be mouse moves, mouse clicks or anything else : checking whether those keys are pressed in such cases is pointless for your program.

    I can’t see your whole code, but you should try something of this taste as your main loop :

    bool isMovingLeft = false;
    bool isMovingDown = false;
    sf::Event event;
    
    while (win.IsOpen())
    {
        // While window catches events...
        while(win.GetEvent(event))
        {
             // If the caught event is a click on the close button, close the window
             if (event.Type == sf::Event::Closed)
                 win.Close();
             // If it's a key press, check which key and move consequently
             else if (event.Type == sf::Event::KeyPressed)
             {
                  if(event.Key.Code == sf::Key::Left)
                       isMovingLeft = true;
                  else if(event.Key.Code == sf::Key::Down)
                       isMovingDown = true;
             }
             // If it's a key release, stop moving in the following direction
             else if (event.Type == sf::Event::KeyReleased)
             {
                  if(event.Key.Code == sf::Key::Left)
                       isMovingLeft = false;
                  else if(event.Key.Code == sf::Key::Down)
                       isMovingDown = false;
             }    
        }
    
        // Now that we have caught events, we move the lil' thing if we need to.
        if(isMovingLeft)
              x = x - SPEED;
        if(isMovingDown)
              y = y - SPEED;
    
    
        win.Clear();
    
        // Draw things on the screen...
    
        win.Display();
    }
    

    In this code, the whole process is split in two parts :

    • We first intercept the user input to see if we need to change the moving state of the thing.
    • Then, once every event has been caught and thoroughly analyzed, we move the thing if it has to. It is done through two bools (that you may need to increase to four if you want a four-direction control. If you want to handle diagonal directions, it would be wiser to use an enum than eight bool, which begins to be rather memory-consuming for such a simple task.)

    Note : you will maybe notice that I changed “Speed” to “SPEED”. I can’t see if it was a define, a const var or simply a var from the code you have given, but the best option would be one of the two first ones. I prefer using #define for such things, to make constants easily reachable (as they’re put in the preprocessor) and the fully capped writing make it more differentiable from classic vars once in the code. But that’s just coding style we’re talking of here 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am making a 2D game for the Android platform. The character is supposed
im making a platform type of game. I made my main character a rectangle
I'm making a game engine in C++. It is supposed to read all its
I am making game for android using libgdx my requirement is that when character
I'm making such a arrow shooting game. Everything is good. but I realized if
I am making game and need to prepare view for level selection. Could you
I built a slideshow/decision-making game in Flash but would like to try to redo
Started making a game. Here's some of my code. package games.tribe.screens; import games.tribe.model.World; import
I am making a game in java and I want to create a simulation
I'm making some game, and I have made it pretty much all, but not

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.