set_global_position 将位置设置为错误的值
set_global_position setting position to the wrong value
嘿,我有一个函数可以将 KinematicBody2D(玩家)body 定位在 Node2D(门户)旁边。 2 个传送门链接在一起,当玩家进入场景 A 中的传送门 X 时,它们将被放置在场景 B 中相应传送门 X 的旁边,反之亦然。这是函数:
func fix_player_position(entry_portal_link_id):
var pos = exit_positions[entry_portal_link_id]
var new_pos: Vector2
print("Current entry points for ")
print(exit_positions)
$player.set_global_position(Vector2(0,0))
print("Player's position before change: " + str($player.get_global_position()))
if portal_list[entry_portal_link_id].landing_point == Direction.Up:
new_pos = Vector2(pos.x, pos.y-64)
elif portal_list[entry_portal_link_id].landing_point == Direction.Down:
new_pos = Vector2(pos.x, pos.y+64)
elif portal_list[entry_portal_link_id].landing_point == Direction.Left:
new_pos = Vector2(pos.x-64, pos.y)
elif portal_list[entry_portal_link_id].landing_point == Direction.Right:
new_pos = Vector2(pos.x+64, pos.y)
$player.set_global_position(new_pos)
print(pos)
print(new_pos)
print($player.get_global_position())
这里我们得到 Node2D 的位置,将玩家 (KinematicBody2D) 位置重置为 (0,0) 根据我们要放置 KinematicBody2D 的哪一侧应用偏移量,然后设置新位置并打印出来所有职位。但是它不能正常工作。这是最后的输出
Current entry points for
{house_link1:(-3807.48999, 11041.799805), house_link2:(-4132.839844, 10655)} # We store the positions in a dictionary using the portal ID
Player's position before change: (-26252.199219, 11936) # This is supposed to be (0,0)
(-4132.839844, 10655) # The pos variable, i.e the stored position of the Node2D
(-4132.839844, 10719) # The new_pos, i.e. the intended position of the character
(-30385.039062, 22655) # The position of the character after setting
# 注释是我添加的,目的是说明要打印的内容。正如您可以清楚地看到 set_global_position 调用没有正确设置位置。然而,它确实在不同的场景中正确地设置了玩家的位置。
场景 A -> 场景 B,正常工作
场景B -> 场景A,突然不能正常工作了。
如您所见,我在这里有点迷路,这是 gdscript 本身的错误吗?还是我是白痴做错了什么?
这是场景 A -> 场景 B -> 场景 A 的完整输出
Current entry points for
{house_link1:(32, -288), house_link2:(-355.494995, -477.505005)}
Player's position before change: (0, 0)
(-355.494995, -477.505005) # pos
(-355.494995, -413.505005) # new_pos
(-355.494995, -413.505005) # players position
Current entry points for
{house_link1:(-3807.48999, 11041.799805), house_link2:(-4132.839844, 10655)}
Player's position before change: (-26252.199219, 11936)
(-4132.839844, 10655) # pos
(-4132.839844, 10719) # new_pos
(-30385.039062, 22655) # players_position
所以我不确定实际问题是什么,但解决方案是更换
$player.set_global_position(new_pos)
和
$player.position = new_pos
代码运行成功。
嘿,我有一个函数可以将 KinematicBody2D(玩家)body 定位在 Node2D(门户)旁边。 2 个传送门链接在一起,当玩家进入场景 A 中的传送门 X 时,它们将被放置在场景 B 中相应传送门 X 的旁边,反之亦然。这是函数:
func fix_player_position(entry_portal_link_id):
var pos = exit_positions[entry_portal_link_id]
var new_pos: Vector2
print("Current entry points for ")
print(exit_positions)
$player.set_global_position(Vector2(0,0))
print("Player's position before change: " + str($player.get_global_position()))
if portal_list[entry_portal_link_id].landing_point == Direction.Up:
new_pos = Vector2(pos.x, pos.y-64)
elif portal_list[entry_portal_link_id].landing_point == Direction.Down:
new_pos = Vector2(pos.x, pos.y+64)
elif portal_list[entry_portal_link_id].landing_point == Direction.Left:
new_pos = Vector2(pos.x-64, pos.y)
elif portal_list[entry_portal_link_id].landing_point == Direction.Right:
new_pos = Vector2(pos.x+64, pos.y)
$player.set_global_position(new_pos)
print(pos)
print(new_pos)
print($player.get_global_position())
这里我们得到 Node2D 的位置,将玩家 (KinematicBody2D) 位置重置为 (0,0) 根据我们要放置 KinematicBody2D 的哪一侧应用偏移量,然后设置新位置并打印出来所有职位。但是它不能正常工作。这是最后的输出
Current entry points for
{house_link1:(-3807.48999, 11041.799805), house_link2:(-4132.839844, 10655)} # We store the positions in a dictionary using the portal ID
Player's position before change: (-26252.199219, 11936) # This is supposed to be (0,0)
(-4132.839844, 10655) # The pos variable, i.e the stored position of the Node2D
(-4132.839844, 10719) # The new_pos, i.e. the intended position of the character
(-30385.039062, 22655) # The position of the character after setting
# 注释是我添加的,目的是说明要打印的内容。正如您可以清楚地看到 set_global_position 调用没有正确设置位置。然而,它确实在不同的场景中正确地设置了玩家的位置。
场景 A -> 场景 B,正常工作
场景B -> 场景A,突然不能正常工作了。
如您所见,我在这里有点迷路,这是 gdscript 本身的错误吗?还是我是白痴做错了什么?
这是场景 A -> 场景 B -> 场景 A 的完整输出
Current entry points for
{house_link1:(32, -288), house_link2:(-355.494995, -477.505005)}
Player's position before change: (0, 0)
(-355.494995, -477.505005) # pos
(-355.494995, -413.505005) # new_pos
(-355.494995, -413.505005) # players position
Current entry points for
{house_link1:(-3807.48999, 11041.799805), house_link2:(-4132.839844, 10655)}
Player's position before change: (-26252.199219, 11936)
(-4132.839844, 10655) # pos
(-4132.839844, 10719) # new_pos
(-30385.039062, 22655) # players_position
所以我不确定实际问题是什么,但解决方案是更换
$player.set_global_position(new_pos)
和
$player.position = new_pos
代码运行成功。