Doing an assignment right now and I can’t limit the objects on a window.
void GameObject::screenLimit()
{
// bounce off edges
if (x > SCREEN_WIDTH - SPRITE_SIZE) xVel = -xVel;
if (x < 0) xVel = -xVel;
if (y > SCREEN_HEIGHT - SPRITE_SIZE) yVel = -yVel;
if (y < 0) yVel = -yVel;
}//---
void GameObject::screenWrap()
{
// Screen Wrap to opposite side if sprite leaves screen
if (x > SCREEN_WIDTH - SPRITE_SIZE) x = 0;
if (x < 0) x = SCREEN_WIDTH - SPRITE_SIZE;
if (y > SCREEN_HEIGHT - SPRITE_SIZE) y = 0;
if (y < 0) y = SCREEN_HEIGHT - SPRITE_SIZE;
}//---
The code above is what I tried doing and here are the resultsThe blue star should be on screen at all times (this is before I move it downwards
However when moving downwards this is the result blue star is off screen
Is there a solution? As I can’t find any
New contributor
notym is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.