我目前在向右移动一个角色并向他显示他正在跑步时遇到问题但是在我按下右键后它向右移动一次然后当我向左进入时它再次向左移动但是向右再次向右移动#&# 39;让它再次向右走。这是我正在使用的代码:
#include <SDL2/SDL.h>
#include "game.h"
#include "graphics.h"
#include "input.h"
/* Game class
* This class holds all information for 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 the 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);
}
和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 by dx
this->_x += this->_dx * elapsedTime;
AnimatedSprite::update(elapsedTime);
}
void Player::draw(Graphics &graphics) {
AnimatedSprite::draw(graphics, this->_x, this->_y);
}
主要功能是&#39; moveLeft()&#39;,&#39; moveRight()&#39;和&#39; stopMoving()&#39;在上面的player.cpp文件中。
我认为在player.cpp文件中存在一些问题,任何人都可以帮我解决这个问题,因为我一直试图解决这个问题超过3个小时。谢谢。
答案 0 :(得分:0)
问题可能是由于每个循环上只轮询一个事件意味着某些事件无法处理。您应该继续轮询事件,直到没有其他事件排队。有关奖励详情,请参阅SDL docs。
在你的情况下,你需要这样的东西:
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;
}
}