SFML community forums

Help => Graphics => Topic started by: Chay Hawk on September 26, 2014, 11:46:49 pm

Title: Sprite wont face different direction
Post by: Chay Hawk on September 26, 2014, 11:46:49 pm
My character won't face a different direction when i hit one of the WASD keys, it will face another direction when i change the direction in  Crop player though, but it wont do anything else. the code compiles and I'm able to move the character around the screen but it just wont change the direction its facing.


/*
//
*/


#include <SFML/Graphics.hpp>
#include <SFGUI/SFGUI.hpp>

#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;


class Game
{
    public:
        void Program();
        void CreateCharacters();
        void LoadCharacters();
        void CropSprite();
        void UserInput();
        void Collision();

    private:
        vector<sf::Vector2f> player_pos;
        vector<sf::Sprite> load_sprites;
        vector<sf::Texture> load_textures;

        sf::Sprite sprite;
        sf::Texture texture;

        sf::Vector2i source;

        enum Direction{Down, Left, Right, Up};
};

class GUI : public Game
{
    public:
        void CreateMenu();
        void CloseMenu();
        void MenuButtons();

    private:
        sfg::SFGUI sfgui;
        sfg::Desktop dtop;
        sfg::Window::Ptr menu_window;

        sfg::Button::Ptr menu_exit;

        sfg::Box::Ptr box_HOR;
        sfg::Box::Ptr box_VER;
};

void GUI::CreateMenu()
{
    box_HOR = sfg::Box::Create(sfg::Box::Orientation::HORIZONTAL);
    box_HOR->Pack(menu_exit, false, true);
}

void GUI::MenuButtons()
{
    menu_exit = sfg::Button::Create("Exit");
}

//Set characters to a sprite.
void Game::CreateCharacters()
{
    sprite.setTexture(texture);

    CropSprite();
}

//This will be a vector later and it will load all
//characters individually.
void Game::LoadCharacters()
{
    if(!texture.loadFromFile("Resources/Characters/Player.png"))
    {
        cout << "Error loading texture" << endl;
    }

    CreateCharacters();
}

//We dont want our character to be the entire sprite sheet
//so we crop out the character and its position that we want.
void Game::CropSprite()
{
    source.x = 1;
    source.y = Left;

    sprite.setTextureRect(sf::IntRect(source.x * 32, source.y * 32, 32, 32));
}

void Game::UserInput()
{
    if(sf::Keyboard::isKeyPressed(sf::Keyboard::W))
    {
        source.y = Up;
        sprite.move(0, -1.3);
    }
    else if(sf::Keyboard::isKeyPressed(sf::Keyboard::A))
    {
        source.y = Left;
        sprite.move(-1.3, 0);
    }
    else if(sf::Keyboard::isKeyPressed(sf::Keyboard::S))
    {
        source.y = Down;
        sprite.move(0, 1.3);
    }
    else if(sf::Keyboard::isKeyPressed(sf::Keyboard::D))
    {
        source.y = Right;
        sprite.move(1.3, 0);
    }
    else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
    {
        //menu_exit
    }
}

void Game::Collision()
{

}

void Game::Program()
{
    sf::RenderWindow window(sf::VideoMode(800, 600), "App");
    window.setFramerateLimit(60);

    window.resetGLStates();


    LoadCharacters();

    while(window.isOpen())
    {
        sf::Event event;
        while(window.pollEvent(event))
        {
            switch(event.type)
            {
                case sf::Event::Closed:
                    window.close();
                break;
            }
        }
        window.clear(sf::Color::White);

        UserInput();

        window.draw(sprite);
        window.display();
    }
}

int main()
{
    Game game;
    game.Program();

    return 0;
}
 
Title: AW: Sprite wont face different direction
Post by: eXpl0it3r on September 27, 2014, 12:05:28 am
Well of course it won't face another direction. To do so you need to write actual code for it. ;D
Title: Re: Sprite wont face different direction
Post by: Chay Hawk on September 27, 2014, 12:06:42 am
But I thought thats what I did though? source.y = Up; etc. That should do it shouldnt it?
Title: AW: Sprite wont face different direction
Post by: eXpl0it3r on September 27, 2014, 12:10:01 am
Where do you spply the source when the game is runninv and you're pressing keys?
Also why would moving left or right changd the vertical orientation?
Title: Re: Sprite wont face different direction
Post by: Chay Hawk on September 27, 2014, 12:52:31 am
We'll I thought It will just get it when I press the keys. I'm following a tutorial and that's what he has in his program. The only real difference between his program and mine is that i split min up into classes and functions. Heres the video if you want to see it:

https://www.youtube.com/watch?v=ma5QWDYsmBg&index=11&list=PLHJE4y54mpC5j_x90UkuoMZOdmmL9-_rg
Title: Re: Sprite wont face different direction
Post by: Hapax on September 27, 2014, 04:08:28 am
Ah, I see why eXpl0it3r was confused. You're using a vector to access the spritesheet and "y" is which texture in that sheet to use, specified by using the value of an enum.
That said, you're resetting "source" just before you use it so it's always 1, 1 (Left).

Try removing:
    source.x = 1;
    source.y = Left;
If you need it for some reason, move it to after the setTextureRect().
Title: Re: Sprite wont face different direction
Post by: Chay Hawk on September 27, 2014, 01:35:31 pm
That still doesnt work for some reason.
Title: Re: Sprite wont face different direction
Post by: G. on September 27, 2014, 01:57:51 pm
We'll I thought It will just get it when I press the keys. I'm following a tutorial and that's what he has in his program. The only real difference between his program and mine is that i split min up into classes and functions.
The real difference is that he calls setTextureRect with the new source.x and .y when it changes. You don't.
The textureRect won't change if you don't call setTextureRect.
Title: Re: Sprite wont face different direction
Post by: Chay Hawk on September 27, 2014, 03:43:13 pm
Ah that's what I was missing, thank you, I got it working now, I just added CropSprite(); in the top of my UserInput function.
Title: Re: Sprite wont face different direction
Post by: Hapax on September 27, 2014, 09:09:30 pm
I didn't notice that you weren't calling CropSprite(). Sorry about that  :(
I didn't bother watching the video though  :P