首页 > 解决方案 > 未遵循 mp_grid_path,对象飞走或不返回被动路径

问题描述

有一个 mp_grid 设置可以在 aggro 状态下正常工作,但被动状态仅在我添加&& (path_index == -1)到 if(mp_grid_path(...))语句时才有效。但是,这会导致对象在离开仇恨状态后不会返回其被动路径。没有&& (path_index == -1),对象就会飞走,完全忽略网格,就像这样

创建事件:

chase = false;
pathPassive = 0;
pathAggro = 0;

global.mpgrid = mp_grid_create(0, 0, room_width / 8, room_height /8, 8, 8);

var _colMap = layer_tilemap_get_id(layer_get_id("col"));
var _w = room_width / TILE_SIZE;
var _h = room_height / TILE_SIZE;

//Loop through x and y to find tiles
for (var i = 0; i < _w; i++;) {
    for (var j = 0; j < _h; j++;) {
      var _tile = tilemap_get_at_pixel(_colMap, i * TILE_SIZE, j * TILE_SIZE);
      if (_tile == 1) {
        mp_grid_add_cell(global.mpgrid, i, j);
      }
    }
}

步骤事件:

//Checks for collision with player in a circle radius and initiates chase
if (collision_circle(x, y, dr, obj_player, false, false))
    {
        chase = true;       
    }
    
//Hiding & line of sight ends the chase
if (global.Hide) && collision_line(x, y, obj_player.x, obj_player.y, obj_player, false, false))
    {
        chase = false;
    }

if (chase == true)
{

    //If the path doesn't exist, create it.
    if(!path_exists(pathAggro)) pathAggro = path_add();
    
    //With each step, the grid path is drawn & checks that a path isn't currently being followed
    if (mp_grid_path(global.mpgrid, pathAggro, x, y, obj_player.x, obj_player.y, 0))
    {
        path_start(pathAggro, global.gamePathSpeed, path_action_stop, 0);
    }
}
else
{
    
    //If the path doesn't exist, create it.
    if(!path_exists(pathPassive)) pathPassive = path_add();
    
    //With each step, the grid path is drawn & checks that a path isn't currently being followed
    //Without path_index == -1, the path is drawn every frame and the movement doesn't repeat in reverse
    if (mp_grid_path(global.mpgrid, pathPassive, xstart, ystart, xTo, yTo, 0))// && (path_index == -1))
    {
        path_start(pathPassive, global.gamePathSpeed, path_action_reverse, 0);
        show_debug_message("Path found");
    } else show_debug_message("Path not found");
        
}```

Any help would be greatly appreciated.

标签: path-findinggame-makergml

解决方案


path_start 的绝对值必须为 1,而不是 0。实例在离开 aggro 状态时遵循相对于自身的路径,从而导致“飞离”。


推荐阅读