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

Author Topic: Beginner Question  (Read 1474 times)

0 Members and 1 Guest are viewing this topic.

psychotical

  • Newbie
  • *
  • Posts: 2
    • View Profile
Beginner Question
« on: April 10, 2011, 05:12:17 pm »
Hello,
I'm quite new to SFML and have a simple(?) question.
I was able to create a sprite and could successfully move it through the window. But how can I put a restriction, that the sprite can't move out of the window?

Thanks in advice
psychotical

PS: I made use of the search, but I had problem finding what I need.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Beginner Question
« Reply #1 on: April 10, 2011, 06:33:39 pm »
Check for the sprite's border positions using sf::Sprite's methods GetPosition() and GetSize(). The window size is given by sf::RenderWindow::GetWidth() and GetHeight(). Now you can prevent movement if the sprite's right / bottom coordinates are bigger than the window width / height or if the sprite's left / top coordinates are smaller than zero.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

psychotical

  • Newbie
  • *
  • Posts: 2
    • View Profile
Beginner Question
« Reply #2 on: April 10, 2011, 08:40:43 pm »
Thanks for your quick reply.

Well, I know what you mean but I can't implement it.
First I have following function for the Window height and width:
App.GetHeight();
App.GetWidth();

For the Sprite I have just this: Sprite.GetPosition()

I'm far away from the solution, because I don't know where to start, could someone please help me?

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
Beginner Question
« Reply #3 on: April 10, 2011, 08:45:31 pm »
Hm... It seems like you are not very experienced in C++ yet. Maybe you should take your time to learn it before you switch to SFML, because a lot of basic knowledge is required. Otherwise, you will stumple upon the easiest problems again and again.

Lookup the documentation for the return type of sf::Sprite::GetPosition(). Then you see a 2D vector is returned which contains separate X and Y coordinates.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development: