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