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

Author Topic: somethings going wrong  (Read 2602 times)

0 Members and 1 Guest are viewing this topic.

GAMINGBACON97

  • Newbie
  • *
  • Posts: 42
    • View Profile
    • Email
somethings going wrong
« on: September 07, 2015, 03:44:18 am »

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
« Last Edit: September 07, 2015, 06:53:52 am by GAMINGBACON97 »

GraphicsWhale

  • Full Member
  • ***
  • Posts: 131
    • View Profile
Re: Keyrepeatenabled not working
« Reply #1 on: September 07, 2015, 05:49:47 am »
Does the same thing happen with the sf::Event::KeyPressed event?

GAMINGBACON97

  • Newbie
  • *
  • Posts: 42
    • View Profile
    • Email
Re: Keyrepeatenabled not working
« Reply #2 on: September 07, 2015, 06:34:38 am »
yes, as well as the sf::Keybaord::isKeyPressed(sf::Keyboard::D);

GAMINGBACON97

  • Newbie
  • *
  • Posts: 42
    • View Profile
    • Email
Re: Keyrepeatenabled not working
« Reply #3 on: September 07, 2015, 06:39:05 am »
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

GAMINGBACON97

  • Newbie
  • *
  • Posts: 42
    • View Profile
    • Email
Re: Keyrepeatenabled not working
« Reply #4 on: September 07, 2015, 06:49:10 am »
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
« Last Edit: September 07, 2015, 06:54:31 am by GAMINGBACON97 »

Brax

  • Newbie
  • *
  • Posts: 39
  • Wannabe C++ Game Developer
    • View Profile
Re: Keyrepeatenabled not working
« Reply #5 on: September 07, 2015, 12:43:05 pm »
yes, as well as the sf::Keybaord::isKeyPressed(sf::Keyboard::D);

The 'window.setKeyRepeatEnabled(bool)' function works only with 'sf::Event::KeyPressed': See here.


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 --- Go and read the post in the link.

GAMINGBACON97

  • Newbie
  • *
  • Posts: 42
    • View Profile
    • Email
Re: somethings going wrong
« Reply #6 on: September 07, 2015, 07:38:02 pm »
thank you Brax, that fixed my issue