You will find there are several ways to do anything in programming, the question is which one suits you best.
One way is to cap the player position inside screen boundaries by comparing x and y to width and height;
(you can use view.width and view.height, it's the same as your game resolution)
Player>Draw actor>
- Code: Select all
if(xscreen>view.width){
xscreen=view.width;
}
xscreen refers to the onscreen position, while x refers to absolute game position.
The downside to this method is that you have to add it for all the directions, but that should be simple enough...
one for xscreen<0, yscreen<0, xscreen>view.width, and yscreen >view.height.
If you wanted the player to stay completely onscreen, add or subtract 1/2 the player width.
- Code: Select all
if(xscreen>view.width-(width/2)){
xscreen=view.width-(width/2);}
else if(xscreen<(width/2)){
xscreen=(width/2);}
Here, width by itself refers to the width of the actor the script is called in; in this case, your player. Any actor variable called without a reference (like, x, y, width, height, angle) is always assumed to refer to the owner of the script.