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

Author Topic: Multiple use of key  (Read 1528 times)

0 Members and 1 Guest are viewing this topic.

reDo

  • Full Member
  • ***
  • Posts: 104
    • View Profile
Multiple use of key
« on: June 30, 2011, 08:08:56 pm »
I use Escape as exit in main menu and also as return from sub menu, I tried to make blocking loop when Escape is pressed for a little time like few seconds or more that only return to menu and no close all program.
Here is my code, I tried to change it many times but it doesnt work good.
Please give me addvice if you can. :roll:
This is my waiting code with mentioned blocking loop after draw score in the display.
Code: [Select]
while(1)
    {
        sf::Event Event;
        App.GetEvent(Event);

        if((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
        {
            sf::Event Event;
            App.GetEvent(Event);
            while(App.GetInput().IsKeyDown(sf::Key::Escape))
            {
                sf::Event Event;
                App.GetEvent(Event);
                sf::Sleep(0.05);
            }
            break;
        }

        if(Event.Type == sf::Event::Closed)
            return -1;


        sf::Sleep(0.05);
    }

reDo

  • Full Member
  • ***
  • Posts: 104
    • View Profile
Multiple use of key
« Reply #1 on: July 01, 2011, 07:11:38 am »
Sorry its credits function  :) , complete code
Code: [Select]
int cCredits::Draw(sf::RenderWindow & App)
{
    App.Clear();
    sf::String author;
    author.SetSize(50);
    author.SetText("Combat Shooter v1.0");
    author.SetPosition(50,0);
    author.SetColor(sf::Color(218,255,127));
    App.Draw(author);

    author.SetSize(30);
    author.SetText("Programming, menu: reDo");
    author.SetPosition(30,100);
    author.SetColor(sf::Color(165,255,127));
    App.Draw(author);

    author.SetText("Designer: KalliSSimo ");
    author.SetPosition(60,140);
    author.SetColor(sf::Color(127,255,197));
    App.Draw(author);
    App.Display();

    while(1)
    {
        sf::Event Event;
        App.GetEvent(Event);

        if((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
        {
            sf::Event Event;
            App.GetEvent(Event);
            while(App.GetInput().IsKeyDown(sf::Key::Escape))
            {
                sf::Event Event;
                App.GetEvent(Event);
                sf::Sleep(0.05);
            }
            break;
        }

        if(Event.Type == sf::Event::Closed)
            return -1;


        sf::Sleep(0.05);
    }

    return 1;
}

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Multiple use of key
« Reply #2 on: July 01, 2011, 10:11:42 am »
You shouldn't use sf::Input, rather check for sf::Event::KeyPressed events. These events occur only once.

And
Code: [Select]
sf::Event event;
App.GetEvent(Event);
is meaningless. Event handling may only be done once each frame, consider the SFML tutorials to find out how it works.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything