Hello ;D
I have a problem with the keyboard input thing... i just want it to click 1 single time instead of 2 or 3 times when i click a key.
Heres my code... just so you can see what i mean. Also... Does key A count as "left mouse click"?... it looks like it does. (u can copy and paste if you wanna test)
#include <SFML/Graphics.hpp>
#include <sstream>
#include <string>
sf::RenderWindow mywindow(sf::VideoMode(800,600,32),"dsda");
sf::Event ev;
int x = 0;
template <typename T>
std::string toString(T arg)
{
std::stringstream ss;
ss << arg;
return ss.str();
}
int main()
{
sf::Text mytext;
mywindow.setVerticalSyncEnabled(true);
while(mywindow.isOpen())
{
mytext.setString(toString(x));
while(mywindow.pollEvent(ev))
{
if(ev.key.code == sf::Keyboard::A)
{
x++;
}
}
mywindow.clear(sf::Color(0,200,0));
mywindow.draw(mytext);
mywindow.display();
}
}
You can use something like:
bool key_triggered[sf::Keyboard::KeyCount]; //array containing all possible keys
void resetTriggered()
{
for (int i = 0; i < sf::Keyboard::KeyCount; ++i)
key_triggered[i] = false;
}
//initialize!
ResetTriggered();
bool isKeyTrigger(sf::Keyboard::Key key)
{
if ( sf::Keyboard::isKeyPressed(key) && !key_triggered[key] )
{
return (key_triggered[sf::Keyboard::Right] = true);
}
}
Now, you can use it like this.
if (isKeyTrigger(sf::Keyboard::Right)) //your choice of key
{
//example: move character right!
}
//must update after end of key-check loop!
resetTriggered();
This is a simplified version; you can insert the variables and fuction inside a class to make it more uniform.
Also, note that you can check for the same key ONLY ONCE per frame/tick.