Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [SOLVED]Sprite not moving  (Read 4639 times)

0 Members and 1 Guest are viewing this topic.

The Illusionist Mirage

  • Full Member
  • ***
  • Posts: 115
  • My Life = Linux, C++ & Computers
    • View Profile
    • fleptic
    • Email
[SOLVED]Sprite not moving
« on: October 17, 2013, 08:21:00 pm »
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
« Last Edit: October 18, 2013, 11:21:37 am by The illusionist mirage »

The Hatchet

  • Full Member
  • ***
  • Posts: 135
    • View Profile
    • Email
Re: Sprite not moving
« Reply #1 on: October 17, 2013, 09:02:44 pm »
What values do you have for the player speed?  I want to say that maybe your DELTA_TIME may be so small that you are only moving by like0.000x a pixel, which would look like you aren't moving at all.  Since you're cout's are all being hit as they should maybe you should print out your players current x/y coordinate to like 6 decimal places, i bet the sprite is only moving a teeny tiny bit.

The Illusionist Mirage

  • Full Member
  • ***
  • Posts: 115
  • My Life = Linux, C++ & Computers
    • View Profile
    • fleptic
    • Email
Re: Sprite not moving
« Reply #2 on: October 18, 2013, 06:36:00 am »
What values do you have for the player speed?  I want to say that maybe your DELTA_TIME may be so small that you are only moving by like0.000x a pixel, which would look like you aren't moving at all.  Since you're cout's are all being hit as they should maybe you should print out your players current x/y coordinate to like 6 decimal places, i bet the sprite is only moving a teeny tiny bit.

I already tried to remove DELTA_TIME and made the player move like m_playerSprite.move(m_PlayerSpeed.x , 0); and m_playerSprite.move(0, m_PlayerSpeed.x); (where m_playerSpeed is a sf::Vector2f and has a value 250 for both x and y). But that doesn't move my sprite either.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Sprite not moving
« Reply #3 on: October 18, 2013, 07:54:41 am »
I don't see any drawing code (window.clear(), window.draw(player), window.display(), etc.) in your code.
Laurent Gomila - SFML developer

The Illusionist Mirage

  • Full Member
  • ***
  • Posts: 115
  • My Life = Linux, C++ & Computers
    • View Profile
    • fleptic
    • Email
Re: Sprite not moving
« Reply #4 on: October 18, 2013, 08:41:56 am »
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;
}
 
« Last Edit: October 18, 2013, 08:50:06 am by The illusionist mirage »

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Sprite not moving
« Reply #5 on: October 18, 2013, 09:26:34 am »
Since the only thing that allows you to conclude that the player is not moving, is that you don't see it moving, the drawing part is actually relevant to the problem.

You should add some cout statements to your code, to track the player position and any intermediate variable used to update it. This way you would easily notice which part of the code doesn't work as expected, instead of trying to guess.
Laurent Gomila - SFML developer

The Illusionist Mirage

  • Full Member
  • ***
  • Posts: 115
  • My Life = Linux, C++ & Computers
    • View Profile
    • fleptic
    • Email
Re: Sprite not moving
« Reply #6 on: October 18, 2013, 11:21:18 am »
Hello

Umm.. it was a very silly mistake on my part that I forgot to initialize the speed variable in the player class. Hence, my sprite wasn't moving.

Apologies for posting without looking thoroughly.

 

anything