Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Jittery movement  (Read 1869 times)

0 Members and 1 Guest are viewing this topic.

sandwich hoop

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Jittery movement
« on: February 25, 2023, 07:37:02 pm »
Hi there, sorry if this is asked a lot, I have written some movement code for a little game, the code works, however on key press, it moves 1 tick of movement, before smoothly moving when held after about a second.

while (this->window->pollEvent(this->ev))
        {
                switch (this->ev.type)
                {
                case sf::Event::Closed:
                                this->window->close();
                                break;
                case sf::Event::KeyReleased:
                        if (this->ev.key.code == sf::Keyboard::Escape)
                        {

                                this->window->close();

                        }

                        int i = 0;

                        if (this->ev.key.code == sf::Keyboard::W)
                        {

                                this->players[i].move(0.f, -15);

                        }

                        if (this->ev.key.code == sf::Keyboard::A)
                        {

                                this->players[i].move(-15, 0.f);

                        }

                        if (this->ev.key.code == sf::Keyboard::S)
                        {

                                this->players[i].move(0.f, 15);

                        }

                        if (this->ev.key.code == sf::Keyboard::D)
                        {

                                this->players[i].move(15, 0.f);

                        }
               
                }





        }




 


My question is if there is a simple way of fixing this so that the movement is smooth immediately. Thanks :)

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: Jittery movement
« Reply #1 on: February 26, 2023, 12:57:06 pm »
this is due to your keyboard default delay time between keypresses. if you open any text editor and hold a letter like W, you'll notice it behaves exactly the same.

so, for this type of movement, you don't want to use keyboard events. you can use the keyboard global state. instead of
if (this->ev.key.code == sf::Keyboard::W)
use
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))

also, your switch statement is a bit messy. why don't you use simply 'if' for now until you get the hang of things?
Visit my game site (and hopefully help funding it? )
Website | IndieDB

sandwich hoop

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Re: Jittery movement
« Reply #2 on: March 18, 2023, 08:10:55 pm »
Sorry for the late reply, I tried this and it had the same effect, I noticed a difference when moving diagonally but there I still a pause when using

if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))

sorry if I'm being dumb but I've been trying to fix this for weeks and I have no idea haha!

sandwich hoop

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Re: Jittery movement
« Reply #3 on: March 18, 2023, 08:26:29 pm »
Nevermind, I borrowed someone else on here's code and switched the variable names and it works. Thanks for your time.

Solution:

while (this->window->pollEvent(this->ev))
        {

                if (sf::Event::Closed)
                {
                        this->window->close();
                        break;

                }

                if (this->ev.key.code == sf::Keyboard::Escape)
                {

                        this->window->close();

                }




        }

                        int i = 0;

                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) {
                                this->players[i].move(0, -3);
                       
                        }
                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::S)) {
                                this->players[i].move(0, 3);
               
                        }
                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) {
                                this->players[i].move(-3, 0);
               
                        }
                        if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) {
                                this->players[i].move(3, 0);
               
                        }

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: Jittery movement
« Reply #4 on: March 18, 2023, 11:06:06 pm »
ops, I forgot to tell you to move that line outside the event loop. but you did it yourdelf, so i'm glad it worked  ;D
Visit my game site (and hopefully help funding it? )
Website | IndieDB

sandwich hoop

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Re: Jittery movement
« Reply #5 on: March 19, 2023, 04:41:51 pm »
 ;D