【发布时间】:2015-10-15 06:55:44
【问题描述】:
我目前在向右移动一个角色并显示他正在运行时遇到问题,但是在我按下右键后它向右移动一次,然后当我向左输入时它再次向左移动但再次按下右不会让它移动再次向右。这是我正在使用的代码:
#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);
}
主要函数是上面 player.cpp 文件中的 'moveLeft()'、'moveRight()' 和 'stopMoving()'。
我认为 player.cpp 文件中存在一些问题,任何人都可以帮助我解决这个问题,因为我已经尝试解决这个问题超过 3 个小时。谢谢。
【问题讨论】: