Legacy GM [SOLVED] About an object following the player

R

Ryzzax

Guest
Hi all!

So, in my game, when I press '1', it creates a shield around the player, which will protect the player from enemies. Everything with that works fine. The problem is, the shield doesn't follow the player when the player moves and that's not what I want. I want the shield to always be around the player when the player moves. Like a normal shield would do, right? So, how should I do that? I guess it's some lines of code in the step event of the obj_shield. But I don't know those lines of code. Could someone tell me how and write me a code I should use, and if possible, explains to me in brief how it works? I will really appreciate it! :)

Here's how it looks in my game currently;


Thank you to anyone in advance! :)
 

Nocturne

Friendly Tyrant
Forum Staff
Admin
OKay, when you create the shield object, do it like this:

Code:
with (instance_create(x, y, obj_SHield))
{
follow = other.id;
}
The code now gives the shield object a new variable "follow" and sets it to hold the ID of the instance that created it (the player). Knowing this we can now add this into the STEP event of the shield object:

Code:
if instance_exists(follow)
{
x = follow.x;
y = follow.y;
}
else instance_destroy();
All that does is check to see if the follow instance (the player) exists and if it does, it jumps to the same position, and if it doesn't then it destroys itself.

Hope that helps!!!

PS: look up "with" in the manual and read the section on "Addressing Variables" for more information in general... ;)
 
R

Ryzzax

Guest
OKay, when you create the shield object, do it like this:

Code:
with (instance_create(x, y, obj_SHield))
{
follow = other.id;
}
The code now gives the shield object a new variable "follow" and sets it to hold the ID of the instance that created it (the player). Knowing this we can now add this into the STEP event of the shield object:

Code:
if instance_exists(follow)
{
x = follow.x;
y = follow.y;
}
else instance_destroy();
All that does is check to see if the follow instance (the player) exists and if it does, it jumps to the same position, and if it doesn't then it destroys itself.

Hope that helps!!!

PS: look up "with" in the manual and read the section on "Addressing Variables" for more information in general... ;)
It perfectly works! Thank you!! :)
 
Top