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

Author Topic: Sprite is going out of bounds on the right and bottom of window! HELP!  (Read 7297 times)

0 Members and 2 Guests are viewing this topic.

KaliAhmed

  • Newbie
  • *
  • Posts: 7
    • View Profile
I am brand new to SFML and no idea how to use anything. I am a noob to C++ too. However, I want to make a sprite that I have stay within the boundaries of the window, as currently he is not stopping on the screen if I direct him too(naughty sprite!)
Thank you :) At the minute I have it so he stays on screen but it's not as fluid at the ones with left and top. My current implementation leads to him "jumping" back and forth when he goes to the right and left.
Here is my code
#include <SFML\Graphics.hpp>
#include <iostream>
#include <string>
int main()
{
        enum Direction{ Down , Left, Right, Up};
        sf::RenderWindow window(sf::VideoMode(800, 600), "My Game");
        sf::Texture playerTexture;
        sf::Sprite gameCharacter;
        sf::Vector2i source(1, 32);
       
        if (!playerTexture.loadFromFile("Player.png"))
                window.setTitle("My Window");
        gameCharacter.setTexture(playerTexture);

        while (window.isOpen())
        {
                sf::Event event;
                while (window.pollEvent(event))
                {
                        switch (event.type)
                        {
                        case sf::Event::Closed:
                                window.close();
                        case sf::Event::KeyPressed:
                                if (event.key.code == sf::Keyboard::Escape)
                                        window.close();
                        }
                }

                if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
                {
                        gameCharacter.move(0, -1);
                        if (gameCharacter.getPosition().y < 0)
                                gameCharacter.setPosition(sf::Vector2f(gameCharacter.getPosition().x, 0));
                        source.y = Up;
                }
                else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
                {
                        gameCharacter.move(0, 1);
                        if (gameCharacter.getPosition().y > window.getSize().y)
                                gameCharacter.setPosition(sf::Vector2f(gameCharacter.getPosition().x ,window.getSize().y - gameCharacter.getGlobalBounds().height));
                        source.y = Down;
                }
                else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
                {
                        gameCharacter.move(1, 0);
                        if (gameCharacter.getPosition().x > window.getSize().x)
                                gameCharacter.setPosition(sf::Vector2f(window.getSize().x-gameCharacter.getGlobalBounds().width, gameCharacter.getPosition().y));
                        source.y = Right;
                }
                else if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
                {
                        gameCharacter.move(-1,0);
                        if (gameCharacter.getPosition().x < 0)
                                gameCharacter.setPosition(sf::Vector2f(0, gameCharacter.getPosition().y));
                        source.y = Left;
                }


                source.x++;
                if (source.x * 32 >= playerTexture.getSize().x)
                        source.x = 0;
                gameCharacter.setTextureRect(sf::IntRect(source.x * 32, source.y * 32, 32, 32));
                window.draw(gameCharacter);
                window.display();
                window.clear();
        }
}
« Last Edit: March 30, 2015, 05:42:54 am by KaliAhmed »

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
The character position is its origin, and by default its origin is its top left corner.
So if you want to prevent your sprite from going further than the right/bottom edge, its position must not be greater than the size of the window minus the size of the sprite.

KaliAhmed

  • Newbie
  • *
  • Posts: 7
    • View Profile
Thank you very much and I have implemented that but he still seems to jump around when I get him to move to the right of the screen or bottom. How can I make this smoother like the top and left?

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
It's easy. You want your sprite position to stop at "value", so you compare its position to "value" and set its position to "value" if needed.
For the left side "value" is 0. For the right side "value" is window.size.x - char.size.x. Currently your condition is wrong. ;)

BTW don't modify your source code in the first post, I almost missed the fact that you updated it! It's better to post your new code in a new reply.

KaliAhmed

  • Newbie
  • *
  • Posts: 7
    • View Profile
I understand!! Thank you G.!  ;D

 

anything