SFML community forums
Help => Graphics => Topic started by: sterex on March 23, 2011, 05:57:08 pm
-
hi there! I am a newbie.
Can someone be kind to explain to me the 'idea' behind containing the sprite within the screen. I m trying to move a simple image with GetInput methods from the keyboard. It does move when I press a key, but it just goes on and on untill it disappears. How to stop it when it reaches the wall?
I tried searching the forum for a long time, tried some people's code, but I couldn't understand it , as each have their own ways.
-
If you want to contain the sprite within the bounds of the window - and have a non-scrolling game, but rather a single screen game - you basically do this.
Figure out the width and height of the window. (there are functions for this).
Check your sprites position + sprite width and compare it with the window width. If it's above the window width, adjust the position.x to window.width - sprite.width.
Use a similar approach for height.
-
Thank you so much devlin. It works.
Basically I had the same idea, but my head was in a mess, thinking that there should be a function or something like that for containing the sprite with the bounds of the window.
-
Hi, I had the same problem and what devlin said helped a lot, thank you. But I don't know if I have to check the coordinate while I'm pressing the key, or somewhere else, so can anyone tell me? Thank you.