void CheckHeight()
{
if (ypos > SCREEN_HEIGHT)
{
ypos -= 2; // need to subtract something to take ship back?
}
}
void CheckWidth()
{
if (xpos > SCREEN_WIDTH)
{
xpos -= 2; // subtract to take ship back?
}
}
limit sprite movement within an app using SDL/C++
I have define my app as so:
#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480
int xpos = 0, ypos = 0;
Now I have some cpp code:
My questions is: how can I have it so that the ship cannot go out of bounds?? I don't want the ship to be able to pass out of the defined screen area and go out of the players sight. That would be a problem. Am I going about doing this the wrong way? I'm using SDL on this project and c++. Thanks.
if (xpos > SCREEN_WIDTH)
xpos = SCREEN_WIDTH - 1;
and change y accordingly.
If you want, you can also subtract the entire sprite's width from the xpos, so that none of it exits the window.
xpos = SCREEN_WIDTH - 1;
and change y accordingly.
If you want, you can also subtract the entire sprite's width from the xpos, so that none of it exits the window.
Thanks but I can still go off the screen. I also have the variables:
int bgX = 0, bgY = 0; // background offset
Would I want to set those to the value of SCREEN_WIDTH - 1??
This might help to further understand. A block of my code from main()
source lang="cpp"]
if(!GamePaused)
{
bgX -= 3; // Was originally bgX -= 2;
if(bgX <= -background->w)
{
bgX = 0; //Reset the offset
}
apply_surface(bgX, bgY, background, screen);
apply_surface(bgX + background->w, bgY, background, screen);
if (keys[SDLK_UP]) { ypos -= 3; }
if (keys[SDLK_DOWN]) { ypos += 3; }
if (keys[SDLK_LEFT]) { xpos -= 3; }
if (keys[SDLK_RIGHT]) { xpos += 3; }
}
[/source]
Maybe that can help some. Thanks again.
int bgX = 0, bgY = 0; // background offset
Would I want to set those to the value of SCREEN_WIDTH - 1??
This might help to further understand. A block of my code from main()
source lang="cpp"]
if(!GamePaused)
{
bgX -= 3; // Was originally bgX -= 2;
if(bgX <= -background->w)
{
bgX = 0; //Reset the offset
}
apply_surface(bgX, bgY, background, screen);
apply_surface(bgX + background->w, bgY, background, screen);
if (keys[SDLK_UP]) { ypos -= 3; }
if (keys[SDLK_DOWN]) { ypos += 3; }
if (keys[SDLK_LEFT]) { xpos -= 3; }
if (keys[SDLK_RIGHT]) { xpos += 3; }
}
[/source]
Maybe that can help some. Thanks again.
I would code it the following way. nX is the object's position on the X axis, nY is the object's position on the Y axis. All other variables should be self explainitory.
Then your other code would look like this:
void CheckBounds(int& nX, int& nY, int& OBJECT_WIDTH, int& OBJECT_HEIGHT){ if(nX < 0) nX = 0; else if(nX + OBJECT_WIDTH > SCREEN_WIDTH) nX = SCREEN_WIDTH - OBJECT_WIDTH; if(nY < 0) nY = 0; else if(nY + OBJECT_HEIGHT > SCREEN_HEIGHT) nY = SCREEN_HEIGHT - OBJECT_HEIGHT;}
Then your other code would look like this:
if(!GamePaused){ bgX -= 3; // Was originally bgX -= 2; if(bgX <= -background->w) bgX = 0; //Reset the offset apply_surface(bgX, bgY, background, screen); apply_surface(bgX + background->w, bgY, background, screen); if (keys[SDLK_UP]) { ypos -= 3; } if (keys[SDLK_DOWN]) { ypos += 3; } if (keys[SDLK_LEFT]) { xpos -= 3; } if (keys[SDLK_RIGHT]) { xpos += 3; } CheckBounds(xpos, ypos, IMAGE_WIDTH, IMAGE_HEIGHT);}
Thanks for the tips. My code for calling the method is as follows:
CheckHeight();
CheckWidth();
DrawScene();
Should they be somewhere else instead of there above the DrawScene function?
CheckHeight();
CheckWidth();
DrawScene();
Should they be somewhere else instead of there above the DrawScene function?
Sadly no, I have not figured it out yet. I can get the code to compile but once my sprite moves to the top of the screen or goes left or right off the screen it is out of sight. This really shouldn't be happening.
I declared 2 new variables ybalance and xbalance and when they are greater than either the screen width or height I subtract 2 from the x or y balance. I just don't want my spirte to go off the screen.
I declared 2 new variables ybalance and xbalance and when they are greater than either the screen width or height I subtract 2 from the x or y balance. I just don't want my spirte to go off the screen.
I cover this in my motion tutorial.
What exactly don't you understand about this?:
What exactly don't you understand about this?:
//Move the dot left or right x += xVel; //If the dot went too far to the left or right if( ( x < 0 ) || ( x + DOT_WIDTH > SCREEN_WIDTH ) ) { //move back x -= xVel; } //Move the dot up or down y += yVel; //If the dot went too far up or down if( ( y < 0 ) || ( y + DOT_HEIGHT > SCREEN_HEIGHT ) ) { //move back y -= yVel; }
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement