GML - Stopping obj player from leaving the room

From Global Programming Syntax

Jump to: navigation, search

There are many ways to prevent the player from going outside the room but below is an example with no side effects.

Example Code

This script will show you how to prevent obj_player moving outside the room where obj_player cannot be seen. Also this script has been design for a sprite with a size of 32x32 with its grid locking centered. This scripts will not stop the player from leaving the view but is a replacement of having a solid object around the entire room. Put the following script in obj_player step event.

if obj_player.x<16 then obj_player.x=16;
if obj_player.x>room_width-16 then obj_player.x=room_width-16;
if obj_player.y<16 then obj_player.y=16;
if obj_player.y>room_height-16 then obj_player.y=room_height-16;
Personal tools
languages
page stats
Toolbox