目标:在一个随机生成的无边界六边形地图中通过鼠标点击控制人物的移动。
现象:在get_id_path时程序报错:
Player_setting.gd:14 @ _process(): Can't get id path. Point (-1, -1) out of bounds [P: (0, 0), S: (0, 0)].
这是出问题的源代码:
extends CharacterBody2D
@export var map:TileMapLayer
func _process(delta: float) -> void:
if Input.is_action_just_pressed("click"):
var mouse_position = get_global_mouse_position()
//获取起始位置和终点位置
var start_coord = map.local_to_map(global_position)
var target_coord = map.local_to_map(mouse_position)
// 出问题的代码
var calculate_path = map.astar.get_id_path(start_coord,target_coord)
// 用于检测是否成功获取path
if target_path:
print("we finish!")
其中,被引用的map来自于随机地图生成节点,主要代码如下:
var astar := AStarGrid2D.new()
func _ready():
astar.region = get_used_rect()
astar.cell_size = tile_set.tile_size
astar.update()
结语:英语渣,看不懂报错,翻译照样看不懂,参照别人4.2的代码做的,因为版本改动前面也有做优化,但这里报错属实想不明白。求指点。