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

Author Topic: Moving sprite objects of a class from within another class  (Read 3277 times)

0 Members and 1 Guest are viewing this topic.

owenmin

  • Newbie
  • *
  • Posts: 6
    • View Profile
Moving sprite objects of a class from within another class
« on: March 26, 2013, 01:39:55 am »
I'm making a base class for re-use in future projects. Basically a template that i could copy and paste to cut the initial time it takes to start a new project in SFML.

It's all working so far, and i can display my Player object's sprite to the screen. I added a function to my player class so that i can move the sprite around the screen by calling it from my base class, but when i do, it doesn't move. It draws from the base class but won't move when i call player->PlayerControl() from the base class.

Here's my code so you can see what i mean.

Main.cpp
#include <SFML/Graphics.hpp>
#include "Engine.h"

int main()
{
        Engine * engine = new Engine();

        engine->Go();

        return EXIT_SUCCESS;
}

Engine.cpp
void Engine::Go()
{
        if(!Init())
        {
                //Error here
        }
}

bool Engine::Init()
{
        window = new sf::RenderWindow(sf::VideoMode(800, 600, 32), "RPG game");
        window->setFramerateLimit(30);

        if(!window)
                return false;
        LoadImages();
        MainLoop();

        return true;
}

void Engine::LoadImages()
{
        sf::Texture playerTexture;
        playerTexture.loadFromFile("Link_front.png");

        imageManager.AddImage(playerTexture);

        player = new Player(imageManager.GetImage(0));
}

void Engine::MainLoop()
{
        while(window->isOpen())
        {
                ProcessInput();
                Update();
                RenderFrame();
        }
}

ProcessInput();
void Engine::ProcessInput()
{
        sf::Event Event;

        if(window->pollEvent(Event))
        {
                if(Event.type == sf::Event::Closed)
                        window->close();

                if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
                        window->close();
        }
        player->PlayerControl();
}

When i call player->PlayerControl(), nothing happens and the sprite doesn't move at all. Here is the Player.cpp move function:

void Player::PlayerControl()
{
        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
                playerSprite.move(0, -2.f);

        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
                playerSprite.move(0, 2.f);

        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
                playerSprite.move(-2.f, 0);

        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
                playerSprite.move(2.f, 0);
}

Sorry if this is hard to understand, it's not documented at all, but it should be simple enough to see what's going on as i have placed the functions in the order they are called so top from bottom is the way my game runs.

Why won't my sprite move when the function is called from ProcessInput() ?

santiaboy

  • Full Member
  • ***
  • Posts: 118
    • View Profile
Re: Moving sprite objects of a class from within another class
« Reply #1 on: March 26, 2013, 04:09:41 am »
Hm, I don't know what player.h is, so im guessing

maybe, just maybe, its because 2.f is a small number, so you can't see the difference, try 20 for example

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
Re: Moving sprite objects of a class from within another class
« Reply #2 on: March 26, 2013, 05:21:26 am »
Hm, I don't know what player.h is, so im guessing

maybe, just maybe, its because 2.f is a small number, so you can't see the difference, try 20 for example
No, 2.f is two whole pixels, and this is per frame, from what I can tell, so that's not it.

Can you post a complete and minimal code that reproduces the problem so we can compile it and test it ourselves?
I use the latest build of SFML2

owenmin

  • Newbie
  • *
  • Posts: 6
    • View Profile
Re: Moving sprite objects of a class from within another class
« Reply #3 on: March 26, 2013, 01:13:53 pm »
Hm, I don't know what player.h is, so im guessing

maybe, just maybe, its because 2.f is a small number, so you can't see the difference, try 20 for example
No, 2.f is two whole pixels, and this is per frame, from what I can tell, so that's not it.

Can you post a complete and minimal code that reproduces the problem so we can compile it and test it ourselves?

https://gist.github.com/OwenMin/5244931

That's the full works right now, all you need to do is copy and paste them into the corresponding file names and it should compile.

EDIT: Oops, forgot to add my ImageManager, it's all functional now.
« Last Edit: March 26, 2013, 01:18:48 pm by owenmin »

OniLinkPlus

  • Hero Member
  • *****
  • Posts: 500
    • View Profile
Re: Moving sprite objects of a class from within another class
« Reply #4 on: March 26, 2013, 08:29:41 pm »
Hm, I don't know what player.h is, so im guessing

maybe, just maybe, its because 2.f is a small number, so you can't see the difference, try 20 for example
No, 2.f is two whole pixels, and this is per frame, from what I can tell, so that's not it.

Can you post a complete and minimal code that reproduces the problem so we can compile it and test it ourselves?

https://gist.github.com/OwenMin/5244931

That's the full works right now, all you need to do is copy and paste them into the corresponding file names and it should compile.

EDIT: Oops, forgot to add my ImageManager, it's all functional now.
That is DEFINITELY not minimal. Minimal means cut out every single line of code that isn't relevant to the problem, and put it all into a SINGLE file. Preferably put it all into main() as well. For this problem, you definitely don't need all those classes to reproduce the problem.

I do, however, see what is most likely the cause of your problem. In Engine::ProcessInput(), you have these lines
if(window->pollEvent(Event))
        {
                if(Event.type == sf::Event::Closed)
                        window->close();
 
                if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
                        window->close();
        }
 
The problem here is that you say if(window->pollEvent(Event)). That won't work. That will only catch one event per frame, meaning any other events will get "backlogged" and not make it to the game for a long time. Since the keyboard state is updated by pollEvent, that means that your if statements won't know that the keyboard has been pressed until the keypress events get out of the backlog. If you want to fix this, you just need to change the if(window->pollEvent(Event)) to while(window->pollEvent(Event)).
I use the latest build of SFML2

 

anything