错误 area_set_shape_disabled: Can't change this state while flushing queries. Use call_deferred() o...

在Godot中, 直接调用方法去 修改secne_tree (添加、删除子节点), 处理图形碰撞等都会造成线程不安全,以至报错:area_set_shape_disabled: Can't change this state while flushing queries. Use call_deferred() or set_deferred() to change monitoring state instead.

简单的处理办法就是使用 call_deferred() 调用想要执行的内容。如:

# 变更场景
func changeLevel():
    var curScene = get_tree().get_current_scene()
    remove_child(curScene.gsLttleWitch)
    curScene.remove_child(curScene.gsCurLevelScene)
    curScene.gsCurLevelScene.queue_free()

    curScene.gsCurLevelScene = LevelScene_0102.instance()
    curScene.gsCurLevelScene.add_child(curScene.gsLttleWitch)
    curScene.gsLttleWitch.global_position = Vector2(100, 260)
    curScene.add_child(curScene.gsCurLevelScene)
    
    pass


func _on_Area2D_body_entered(body):
    # 如果碰撞体棋子类
    if body is Pawn :
        # 转场
        # get_tree().change_scene("res://assets/scenes/levels/LevelScene_0102.tscn")
        # 为了线程安全 使用call_deferred调用
        call_deferred("changeLevel")
    pass # Replace with function body.

你可能感兴趣的:(错误 area_set_shape_disabled: Can't change this state while flushing queries. Use call_deferred() o...)