Hello
I have a player class which contains a member function called Update() to get the key the user is pressing(if any) and then move the sprite accordingly using the move(offsetX, offsetY) function. But that's not working in my case. Here's how I move my sprite:
void Player::Update(float DELTA_TIME)
{
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
{
m_playerSource.y = PlayerDirection::UP;
m_isKeyPressed = true;
m_currentKeyState = KeyState::UP_PRESSED;
m_playerSprite.move(0, - m_PlayerSpeed.y * DELTA_TIME);
std::cout << "Up" << std::endl;
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
{
m_playerSource.y = PlayerDirection::DOWN;
m_isKeyPressed = true;
m_currentKeyState = KeyState::DOWN_PRESSED;
m_playerSprite.move(0, m_PlayerSpeed.y * DELTA_TIME);
std::cout << "Down" << std::endl;
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
{
m_playerSource.y = PlayerDirection::LEFT;
m_isKeyPressed = true;
m_currentKeyState = KeyState::LEFT_PRESSED;
m_playerSprite.move(- m_PlayerSpeed.x * DELTA_TIME, 0);
std::cout << "Left" << std::endl;
}
else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
{
m_playerSource.y = PlayerDirection::RIGHT;
m_isKeyPressed = true;
m_currentKeyState = KeyState::RIGHT_PRESSED;
m_playerSprite.move(m_PlayerSpeed.x * DELTA_TIME, 0);
std::cout << "Right" << std::endl;
}
else
{
m_isKeyPressed = false;
m_currentKeyState = KeyState::NONE_PRESSED;
}
m_frameCounter += m_frameSpeed * m_frameTimer.restart().asSeconds();
if(m_frameCounter >= m_switchFrame)
{
m_frameCounter = 0.0f;
if(m_isKeyPressed)
{
m_playerSource.x++;
}
if(m_playerSource.x * 32 >= m_playerTexture.getSize().x)
{
m_playerSource.x = 0;
}
}
m_playerSprite.setTextureRect(sf::IntRect(m_playerSource.x * 32, m_playerSource.y * 32, 32, 32));
}
Even the cout statement gets printed if I press a key but the sprite's not moving. This function is called directly in the main game loop as follows:
while(myWindow.isOpen())
{
t_f = m_timer.getElapsedTime().asSeconds();
dt = t_f - t_i;
t_i = t_f;
sf::Event m_gameEvent;
while(myWindow.pollEvent(m_gameEvent))
{
if(m_gameEvent.type == sf::Event::Closed
|| m_gameEvent.key.code == sf::Keyboard::Escape)
{
m_gameWindow.close();
}
}
m_player.Update(dt);
}
Where am I going wrong?
Thanks
I don't see any drawing code (window.clear(), window.draw(player), window.display(), etc.) in your code.
Actually, I have it properly in place, but my problem lies in update only so i didn't post full code here ;)
My main is something like this:
#include <SFML/Graphics.hpp>
#include <SFML/Audio.hpp>
#include <iostream>
#include "Constants.h"
#include "Game.h"
#include "TileMap.h"
#include "Player.h"
int main()
{
//Game m_game;
//m_game.Run();
sf::RenderWindow m_gameWindow(sf::VideoMode(WINDOW_WIDTH, WINDOW_HEIGHT,
WINDOW_BPP), WINDOW_TITLE, sf::Style::Close);
sf::Event m_gameEvent;
TileMap m_tileMap;
Player m_player;
float t_i = 0.0f, t_f = 0.0f, dt = 0.0f;
sf::Clock m_timer;
while(m_gameWindow.isOpen())
{
t_f = m_timer.getElapsedTime().asSeconds();
dt = t_f - t_i;
t_i = t_f;
while(m_gameWindow.pollEvent(m_gameEvent))
{
if(m_gameEvent.type == sf::Event::Closed
|| m_gameEvent.key.code == sf::Keyboard::Escape)
{
m_gameWindow.close();
}
}
m_gameWindow.clear();
//m_tileMap.RenderTileMaps(m_gameWindow);
m_player.Draw(m_gameWindow);
m_player.Update(dt);
m_gameWindow.display();
}
return EXIT_SUCCESS;
}