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

Author Topic: Input::IsKeyDown problem  (Read 5040 times)

0 Members and 1 Guest are viewing this topic.

iSkull

  • Newbie
  • *
  • Posts: 6
    • View Profile
Input::IsKeyDown problem
« on: March 16, 2011, 05:14:45 pm »
Hello,
I just started programming with SFML and i wanted to make a simple moving mechanism.

The first attempt was like:

Code: [Select]
if(App.GetInput().IsKeyDown(sf::Key::Left)){...}
if(App.GetInput().IsKeyDown(sf::Key::Right)){...}


But it couldn't process multiple inputs at once like jumping and moving.

I tried to use sf::Input::IsKeyDown() to try to fix that:
Code: [Select]
if(sf::Input::IsKeyDown(sf::Key::Left)){...}
if(sf::Input::IsKeyDown(sf::Key::Right)){...}

But i get this error:
Code: [Select]
error: cannot call member function 'bool sf::Input::IsKeyDown(sf::Key::Code) const'
Im using SFML 1.6 with MinGW and Code::Blocks
Sorry for my poor English.

James

  • Newbie
  • *
  • Posts: 6
    • View Profile
Input::IsKeyDown problem
« Reply #1 on: March 16, 2011, 05:26:58 pm »
Why couldn't you process multiple inputs using the first method? May we see some more code, since the first method is the correct way of doing things.

In the second way you are trying to call a method of the class instead of a method of the instance, which is what is giving you an error.

iSkull

  • Newbie
  • *
  • Posts: 6
    • View Profile
Input::IsKeyDown problem
« Reply #2 on: March 16, 2011, 05:32:01 pm »
Well, i don't really care which method i am using, just want it to process multiple keys.

Right now, if i hold the left arrow and press the up arrow to jump, the character will jump, but it will stop moving left, and i have to release and re-press the left arrow to make him move again.

I just want to fix this.

devlin

  • Full Member
  • ***
  • Posts: 128
    • View Profile
Input::IsKeyDown problem
« Reply #3 on: March 16, 2011, 05:41:41 pm »
Quote from: "iSkull"
Well, i don't really care which method i am using, just want it to process multiple keys.

Right now, if i hold the left arrow and press the up arrow to jump, the character will jump, but it will stop moving left, and i have to release and re-press the left arrow to make him move again.

I just want to fix this.

This is a problem with your game logic, not IsKeyDown.

We'll need to see the "..." stuff that you commented out earlier - and for the Up Key as well.

Even better would be a minimal compilable code example that reproduces the problem.

iSkull

  • Newbie
  • *
  • Posts: 6
    • View Profile
Input::IsKeyDown problem
« Reply #4 on: March 16, 2011, 06:24:45 pm »
Ok, here is the sourcecode of a program that reproduces the problem:

Code: [Select]
#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow App(sf::VideoMode(600,300,32), "Test");
    sf::Event Event;
    sf::Image img(10,10,sf::Color());
    sf::Sprite Spr;

    bool running=true;
    bool jumping=false;
    short jumpenergy=0;

    App.SetFramerateLimit(40);
    App.UseVerticalSync(true);
    img.SetSmooth(false);
    Spr.SetImage(img);
    Spr.SetCenter(5,5);
    Spr.SetScale(2,2);
    Spr.SetPosition(200,290);

    while(running)
    {
        while(App.GetEvent(Event))
        {
            if(Event.Type==sf::Event::Closed)
                running=false;

            if(Event.Type==sf::Event::KeyPressed){
                if(Event.Key.Code==sf::Key::Escape)running=false;
                if(Event.Key.Code==sf::Key::Up && !jumping){jumping=true;jumpenergy=15;}
            }
        if(App.GetInput().IsKeyDown(sf::Key::Left))Spr.Move(-10,0);
        if(App.GetInput().IsKeyDown(sf::Key::Right))Spr.Move(10,0);
        if(Spr.GetPosition().x<-5) Spr.SetX(600); else if(Spr.GetPosition().x>605) Spr.SetX(0);
        }
        if(jumping){if(jumpenergy>-16){Spr.SetY(Spr.GetPosition().y-jumpenergy);jumpenergy--;}
            else jumping=!jumping;}
        App.Clear(sf::Color(0,150,0));
        App.Draw(Spr);
        App.Display();
    }

}



PS:strangely, if i move the mouse over the application while is moving/jumping, it works just how i want to

James

  • Newbie
  • *
  • Posts: 6
    • View Profile
Input::IsKeyDown problem
« Reply #5 on: March 16, 2011, 06:50:00 pm »
You can either check for keypresses directly through the events, or through the input handler instance.

If you just move those if(App.GetInput()...)s out of your event processing loop it should work fine.

The reason the mouse works is having the mouse over the window generates an event every frame (iirc) which would allow the code in the event handling loop to run normally.

iSkull

  • Newbie
  • *
  • Posts: 6
    • View Profile
Input::IsKeyDown problem
« Reply #6 on: March 16, 2011, 06:59:41 pm »
Quote from: "James"
You can either check for keypresses directly through the events, or through the input handler instance.

If you just move those if(App.GetInput()...)s out of your event processing loop it should work fine.


It works perfectly now, thank you very much!

This topic can be closed.