Sign in to follow this  
SKATIN_HARD

Staying within a window

Recommended Posts

SKATIN_HARD    175
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
Guest Anonymous Poster   
Guest Anonymous Poster
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 this post


Link to post
Share on other sites
SKATIN_HARD    175
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
LostSource    199
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
SKATIN_HARD    175
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
LostSource    199
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
SKATIN_HARD    175
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 this post


Link to post
Share on other sites
LostSource    199
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 this post


Link to post
Share on other sites
SKATIN_HARD    175
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 this post


Link to post
Share on other sites
LostSource    199
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 this post


Link to post
Share on other sites
Thunder_Hawk    314
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 this post


Link to post
Share on other sites
SKATIN_HARD    175
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this