SFML community forums
Help => Window => Topic started by: Finn on August 08, 2010, 09:45:56 pm
-
Hey
I'm working on a rpg at the moment. Now I want to know how to clear the Input. For example:
I want to level up the character by pressing 'F1'.
If I do it like:
if((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::F1))
{
Player.levelup();
//...
}
it's like 30 level ups by pressing 'f1'....any suggestions?
thanxs,
Finn
-
There is a function to enable or disable automatic key repeats:
sf::Window::EnableKeyRepeat(bool enable);
-
There is a function to enable or disable automatic key repeats:sf::Window::EnableKeyRepeat(bool enable);
Didn't change a thing :-/
-
Can you show a minimal and complete code example still reproducing the problem?
Which version of SFML do you use?
-
Just to be sure: you didn't copy & paste the code given by Nexus, right? You wrote window.EnableKeyRepeat(false)? ;)
-
Just to be sure: you didn't copy & paste the code given by Nexus, right? You wrote window.EnableKeyRepeat(false)? ;)
Sure I did O_o
Version 1.6
if((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::F1))
{
window.EnableKeyRepeat(false);
Player.levelup();
Text_Playerlevel.SetText(Player.GetLevel());
}
I also tried to put it on the beginning of the main function...the same
-
I also tried to put it on the beginning of the main function...
This is where it's supposed to be put, yes.
Ok, so now we'll need the complete and minimal example that Nexus talked about, to figure out what you did wrong ;)
-
It's like:
main.cpp
#include "main.h"
#include "engine.h"
int main()
{
engine engine;
engine.game();
return EXIT_SUCCESS;
}
engine.cpp
engine::engine()
{
window.EnableKeyRepeat(false);
window.Create(sf::VideoMode(1024,768,32),"RPG");
}
void engine::game()
{
while(window.IsOpened())
{
while(window.GetEvent(Event))
{
if(Event.Type == sf::Event::Closed)
window.Close();//exit
}
if(GSchanged)//GameState changed
Load();//load stuff for the new GameState
Draw();//Draw everything onto the screen
HandleInput();//handle input for each gamestate
}
}
void engine::HandleInput()
{
if((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::F1))
{
//...
}
}
-
We really need a complete and minimal code. The prupose is to have something that we can compile, execute and debug without doing anything else than copying and pasting your code in a cpp file.
So you actually need to take some time to write this code and make sure it compiles and still produces the error ;)
-
Remove all the stuff that is not directly required for a working application or related to the error. This concerns game logics, graphic handling, user-defined classes, and so on. Only leave what's really necessary.
Use a single file (no headers) to demonstrate the problem with as few code lines as possible. Then you may call your code minimal. But don't forget it still has to be complete and compilable... ;)
-
This should work:
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <iostream>
int main()
{
sf::RenderWindow window(sf::VideoMode(800,600),"Test");
sf::Event Event;
window.EnableKeyRepeat(false);
int i = 0;
while(window.IsOpened())
{
window.GetEvent(Event);
if((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::F1))
{
std::cout << i << std::endl;
i++;
}
window.Clear();
window.Display();
}
}
-
That's perfect, thank you :)
So with the code above, when you hold F1 you get repeated KeyPressed events?
What operating system do you use?
-
Linux Ubuntu 10.04
-
Anything I can do to solve this problem?
-
What you could do it to test the same piece of code with SFML 2.
-
Ok....I installed SFMl 2!
But...is there something wrong with the sf::String class? With 1.6 my project compiled without any errors...but now I get various errors on using the sf:String class.. :(
like
error: ‘class sf::String’ has no member named ‘SetFont’
What the... :-o
-
sf::String is now sf::Text I believe (which is a much better name).
-
engine.cpp:85: error: ‘class sf::Text’ has no member named ‘SetText’
Just the same
-
sf::String::SetText -> sf::Text::SetString ;)
-
Omg xD Why??? :D
-
I think they are better names, aren't they?
-
kk....but other errors left here!
error: ‘class sf::FloatRect’ has no member named ‘Right’
error: ‘class sf::FloatRect’ has no member named ‘Bottom’
And many issues with the sf::IntRect class...like:
‘class sf::IntRect’ has no member named ‘Offset’
Also a renamed class?
-
A huge number of things have changed... that's why it's SFML 2.0 and not 1.7 ;)
You'd better have a look at the documentation:
http://www.sfml-dev.org/documentation/2.0/