Hello, I just started learning SFML and need help with sprite movement. I am able to get a specific frame in the sheet and face it the correct direction depending on the key pressed (left, down, up, right). However, sometimes the sprite faces in another direction when I move my mouse even though I have no mouse events. Another problem I have is that it is not that responsive. If I spam left and right arrow keys, there is sometimes a delay before it faces it another direction (same with any two key combination spams).
Spritesheet:
http://i.imgur.com/mvqg4CI.pngHere is my code:
Player.h
#pragma once
#include <iostream>
#include <string>
#include <SFML/Graphics.hpp>
class Player
{
private:
sf::Texture texture;
sf::Sprite sprite;
sf::Vector2i source;
sf::Vector2f position;
unsigned int rows, columns;
unsigned int spriteWidth, spriteHeight;
unsigned int sheetWidth, sheetHeight;
// Corresponds to the row
enum Direction { Up, Left, Down, Right };
public:
Player(std::string filename, int rows, int columns, sf::Vector2f position);
void update(sf::Event event);
void draw(sf::RenderWindow &window);
};
Player.cpp
#include "Player.h"
Player::Player(std::string filename, int rows, int columns, sf::Vector2f position)
{
// Load the texture
if (!texture.loadFromFile(filename))
std::cout << "Could not load " << filename << std::endl;
// Sheet dimensions
sheetWidth = texture.getSize().x;
sheetHeight = texture.getSize().y;
// Attach to player sprite
sprite.setTexture(texture);
// Set the position
this->position.x = position.x;
this->position.y = position.y;
sprite.setPosition(this->position);
// Get the sprite with and height
spriteWidth = sheetWidth / columns;
spriteHeight = sheetHeight / rows;
// Set the source frame
source.x = 0;
source.y = Right;
}
void Player::update(sf::Event event)
{
if (sf::Event::KeyPressed)
{
switch (event.key.code)
{
case sf::Keyboard::Up:
source.y = Up;
break;
case sf::Keyboard::Down:
source.y = Down;
break;
case sf::Keyboard::Left:
source.y = Left;
break;
case sf::Keyboard::Right:
source.y = Right;
break;
}
}
}
void Player::draw(sf::RenderWindow &window)
{
// Create rectangle in sheet around a sprite
sprite.setTextureRect(sf::IntRect(source.x * spriteWidth, source.y * spriteHeight, spriteWidth, spriteHeight));
// Draw the sprite
window.draw(sprite);
}
Game.cpp
#include "Player.h"
int main()
{
sf::RenderWindow window;
// Initialize
unsigned int width = 900;
unsigned int height = 500;
window.create(sf::VideoMode(width, height), "FPS");
window.setFramerateLimit(60);
window.setVerticalSyncEnabled(false);
// Sprite stuff
Player player("Sprites/Skeleton.png", 4, 9, sf::Vector2f(100, 100));
// Game loop
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
switch (event.type)
{
case sf::Event::Closed:
window.close();
break;
}
player.update(event);
}
window.clear();
player.draw(window);
window.display();
}
}
I am fairly new to C++, so sorry if there are any mistakes. I come from a Java background. Any help would be appreciated, thank you!