mp_potential_step(xgoal, ygoal, stepsize, checkall)
参数 | 描述 |
---|---|
xgoal | The target x position. |
ygoal | The target y position. |
stepsize | The speed the instance moves in pixels per step. |
checkall | Whether to check all instances (true) or just solid ones (false). |
返回: Boolean(布尔值)
This function lets the instance take a step towards a particular
position defined by xgoal/ygoal, all the while trying to avoid
obstacles. When the instance would run into a solid instance (or
any instance when checkall is true) it will change the direction of
motion to try to avoid that instance and move around it. This
approach is not guaranteed to work but in most easy cases it will
effectively move the instance towards the goal. The function
returns whether the goal was reached or not.
if instance_exists(obj_Enemy)
{
var inst;
inst = instance_nearest(x, y, obj_Enemy);
mp_potential_step(inst.x, inst.y, 5, false);
}
The above code first checks to see if there are any instances of "obj_Enemy" in the room. If there are it then finds the nearest one and stores its id in a variable. This variable is then used to tell mp_potential_step to move the instance with the code towards the x/y position of the object that was found at a speed of 5 pixels per step while avoiding only instances flagged as solid.