1
我目前有問題移動一個字符權利,並顯示他像他正在運行,但是當我按下右鍵後,它向右移動一次,然後當我進入左側它再次離開,但再次按右鍵不會去再對。這裏是我的代碼,我使用:無法使用SDL將字符移動到正確的位置?
#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);
}
的主要功能是 'moveLeft()', 'MoveRight的()' 和 'stopMoving()'上面的player.cpp文件。
我認爲在player.cpp文件中存在一些問題,任何人都可以幫助我解決這個問題,因爲我一直在嘗試修復問題超過3個小時。謝謝。