如何在godot引擎中为添加到Line2d的各个点添加计时器?

问题描述

我正在开发一款使用 line2d 的游戏。我在脚本中的 line2d 上添加点。但我想为 line2d 中添加的各个点添加计时器。如果某个点的计时器超时,则应删除该点。我怎样才能做到这一点?有人可以帮忙吗。

if event is InputEventMouseMotion:
        if pressed:
            active_line.add_point(event.position)
            timer = Timer.new()
            timer.start(3)
            timer.connect("timeout",self,"_on_timer_timeout")
            add_child(active_line)
        
        elif !pressed:
            active_line.clear_points()
            remove_child(active_line)

func _on_timer_timeout():
    active_line.remove_point(0)

解决方法

如果你想在函数中间添加一个简单的计时器,我会推荐这个:

yield(get_tree().create_timer(2.0),"timeout")

2.0 是您希望函数暂停的时间量,"timeout" 是指超时信号,因此在计时器用完后,该进程将继续执行其下方的任何内容.