i’m getting started with the SFML library for C++,and i’ve come along a place where,everything is working,but not the way i’d like it to.
My question is,how can i move an object with keyboard,smoothly,without that little delay?
#include <SFML/System.hpp>
#include <SFML/Graphics.hpp>
using namespace sf;
const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 600;
int main(){
VideoMode VMode(SCREEN_WIDTH, SCREEN_HEIGHT, 32);
RenderWindow screen(VMode, "Empty Window");
Event event;
Keyboard key;
Color screencolor(0,150,0);
Texture pietexture;
pietexture.loadFromFile("image.png");
Sprite pie(pietexture);
pie.setPosition(150,150);
screen.draw(pie);
screen.display();
bool on = true;
while(on){
screen.clear(Color(0,255,0));
while(screen.pollEvent(event)){
if(event.type == Event::Closed){
screen.close();
on = false;
}
else if(event.type == Event::KeyPressed){
if(key.isKeyPressed(Keyboard::Left)){
pie.move(-10,0);
}
if(key.isKeyPressed(Keyboard::Right)){
pie.move(10,0);
}
if(key.isKeyPressed(Keyboard::Up)){
pie.move(0,-10);
}
if(key.isKeyPressed(Keyboard::Down)){
pie.move(0,10);
}
}
}
screen.draw(pie);
screen.display();
}
}
What it currently does,is makes a little pause after a keypress,and then goes normally,but how can i make it without that little pause at the beggining.
Instead of moving the object on keypress events, set a variable to indicate which direction it should be moving, and at what speed. Then, on key release events, reset the speed to 0. Then handle the movement separately from the event processing section, regulated by a combination of those variables and some kind of timing system.