c++ - Unable to move character to right position using SDL? -
i having problem moving character right , showing him he's running after press right key moves right 1 time , when enter left goes left again pressing right again doesn't make go right again. here code using :
#include <sdl2/sdl.h> #include "game.h" #include "graphics.h" #include "input.h" /* game class * class holds information our game loop */ namespace { const int fps = 50; const int max_frame_time = 5 * 1000/ fps; } game::game() { sdl_init(sdl_init_everything); this->gameloop(); } game::~game() { } void game::gameloop() { graphics graphics; input input; sdl_event event; this->_player = player(graphics, 100, 100); int last_update_time = sdl_getticks(); //start game loop while(true){ input.beginnewframe(); if(sdl_pollevent(&event)){ if(event.type == sdl_keydown){ if(event.key.repeat == 0){ input.keydownevent(event); } } else if(event.type == sdl_keyup){ input.keyupevent(event); } else if(event.type == sdl_quit){ return; } } if(input.waskeypressed(sdl_scancode_escape) == true){ return; } else if(input.iskeyheld(sdl_scancode_left) == true) { this->_player.moveleft(); } else if(input.iskeyheld(sdl_scancode_right) == true) { this->_player.moveright(); } if(!input.iskeyheld(sdl_scancode_left) && !input.iskeyheld(sdl_scancode_right)){ this->_player.stopmoving(); } const int current_time_ms = sdl_getticks(); int elapsed_time_ms = current_time_ms - last_update_time; this->update(std::min(elapsed_time_ms, max_frame_time)); last_update_time = current_time_ms; this->draw(graphics); } } void game::draw(graphics &graphics){ graphics.clear(); this->_player.draw(graphics); graphics.flip(); } void game::update(float elapsedtime){ this->_player.update(elapsedtime); }
and player.cpp
#include "player.h" #include "graphics.h" namespace player_constants { const float walk_speed = 0.2f; } player::player() {} player::player(graphics &graphics, float x, float y) : animatedsprite(graphics, "mychar.png", 0, 0, 16, 16, x, y, 100) { graphics.loadimage("mychar.png"); this->setupanimations(); this->playanimation("runright"); } void player::setupanimations() { this->addanimation(1, 0, 0, "idleleft", 16, 16, vector2(0,0)); this->addanimation(1, 0, 16, "idleright", 16, 16, vector2(0,0)); this->addanimation(3, 0, 0, "runleft", 16, 16, vector2(0,0)); this->addanimation(3, 0, 16, "runright", 16, 16, vector2(0,0)); } void player::animationdone(std::string currentanimation) {} void player::moveleft(){ this->_dx = -player_constants::walk_speed; this->playanimation("runleft"); this->_facing = left; } void player::moveright(){ this->_dx = player_constants::walk_speed; this->playanimation("runright"); this->_facing = right; } void player::stopmoving() { this->_dx = 0.0f; this->playanimation(this->_facing == right ? "idleright" : "idleleft"); } void player::update(float elapsedtime) { //move dx this->_x += this->_dx * elapsedtime; animatedsprite::update(elapsedtime); } void player::draw(graphics &graphics) { animatedsprite::draw(graphics, this->_x, this->_y); }
the main functions 'moveleft()', 'moveright()' , 'stopmoving()' in player.cpp file above.
i think there's problem in player.cpp file, can me have been trying fix problem more 3 hours. thank you.
the problem caused polling 1 event on each loop meaning events won't processed. should continue polling events until there no more events queued. see sdl docs bonus details.
in case, need this:
while(sdl_pollevent(&event)){ if(event.type == sdl_keydown){ if(event.key.repeat == 0){ input.keydownevent(event); } } else if(event.type == sdl_keyup){ input.keyupevent(event); } else if(event.type == sdl_quit){ return; } }
Comments
Post a Comment