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

Author Topic: [SFML2] Handling several keypresses  (Read 1305 times)

0 Members and 1 Guest are viewing this topic.

Splux

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
[SFML2] Handling several keypresses
« on: May 15, 2012, 12:50:13 pm »
So, here I am again, missed me? ;)

I'm trying to handle several keypresses by using if/else if-statements and have one problem;

When I'm trying to get it to make a certain thing when pressing Space, it will actually do what it is supposed to do... unless I'm pressing space, up and left or space, down and right at the same time.

Then it will ignore space, and only do what it's supposed to do for up and left and what it is supposed to do for down and right...

#include <SFML2\Graphics.hpp>


class Player
{
public:
        Player()
        {
                t.loadFromFile("human.bmp");
                spPl.setTexture(t);
                spPl.setPosition(200, 200);
                spPl.setOrigin(t.getSize().x / 2, t.getSize().y / 2);
        }

        sf::Sprite getSprite()
        {
                return spPl;
        }
        sf::Vector2f getPosition()
        {
                return spPl.getPosition();
        }
        void move(int x, int y)
        {
                spPl.move(x, y);       
        }
private:
        sf::Texture t;
        sf::Sprite spPl;
};

class Zombie
{
public:
        Zombie(sf::Vector2f pos)
        {
                t.loadFromFile("zombie.bmp");
                spZm.setTexture(t);
                spZm.setPosition(pos);
        }
        sf::Sprite getSprite()
        {
                return spZm;
        }
private:
        sf::Texture t;
        sf::Sprite spZm;
};

int main (void)
{
        sf::RenderWindow w(sf::VideoMode(800, 600, 32), "lol");
        w.display();

        Player * p = new Player();
        Zombie * zm[10];
        for(int i = 0; i < 10; i++)
                zm[i] = nullptr;

        sf::Clock c;
        c.restart();

        sf::Event ev;
        while(w.isOpen())
        {
                while(w.pollEvent(ev))
                {
                        switch(ev.type)
                        {
                        case sf::Event::Closed:
                                w.close();
                        }
                }

                //my way of handling inputs:
                if(c.getElapsedTime().asMilliseconds() > 100)
                {
                        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
                        {
                                for(int i = 0; i < 10; i++)
                                {
                                        if(zm[i] == nullptr)
                                        {
                                                zm[i] = new Zombie(p->getPosition());
                                                break;
                                        }
                                }
                        }
                        //player movement:
                        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left) && sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
                        {
                                p->move(-5, -5);
                        }
                        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up) && sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
                        {
                                p->move(5, -5);
                        }
                        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right) && sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
                        {
                                p->move(5, 5);
                        }
                        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down) && sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
                        {
                                p->move(-5, 5);
                        }
                        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
                        {
                                p->move(-5, 0);
                        }
                        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
                        {
                                p->move(0, -5);
                        }
                        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
                        {
                                p->move(5, 0);
                        }
                        else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
                        {
                                p->move(0, 5);
                        }
                        c.restart();
                }

                w.clear();
                for(int i = 0; i < 10; i++)
                {
                        if(zm[i] != nullptr)
                                w.draw(zm[i]->getSprite());
                }
                w.draw(p->getSprite());
                w.display();
        }

        for(int i = 0; i < 10; i++)
        {
                if(zm[i] != nullptr)
                        delete zm[i];
        }
        delete p;
}
 

I've also tried to add another if-statement like this:
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Space) && sf::Keyboard::isKeyPressed(sf::Keyboard::Up) && sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
 
without any success, it still ignores space when Up and left or Down and Right is pressed. And yes, I did try to have it for both Up and Left, and Down and Right.

Anyone who knows a solution for this?
Just replace human.bmp and zombie.bmp if you don't believe me and want to test.

Laurent; this time I actually knew what the problem is, where to find it... just not how to solve it ;)
« Last Edit: May 15, 2012, 01:21:57 pm by Laurent »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: [SFML2] Handling several keypresses
« Reply #1 on: May 15, 2012, 01:26:05 pm »
The problem is that keyboards cannot handle an infinite number of key pressed. A keyboard is internally cabled as a grid, so if you press multiple keys on the same row or column, some of them will be ignored.

Try to use other keys (like letters) instead of arrows, and it should work.
Laurent Gomila - SFML developer

Splux

  • Newbie
  • *
  • Posts: 8
    • View Profile
    • Email
Re: [SFML2] Handling several keypresses
« Reply #2 on: May 16, 2012, 08:54:19 am »
Laurent;
Thanks.

Didn't even think of changing from the arrows to other keys, thought they all could take the same amount keypresses...

 

anything