在我的 2D 游戏中,玩家能够摧毁箱子、具有两种碰撞形状的物体。当被摧毁时,板条箱会产生也具有碰撞形状的物品。但是当调用以下函数时,Godot控制台中会显示许多类似的错误
代码:
func _on_Crate_item_dropped(collectible, pos):
collectible.init(pos, Vector2(rand_range(30, 100), rand_range(-10, 10)))
$CollectibleContainer.add_child(collectible) # error occurs here
Run Code Online (Sandbox Code Playgroud)
错误:
ERROR: Can't change this state while flushing queries. Use call_deferred() or set_deferred() to change monitoring state instead.
Run Code Online (Sandbox Code Playgroud)
And*_*Uho 14
该方法call_deferred()在空闲时间调用对象上的方法。它的第一个参数是方法名称字符串,其他参数是方法参数。
代替
$CollectibleContainer.add_child(collectible)
Run Code Online (Sandbox Code Playgroud)
和
$CollectibleContainer.call_deferred("add_child", collectible)
Run Code Online (Sandbox Code Playgroud)