# Staying within a window

## 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 on other sites
Sure, just simply do collision to see if the sprite is about to move about of bounds:

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

##### 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 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 on other sites

     void CheckBounds()  {if (xpos >= LEVEL_WIDTH){        {xpos = LEVEL_WIDTH - ship_WIDTH;}    if (ypso >= LEVEL_HEIGHT) 	{ypos = LEVEL_HEIGHT - ship_HEIGHT;}}}

##### Share on other sites
In theory that should work.

##### Share on other sites
I thought so too. I'm getting an error:

expected primary-expression before '=' token

Can't quite make that out at this time.

##### Share on other sites
post the line of code and the error message for it. = ]

##### Share on other sites
The code piece is the same that I just posted above and it says the error occurs on this line:

if (xpos >= LEVEL_WIDTH)

##### 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 on other sites
I changed it to that and I still get the same error that I posted before.

##### Share on other sites
I'm curious to figure this out, could you post your code. (cpp file that the error is in)

##### Share on other sites
I don't wanna post the whole soucre but here is most of that part of code starting with a few #defines

#define SCREEN_WIDTH 640;
#define SCREEN_HEIGHT 480;

The function is:

   void CheckBounds()  {if (xpos => LEVEL_WIDTH){        {xpos = LEVEL_WIDTH - ship_WIDTH;}    if (ypso => LEVEL_HEIGHT) 	{ypos = LEVEL_HEIGHT - ship_HEIGHT;}}}

The errors all put to line 74 which is this line:

if (xpos => LEVEL_WIDTH)

And the 5 errors are:

expected primary-expression before '>' token

expected primary-expression before '=' token

expected )' before ';' token

expected primary-expression before ')' token

expected ;' before ')' token

##### Share on other sites
I'm still thinking about the errors, but I was just curious about your defines. Do you have ";" and the end of them like what you posted?

Quote:
 #define SCREEN_WIDTH 640;#define SCREEN_HEIGHT 480;

If so remove the ";" from the define statements.

##### Share on other sites
I did have it for a few of the things. I took them out and I still get the errors. I actually get more now b/c it says the same 5 errors for the line:

if (ypso => LEVEL_HEIGHT)

It doesn't like these 2 lines:

if (xpos => LEVEL_WIDTH)

and

if (ypso => LEVEL_HEIGHT)

but I don't really see what's wrong with them.

##### Share on other sites
I created a function like the one in your code and just made some variables so that I wouldn't get errors for the code, but I got errors. When looking through the code, you have two "{" that you don't need. And I change the "=>" to ">=" and it compiled fine.

void CheckBounds() {	if (xpos >= LEVEL_WIDTH) {		//{		xpos = LEVEL_WIDTH - ship_WIDTH;	}    if (ypos >= LEVEL_HEIGHT) {		ypos = LEVEL_HEIGHT - ship_HEIGHT;	}//}}

Edit: the "//{" are the two "{" that shouldn't be there

##### Share on other sites
Also note that you will probably need to do a similar test for the other two sides of the window (xpos <= 0 and ypos <= 0) if such behaviour isn't already present in your program.

##### Share on other sites
Hmmm.....I changed it to like you said and I still got that same error about expecting primary expression.

I am gonna have to mess around with it tomorrow I guess. Thanks for the help. I don't know if I have an extra } in there that's causing it or what. I'll post or look here tomorrow to see if anything else has changed.

## Create an account

Register a new account

• ## Partner Spotlight

• ### Forum Statistics

• Total Topics
627676
• Total Posts
2978581

• 11
• 12
• 10
• 12
• 22