GML - Changing room without changing player position

From Global Programming Syntax

Jump to: navigation, search

In this script, you will find out how to make a player appear in the same position of the room when changing rooms. This is especially handy in 2D if you change from outdoors (in 2D) to indoors then back outdoors. That is when indoors is a different room to outdoors.

Example Code

So create a object called obj_positiona and place the object anywhere in the room but with no sprite. In these scripts, it is ashumed that the name of the player object is obj_player. So in obj_positiona object, put the following GML in its create event.

globalvar playerax, playeray;

In the 'Room Start' event of obj_positiona, add the following.

{
obj_player.x=playerax;
obj_player.y=playeray;
if obj_player.x=0 and obj_player.y=0 then
{
playerax=32; // position player starts on the x axis (horizontal)
playerab=70; // position player starts on the y axis (vertical)
}
}

Now in the 'Room End' event of obj_positiona, add the following GML.

{
playerax=obj_player.x;
playeray=obj_player.y;
}

Now all you need to do for different levels is to duplicate the object you have just created with the above GML but you need to change the following variables. Whenever obj_playerax is mentioned, it needs to be changed to obj_playerbx and whenever obj_playeray is mentioned, change it to obj_playerby. Then make this second object's name obj_positionb. So for the next level replace the b in obj_positionb to a 'c' and same with the variables.

Personal tools
languages
page stats
Toolbox