如何修复 Godot 中的所有 area2D 函数,使其不在启动时运行?

发布于 2025-01-13 15:44:33 字数 456 浏览 4 评论 0原文

好吧,我已经使用 Godot 一段时间了,并没有遇到任何问题。但是,在我添加了区域 2D 来检测玩家并传送它们(如下所示)后,我遇到了问题。每次我启动游戏时,控制台都会显示这两个函数已经运行,即使起始位置离area2Ds很远。另外,因为它们运行在输入->退出命令,它会在隧道的出口处生成我,而不是在地图的开头。

func _on_Tunnel_body_entered(_body):
    print("entered_tunnel")
    global_position.x = 952.5
    global_position.y = 487

func _on_TunnelBack_body_entered(_body):
    print("exited_tunnel")
    global_position.x = 920
    global_position.y = 635

任何帮助将不胜感激!

Ok, I've been using Godot for a while now, and haven't really had any issues. But, after I added an area2D to detect the player and teleport them as shown below that I run into issues. Every time I start the game, the console shows that both of the functions have already been run, even though the starting location is nowhere near the area2Ds. In addition, because they run in enter -> exit order, it spawns me at the exit of the tunnel, instead of the start of the map.

func _on_Tunnel_body_entered(_body):
    print("entered_tunnel")
    global_position.x = 952.5
    global_position.y = 487

func _on_TunnelBack_body_entered(_body):
    print("exited_tunnel")
    global_position.x = 920
    global_position.y = 635

Any help would be appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

与风相奔跑 2025-01-20 15:44:34

您是否偶然实例化了玩家角色,将其添加到场景中,然后设置其位置(按顺序)?

这是此问题的常见原因。发生的情况是,一旦您将其添加到场景中但在设置其位置之前,它就会与区域发生碰撞。

要解决此问题,请在将其添加到场景之前设置全局位置。


您可以通过以下任何方式暂时禁用该行为:

  • 临时更改图层和遮罩以避免碰撞(您可以使用 set_collision_layer_bitset_collision_mask_bit 来实现)。
  • 暂时禁用碰撞形状(通过将 disabled 设置为 true。但是,使用 set_deferred 以避免在 Godot 仍在进行物理计算时禁用它
  • )碰撞异常(通过调用 add_collision_exception_withremove_collision_exception_with)。
  • 有一个标志,您可以在接受信号的函数中检查该标志来决定它是否应该生效。
  • 将代码中的信号连接一次,因此不会预先连接它们。

Are you, by chance, instancing the player character, adding it to the scene, and then setting its position (in that order)?

That is a common reason for this problem. What happens is that it collides with the areas once you add it to the scene but before you set its position.

To solve it set the global position before adding it to the scene.


You could temporarily disable the behavior by any of these means:

  • Temporarily changing the layers and mask to avoid the collision (you can do it with set_collision_layer_bit and set_collision_mask_bit).
  • Temporarily disabling collision shapes (by setting disabled to true. However, use set_deferred to avoid disabling it while Godot is still doing physics computations)
  • Temporarily adding collision exceptions (by calling add_collision_exception_with and remove_collision_exception_with).
  • Having a flag that you can check in the function that takes the signal to decide if it should take effect or not.
  • Connecting the signals from code once, so they are not connected beforehand.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文