1
General / Close Event from nothing
« on: March 20, 2011, 10:05:24 am »
I get it now, thanks
This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.
#include <SFML/Graphics.hpp>
int main()
{
sf::RenderWindow App(sf::VideoMode(600,300,32), "Test");
sf::Event Event;
sf::Image img(10,10,sf::Color(0,0,0));
sf::Sprite Spr;
bool running=true;
App.UseVerticalSync(true);
img.SetSmooth(false);
Spr.SetImage(img);
Spr.SetScale(2,2);
Spr.SetPosition(300,280);
while(running)
{
App.GetEvent(Event);
if(Event.Type==sf::Event::Closed)
running=false;
if(Event.Type==sf::Event::KeyPressed) {if(Event.Key.Code==sf::Key::Escape)running=false;}
if(App.GetInput().IsKeyDown(sf::Key::Left))Spr.Move(-10,0);
if(App.GetInput().IsKeyDown(sf::Key::Right))Spr.Move(10,0);
if(Spr.GetPosition().x<-5) Spr.SetX(600); else if(Spr.GetPosition().x>605) Spr.SetX(0);
App.Clear(sf::Color(0,150,0));
App.Draw(Spr);
App.Display();
}
}
if(Event.Type==sf::Event::Closed)
running=false;
You can either check for keypresses directly through the events, or through the input handler instance.
If you just move those if(App.GetInput()...)s out of your event processing loop it should work fine.
#include <SFML/Graphics.hpp>
int main()
{
sf::RenderWindow App(sf::VideoMode(600,300,32), "Test");
sf::Event Event;
sf::Image img(10,10,sf::Color());
sf::Sprite Spr;
bool running=true;
bool jumping=false;
short jumpenergy=0;
App.SetFramerateLimit(40);
App.UseVerticalSync(true);
img.SetSmooth(false);
Spr.SetImage(img);
Spr.SetCenter(5,5);
Spr.SetScale(2,2);
Spr.SetPosition(200,290);
while(running)
{
while(App.GetEvent(Event))
{
if(Event.Type==sf::Event::Closed)
running=false;
if(Event.Type==sf::Event::KeyPressed){
if(Event.Key.Code==sf::Key::Escape)running=false;
if(Event.Key.Code==sf::Key::Up && !jumping){jumping=true;jumpenergy=15;}
}
if(App.GetInput().IsKeyDown(sf::Key::Left))Spr.Move(-10,0);
if(App.GetInput().IsKeyDown(sf::Key::Right))Spr.Move(10,0);
if(Spr.GetPosition().x<-5) Spr.SetX(600); else if(Spr.GetPosition().x>605) Spr.SetX(0);
}
if(jumping){if(jumpenergy>-16){Spr.SetY(Spr.GetPosition().y-jumpenergy);jumpenergy--;}
else jumping=!jumping;}
App.Clear(sf::Color(0,150,0));
App.Draw(Spr);
App.Display();
}
}
if(App.GetInput().IsKeyDown(sf::Key::Left)){...}
if(App.GetInput().IsKeyDown(sf::Key::Right)){...}
if(sf::Input::IsKeyDown(sf::Key::Left)){...}
if(sf::Input::IsKeyDown(sf::Key::Right)){...}
error: cannot call member function 'bool sf::Input::IsKeyDown(sf::Key::Code) const'