Why is this not working?

Ok so I am making something in SFGUI but there is one thing that isnt working, this line:

source(1, Down);

inside of LoadCharacters. When I remove it, the code runs and my character appears on screen, when I add it, I get htis error:

C:\Users\thund_000\Desktop\SFML\main.cpp|50|error: no match for call to '(sf::Vector2i {aka sf::Vector2<int>}) (int, int)'|

I don't understand what the problem is.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
//
*/

#include <SFML/Graphics.hpp>

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

using namespace std;


class Game
{
    public:
        Game();

        void Program();
        void CreateCharacters();
        void LoadCharacters();
        void CropSprite();
        void UserInput();

    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};
};



void Game::CreateCharacters()
{
    sprite.setTexture(texture);

    CropSprite();
}

void Game::LoadCharacters()
{
    source(1, Down);

    if(!texture.loadFromFile("Resources/Characters/Player.png"))
    {
        cout << "Error loading texture" << endl;
    }

    CreateCharacters();
}

void Game::CropSprite()
{
    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;
    }
    if(sf::Keyboard::isKeyPressed(sf::Keyboard::A))
    {
        source.y = Left;
    }
    if(sf::Keyboard::isKeyPressed(sf::Keyboard::S))
    {
        source.y = Down;
    }
    if(sf::Keyboard::isKeyPressed(sf::Keyboard::D))
    {
        source.y = Right;
    }
}

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

    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;
}
And what should that line do?
Oh sorry, the 1 tells it to go in the middle of the character sprite sheet and Down tells it what direction to face. I'm following this video tutorial

http://youtu.be/ma5QWDYsmBg?list=PLHJE4y54mpC5j_x90UkuoMZOdmmL9-_rg

what im trying to do is at 3:37
Last edited on
But there is no () operator in Vector2. http://www.sfml-dev.org/documentation/2.0/classsf_1_1Vector2.php
If you want to change it, you need to assign it manually:
1
2
3
4
source.x = 1;
source.y = Down;
// or
source = sf::Vector2i(1, Down)
Alrigth sweet I got it working, but now I have another problem, the character is supposed to turn in the direction i specify (W A S D) but She isnt so I think the variables are being destroyed when it leaves block scope, but it shouldnt do that because the functions are a part of the class right? Also I have a GUI class as you can see, and I need to use my variables from it in the Program() function but it's already a member function of Game so how do i use the variable from GUI class in it?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
//
*/

#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 = Down;

    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;
    }
    if(sf::Keyboard::isKeyPressed(sf::Keyboard::A))
    {
        source.y = Left;
    }
    if(sf::Keyboard::isKeyPressed(sf::Keyboard::S))
    {
        source.y = Down;
    }
    if(sf::Keyboard::isKeyPressed(sf::Keyboard::D))
    {
        source.y = Right;
    }
    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);

    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;
}
Last edited on
You do not change your sprite after key is pressed. You just changing some values in vector, that all.
well in the CropSprite() function if i change: source.y = Down; to Up, it will change the direction the player is facing so doing what i have should work.
No. CropSprite is called only from CreateCharacters which is only called from LoadCharacters which is callde only once before your game loop. After that you do not touch your sprite, so it never changes.
So I need to call cropped sprite again? The image only needs cropping once so will it mess anything up by putting it in a loop that will continuously execute it?
That depends on how do you want to process your sprite, You can recreate it, replace it with another, etc.
Ok I got it working, I added CropSprite in the beginning of UserInput()
Topic archived. No new replies allowed.