How to stop a sprite in SFML ?

Hey, I have a question, how to stop a sprite when it reaches the end of the window in SFML 2.4.2 (I use CodeBlocks ) ?

PS : Sorry for my bad english.
Last edited on
That is the code so far ...

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
#include <SFML/Graphics.hpp>
#include <iostream>
using namespace sf;
using namespace std;
int main()
{
    float x = 0;
    float y = 0;
    const float speed = 5.0;
    bool vSyncEnabled = true;
    RenderWindow window(VideoMode(800,640), "SFML works!");
    window.setFramerateLimit(60);
    window.setVerticalSyncEnabled(vSyncEnabled);
    Texture textura;
    if(!textura.loadFromFile("Sprite.png"))
    {
        cout<<"ERROR"<<endl;
    }
    else
    {
        cout<<"TEXTURE LOADED"<<endl;
    }
    Sprite sprit;
    sprit.setTexture(textura);
    sprit.setOrigin(sf::Vector2f(0, 0));
    sprit.setPosition(400, 300);
    Vector2f pos = sprit.getPosition();
    while (window.isOpen())
    {
        Event event;
        while (window.pollEvent(event))
        {
            if (event.type == Event::Closed)
                window.close();
        }
        if(Keyboard::isKeyPressed(Keyboard::Left))
        {
            sprit.setPosition(pos.x , pos.y);
            pos.x -= speed;
            cout<<pos.x<<endl;
        }
        if(Keyboard::isKeyPressed(Keyboard::Right))
        {
            sprit.setPosition(pos.x , pos.y);
            pos.x += speed;
            cout<<pos.x<<endl;
        }
        if(Keyboard::isKeyPressed(Keyboard::Up))
        {
            sprit.setPosition(pos.x , pos.y);
            pos.y -= speed;
            cout<<pos.y<<endl;
        }
        if(Keyboard::isKeyPressed(Keyboard::Down))
        {
           sprit.setPosition(pos.x , pos.y);
           pos.y += speed;
           cout<<pos.y<<endl;
        }
        window.clear();
        window.draw(sprit);
        window.display();
    }
    return 0;
}
Your window is 800 pixels wide, so:

1
2
const int Min_x = 0;
const int Max_x = 800;


You might want to adjust Max_x so your sprite stays fully on screen. 800 - sprite width, say.

Then after you decrement your sprite's x position (line #39 in your posted code), make sure it isn't less than Min_x. If it is, set it to equal Min_x. Similar for Max_x. Use the same technique for the vertical axis.

Also, I don't think I'd call setPosition first thing. Just modify the pos.x and pos.y variables in the if statements and then call setPosition once at the end - just before you call window.clear().

E.g.
1
2
3
4
5
6
7
8
9
10
11
12
        if(Keyboard::isKeyPressed(Keyboard::Left))
        {
            sprit.setPosition(pos.x , pos.y);
            pos.x -= speed;
            if (pos.x < Min_x) { /* adjust */ }
            cout<<pos.x<<endl;
        }

        // ...

        sprit.setPosition(pos.x, pos.y);
        window.clear()
Last edited on
It worked, but when the sprite was down(or right) it continued to go till it was like 1% of the sprite
like



!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#########################################################
Last edited on
Garbled rubbish that wasn't pertinent to the conversation. Sorry guys.
Last edited on
Yes because the position you set refers to the top-left corner of the sprite. So if your 'max x' is 800, it'll keep going until it draws the leftmost pixels of your sprite at position 800 - i.e. the whole thing's (just) offscreen.
Sometimes that's what you want (- it means a game character can smoothly exit the screen stage left/right) but if you want to keep your sprite visible at all times modify that max value. For example, if your sprite's 64 pixels wide, maybe:
const int Max_x = 800 - 64;
Hope that helps.
That's the code, but I don't have any idea why it does like that ...
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
#include <SFML/Graphics.hpp>
#include <iostream>
using namespace sf;
using namespace std;
int main()
{
    float x = 0;
    float MinX = 0;
    float MaxX = 480;
    float MinY = 0;
    float MaxY = 480;
    float y = 0;
    const float speed = 5.0;
    bool vSyncEnabled = true;
    RenderWindow window(VideoMode(480,480), "SFML works!");
    window.setFramerateLimit(60);
    window.setVerticalSyncEnabled(vSyncEnabled);
    Texture textura;
    if(!textura.loadFromFile("Sprite.png"))
    {
        cout<<"ERROR"<<endl;
    }
    else
    {
        cout<<"TEXTURE LOADED"<<endl;
    }
    Sprite sprit;
    sprit.setTexture(textura);
    sprit.setOrigin(sf::Vector2f(0, 0));
    sprit.setPosition(400, 300);
    Vector2f pos = sprit.getPosition();
    while (window.isOpen())
    {
        Event event;
        while (window.pollEvent(event))
        {
            if (event.type == Event::Closed)
                window.close();
        }
        if(Keyboard::isKeyPressed(Keyboard::Left))
        {
            pos.x -= speed;
            if(pos.x < MinX)
            {
                pos.x += speed;
            }
            cout<<"Pos. of x : "<<pos.x<<endl;
        }
        if(Keyboard::isKeyPressed(Keyboard::Right))
        {
            pos.x += speed;
            if(pos.x > MaxX)
            {
                pos.x -= speed;
            }
            cout<<"Pos. of x : "<<pos.x<<endl;
        }
        if(Keyboard::isKeyPressed(Keyboard::Up))
        {
            pos.y -= speed;
            if(pos.y < MinY)
            {
                pos.y += speed;
            }
            cout<<"Pos. of y : "<<pos.y<<endl;
        }
        if(Keyboard::isKeyPressed(Keyboard::Down))
        {
           pos.y += speed;
           if(pos.y > MaxY)
            {
                pos.y -= speed;
            }
           cout<<"Pos. of y : "<<pos.y<<endl;
        }
        sprit.setPosition(pos.x , pos.y);
        window.clear();
        window.draw(sprit);
        window.display();
    }
    return 0;
}
1
2
3
4
    float MinX = 0;
    float MaxX = 480 - /* sprite width */ ;
    float MinY = 0;
    float MaxY = 480 - /* sprite height */ ;
Thank you, it worked !
Topic archived. No new replies allowed.