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

Author Topic: sprite moving out of view!!  (Read 2206 times)

0 Members and 1 Guest are viewing this topic.

sterex

  • Newbie
  • *
  • Posts: 5
    • Yahoo Instant Messenger - sterexg
    • View Profile
sprite moving out of view!!
« 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.
programming is pure fun.

devlin

  • Full Member
  • ***
  • Posts: 128
    • View Profile
sprite moving out of view!!
« Reply #1 on: March 23, 2011, 07:06:42 pm »
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.

sterex

  • Newbie
  • *
  • Posts: 5
    • Yahoo Instant Messenger - sterexg
    • View Profile
sprite moving out of view!!
« Reply #2 on: March 23, 2011, 08:37:02 pm »
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.
programming is pure fun.

guiguithebest

  • Newbie
  • *
  • Posts: 6
    • View Profile
sprite moving out of view!!
« Reply #3 on: May 30, 2011, 08:10:01 pm »
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.

 

anything