Staying within a window

Started by
16 comments, last by SKATIN_HARD 18 years ago
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?
Advertisement
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]
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.

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}
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;}}} 
In theory that should work.
I thought so too. I'm getting an error:

expected primary-expression before '=' token

Can't quite make that out at this time.
post the line of code and the error message for it. = ]
The code piece is the same that I just posted above and it says the error occurs on this line:


if (xpos >= LEVEL_WIDTH)
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"

This topic is closed to new replies.

Advertisement