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