c# - Smoother transition in movement? -


i question. still new sfml 1 seemed bit tough. have tried using while loops , such no avail.

the game doing @ moment car going , forth on x axis , guess able pick points , stuff or avoid objects coming towards car until dies.

how make movement smoother? car move 8 units first, before moving 8 units in desired direction until let go of button. it's same when other way. instantly move , keep moving when press either key.

    static void onkeypressed(object sender, eventargs e)     {         vector2f newpos = new vector2f(0, car.position.y);         keyeventargs ke = (keyeventargs)e;         if (ke.code.equals(keyboard.key.a))         {             if (car.position.x != 0)             {                 newpos.x = car.position.x - 8;                 car.position = newpos;             }             else if (car.position.x < 0)             {                 newpos.x = 0;                 car.position = newpos;             }             else if(car.position.x == 0)             {                 // nothing             }         }         else if (ke.code.equals(keyboard.key.d))         {             if (car.position.x != window.size.x - 32)             {                 newpos.x = car.position.x + 8;                 car.position = newpos;             }             else if (car.position.x > window.size.x)             {                 newpos.x = window.size.x;                 car.position = newpos;             }             else if (car.position.x == window.size.x)             {                 // nothing             }         }     } 

and entire thing reference:

using system; using system.collections.generic; using system.threading.tasks; using sfml.audio; using sfml.graphics; using sfml.window;  namespace sfmlcargame {     class program     {         private static renderwindow window;         private static sprite car;          static void main(string[] args)         {             window = new renderwindow(new videomode(256,512), "car game");             window.closed += new eventhandler(onclose);             window.keypressed += new eventhandler<keyeventargs>(onkeypressed);              sprite bg = new sprite(new texture("road.png"));             car = new sprite(new texture("car.png"));             car.position = new vector2f(window.size.x / 2, window.size.y - 96);             while (window.isopen())             {                 window.dispatchevents();                  window.clear();                  window.draw(bg);                 window.draw(car);                  window.display();             }         }          static void onclose(object sender, eventargs e)         {             renderwindow window = (renderwindow)sender;             window.close();         }          static void onkeypressed(object sender, eventargs e)         {             vector2f newpos = new vector2f(0, car.position.y);             keyeventargs ke = (keyeventargs)e;             if (ke.code.equals(keyboard.key.a))             {                 if (car.position.x != 0)                 {                     newpos.x = car.position.x - 8;                     car.position = newpos;                 }                 else if (car.position.x < 0)                 {                     newpos.x = 0;                     car.position = newpos;                 }                 else if(car.position.x == 0)                 {                     // nothing                 }             }             else if (ke.code.equals(keyboard.key.d))             {                 if (car.position.x != window.size.x - 32)                 {                     newpos.x = car.position.x + 8;                     car.position = newpos;                 }                 else if (car.position.x > window.size.x)                 {                     newpos.x = window.size.x;                     car.position = newpos;                 }                 else if (car.position.x == window.size.x)                 {                     // nothing                 }             }         }     } } 

you may want read short tutorial sfml's website.

the events fired operating system, , purposes (quit event, key pressed, left mouse button clicked) aren't things need updated often. imagine writing text, , keep 'a' key pressed. text progressively grow aaaaaaaaaaa there little delay between spawns, imposed os. get, say, 6 letters second. same events. game running , displaying @ 60 fps clear movement not smooth , barely manageable.

hopefully sfml provides way check instant state of keyboard (and mouse , joystick). c# binding (i assuming using binding 2.0 version, , if don't should) have check keyboard.iskeypressed(yourkey). doing in main loop check @ each frame key pressed, , not wait os event way slower.

on side note, if previous questions answered correctly please mark them accepting answer.

edit : haven't sfml .net installed on computer here like

static void main(string[] args)     {         window = new renderwindow(new videomode(256,512), "car game");         window.closed += new eventhandler(onclose);         // note         // window.keypressed += new eventhandler<keyeventargs>(onkeypressed);          sprite bg = new sprite(new texture("road.png"));         car = new sprite(new texture("car.png"));         car.position = new vector2f(window.size.x / 2, window.size.y - 96);         while (window.isopen())         {             window.dispatchevents();             checkinputs(); // , !              window.clear();              window.draw(bg);             window.draw(car);              window.display();         }     }  void checkinputs() {     if(keyboard.iskeypressed(keyboard.key.a))     {         if(car.position.x < 0)             car.position.x = 0         else if(car.position.x > 0)             car.position.x -= 8; // shortcut 'car.position.x = car.position.x - 8'     }     else if(keyboard.iskeypressed(keyboard.key.d))     {         // etc     } } 

this way transition should smoother, beware, 8 pixels might fast ! recommend read tutorial have linked, because explains functionning of inputs in language-agnostic way (even if examples in c++).

also, don't need event handler anymore commented it. but if checking other keys (like escape pause or whatever) don't delete it, remove part wrote moving car.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -