Jump to content
  • Advertisement
Sign in to follow this  
SKATIN_HARD

Staying within a window

This topic is 4501 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

What is the easiest way to limit a sprite from going off the screen in a window? I don't want to be able to go off the screen and get "lost" out of sight. Is there a way to easily do this within a 640 x 480 window?

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
Sure, just simply do collision to see if the sprite is about to move about of bounds:


if(!(spirte.pos_x >= (window_size.right - sprite.width)) {
// then the sprite is free to move
}[/cpde]

Share this post


Link to post
Share on other sites
Hey thanks......so far I have this:


if ( xpos >= LEVEL_WIDTH)
{
xpos = LEVEL_WIDTH - ship_WIDTH;


is that going in the right direction. I guess I'd just have put something in for the ypos next.

Share this post


Link to post
Share on other sites
I forgot to login for the last post sorry, but yea your headed in the right direction if that is what your meaning. Or if you mean by saying that, "LEVEL_WIDTH - ship_WIDTH", will move your sprite back in to bounds if it moves out of bounds yes it will. But what you want to do is have a sprite.temp_pos_x that will check to see if the new postion that the sprite is wanting to move to is legal. If the sprite.temp_pos_x is a legal move then set the sprite.pos_x equal to the sprite.temp_pos_x position. Also if you wanted, just to see another why of doing this:


if(!(xpos >= LEVEL_WIDTH)
{
// move the sprite with is speed in that direction
}

Share this post


Link to post
Share on other sites
How about this. Does this look ok?



void CheckBounds()

{


if (xpos >= LEVEL_WIDTH)
{
{xpos = LEVEL_WIDTH - ship_WIDTH;}

if (ypso >= LEVEL_HEIGHT)

{ypos = LEVEL_HEIGHT - ship_HEIGHT;}
}
}

Share this post


Link to post
Share on other sites
I don't see any errors with that line of code unless the LEVEL_WIDTH isn't defined, but I'm 100% sure that you have defined it. I'm guessing that maybe the error isn't from that line because it say's, "before '=' token". Check around that line of code to make sure everything is right. Post the block of code, function that that line of code is in.

Ignore the last "edit"

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!