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

Author Topic: Sprite wont face different direction  (Read 3028 times)

0 Members and 1 Guest are viewing this topic.

Chay Hawk

  • Full Member
  • ***
  • Posts: 101
    • View Profile
    • Email
Sprite wont face different direction
« 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;
}
 

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10826
    • View Profile
    • development blog
    • Email
AW: Sprite wont face different direction
« Reply #1 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
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Chay Hawk

  • Full Member
  • ***
  • Posts: 101
    • View Profile
    • Email
Re: Sprite wont face different direction
« Reply #2 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?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10826
    • View Profile
    • development blog
    • Email
AW: Sprite wont face different direction
« Reply #3 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?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Chay Hawk

  • Full Member
  • ***
  • Posts: 101
    • View Profile
    • Email
Re: Sprite wont face different direction
« Reply #4 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:

« Last Edit: September 27, 2014, 01:04:29 am by Chay Hawk »

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Sprite wont face different direction
« Reply #5 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().
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

Chay Hawk

  • Full Member
  • ***
  • Posts: 101
    • View Profile
    • Email
Re: Sprite wont face different direction
« Reply #6 on: September 27, 2014, 01:35:31 pm »
That still doesnt work for some reason.

G.

  • Hero Member
  • *****
  • Posts: 1592
    • View Profile
Re: Sprite wont face different direction
« Reply #7 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.

Chay Hawk

  • Full Member
  • ***
  • Posts: 101
    • View Profile
    • Email
Re: Sprite wont face different direction
« Reply #8 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.

Hapax

  • Hero Member
  • *****
  • Posts: 3351
  • My number of posts is shown in hexadecimal.
    • View Profile
    • Links
Re: Sprite wont face different direction
« Reply #9 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
Selba Ward -SFML drawables
Cheese Map -Drawable Layered Tile Map
Kairos -Timing Library
Grambol
 *Hapaxia Links*

 

anything