-
scroll down to see the new problem
I'm having an issue where when I hold down a key it sends a bunch of things to the program saying im pressing it. so I did MainWindow.setKeyRepeatEnabled(false);//doesn't work
while (MainWindow.pollEvent(Event)){
if (Event.type == sf::Event::KeyReleased){
if (Event.key.code == sf::Keyboard::D){
if (PlayerColor == Blue){
BlueScore++;
ChooseNextPlayer();
}
if (PlayerColor == Pink){
GameOver();
}
}
if (Event.key.code == sf::Keyboard::A){
if (PlayerColor == Pink){
PinkScore++;
ChooseNextPlayer();
}
if (PlayerColor == Blue){
GameOver();
}
}
}
//that it my code for trying to make part of a sorting game
-
Does the same thing happen with the sf::Event::KeyPressed event?
-
yes, as well as the sf::Keybaord::isKeyPressed(sf::Keyboard::D);
-
actually, I did some more testing, thats not the error appereantly, I'm having a different issue somewhere else in my code, I will get back to this,in about 5 minutes
-
current problem
Ok so my issue is when I change the color, I lose even if I don't touch anything
here is the code for that:
while (MainWindow.pollEvent(Event)){
if (Event.type == sf::Event::KeyReleased){
if (Event.key.code == sf::Keyboard::D){
if (PlayerColor == Blue){
BlueScore++;
ChooseNextPlayer();
}
if (PlayerColor == Pink){
GameOver();
}
}
if (Event.key.code == sf::Keyboard::A){
Player.move(10, 0);
if (PlayerColor == Pink){
PinkScore++;
ChooseNextPlayer();
}
if (PlayerColor == Blue){
GameOver();
}
}
}
void ChooseNextPlayer(){
if (Lose == false){
srand(time(0));
NumberChosen = 1 + (rand() % 2);
if (NumberChosen == 1){
PlayerColor = Pink;
}
if (NumberChosen == 2){
PlayerColor = Blue;
}
std::cout << NumberChosen << std::endl;
}
}
if you need anything else of my code to help please just ask
-
yes, as well as the sf::Keybaord::isKeyPressed(sf::Keyboard::D);
The 'window.setKeyRepeatEnabled(bool)' function works only with 'sf::Event::KeyPressed': See here (http://www.sfml-dev.org/documentation/2.3.1/classsf_1_1RenderWindow.php#aef9f2b14c10ecba8a8df95dd51c5bb73).
And I am sorry, but your code and explanation is quite confusing.
My wild guess is that you have a faulty 'if' statements:
if (Event.key.code == sf::Keyboard::D){
if (PlayerColor == Blue){
BlueScore++;
ChooseNextPlayer();
}
if (PlayerColor == Pink){
GameOver();
}
Here in this code you say to the program: "When I press the 'D' key, check whether the player is blue AND THEN check whether the player is pink."
If the player is blue, the program will then continue to execute the said statement, where you call the 'ChooseNextPlayer()' function (which apparently randomly chooses the next player).
When program randomly chooses next player, it proceeds with next 'if' statement, which checks whether the player is pink, and if that player is pink - because in previous 'if' statement you called 'ChooseNextPlayer()' function - it will then execute it's statement, which calls 'GameOver()'.
The same applies when you press 'A' key.
In order to prevent this, you need to use an 'else' or 'else if' in your second statements, so when the first 'if' statement is true, it won't proceed to check the other statements.
If there is another problem, then I can't help you further without you providing a complete and minimal code (http://en.sfml-dev.org/forums/index.php?topic=5559.msg36368#msg36368) --- Go and read the post in the link.
-
thank you Brax, that fixed my issue