c++ - Moving Rectangle in SFML, how to make it smoother? -



c++ - Moving Rectangle in SFML, how to make it smoother? -

this code:

#include <sfml/graphics.hpp> #include <iostream> #include <windows.h> int main() { sf::renderwindow window(sf::videomode(800, 600), "sfml game"); window.setframeratelimit(200); //variable keeps game loop running bool play = true; //event object holding events sf::event event; //states button/events bool left = false; bool right = false; bool space = false; //variables int rectxposition = 375; //rectangles x position int rectyposition = 400; //rectangles y position //images sf::texture image1; if (image1.loadfromfile("images/greeksoldier.png") == -1) { std::cout << "failed!!!" << "\n"; homecoming 1; } //render shapes sf::rectangleshape rect; rect.setsize(sf::vector2f(77, 150)); //width , height rect.setposition(375, 400); //position rect.setfillcolor(sf::color::white); //color rect.settexture(&image1); //game loop while (play == true) { //events while (window.pollevent(event)) { if (event.type == sf::event::keypressed && event.key.code == sf::keyboard::left) { //set state true left = true; } if (event.type == sf::event::keypressed && event.key.code == sf::keyboard::right) { right = true; } if (event.type == sf::event::keypressed && event.key.code == sf::keyboard::space) { space = true; } //event type window closed if (event.type == sf::event::closed) { //set play false in order stop game loop play = false; } } //logic if (left == true) { int x = 0; (x = 1; x < 2; x++) { rectxposition-=5; //x position variable of rectangle rect.setposition(rectxposition, rectyposition); window.clear(); window.draw(rect); window.display(); sleep(5); } left = false; } if (right == true) { int x = 0; (x = 1; x < 2; x++) { rectxposition+= 5; //x position variable of rectangle rect.setposition(rectxposition, rectyposition); window.clear(); window.draw(rect); window.display(); sleep(5); } right = false; } if (space == true) { int x = 0; (x = 1; x < 15; x++) { rectyposition-= 3; //x position variable of rectangle rect.setposition(rectxposition, rectyposition); window.clear(); window.draw(rect); window.display(); sleep(10); } (x = 1; x < 15; x++) { rectyposition+= 3; //x position variable of rectangle rect.setposition(rectxposition, rectyposition); window.clear(); window.draw(rect); window.display(); sleep(10); } space = false; } //rendering window.clear(); //draw rectangle shape window.draw(rect); window.display(); } //clean , close window window.close(); homecoming 0; }

how create motion more smoother? how create rectangle(the soldier) can jump , move @ same time? , how stop so, when holding downwards left doesn't take 3 seconds move?

i've added youtube video if guys can't bothered compile , want see i'm talking about. https://www.youtube.com/watch?v=g_jlwgh1wgo

the keypressed events generated when presses key , repeat every (but not every frame, makes not smooth). intended used text input, example.

pressing key interrupt repetition of events previous key, other problem has same cause.

you should handle keypressed events instant actions, pressing menu button. in particular illustration should rid of altogether , utilize sf::keyboard replacing line:

if (left == true)

with:

if (sf::keyboard::iskeypressed(sf::keyboard::left))

and on...

i explain why sf::keyboard right thing here, documentation well.

another big problem have multiple loops don't allow else happen in game. should familiar concepts of event loops, velocity , gravity (and create corresponding variables).

when player presses space shouldn't hardcode character move n pixels , n pixels downwards while stopping every other possible action in game, instead alter character's vertical velocity , utilize alter character's position in every frame (and have velocity reduced gravity).

the of import point take must have 1 main event loop calls window.display.

c++ sfml

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -