You need to enable JavaScript to run this app. Godot引擎中如何为Line2D的单个添加点配置独立超时计时器并自动移除?
优惠活动
大模型
产品
解决方案
定价
更多

Godot引擎中如何为Line2D的单个添加点配置独立超时计时器并自动移除?

原代码的核心问题
  • 所有新建Timer都绑定到同一个无参的_on_timer_timeout回调,超时后完全无法区分自身对应Line2D中的哪个点位
  • 回调内硬编码删除索引0的点位,不管哪个Timer触发都会移除首个点,完全匹配不上绑定关系
  • 把add_child(active_line)放在鼠标移动加点位的分支循环内,每次新增点都会重复挂载Line2D节点,属于冗余操作
  • 创建的Timer未做生命周期管理,触发超时后会一直留在节点树中,长期运行容易产生内存泄漏
基础调整实现(适配固定时序场景)

如果你的点位超时删除顺序是固定的(比如先加的点先超时,不存在前面的点没删、后面的点先触发超时的情况),可以通过回调传参的方式给每个Timer绑定对应点位的索引:

  1. 提前在_ready阶段初始化并挂载Line2D,避免重复挂载
  2. 新建Timer时通过connect的绑定参数,把当前要添加的点位索引传入超时回调
  3. 回调触发时直接根据传入的索引删除对应点位,删除前先做索引越界判断

调整后的基础代码:

var active_line: Line2D

func _ready():
    # 初始化时一次性挂载Line2D
    active_line = Line2D.new()
    add_child(active_line)

func _input(event):
    if event is InputEventMouseMotion:
        if pressed:
            # 记录新点位的索引
            var new_point_index = active_line.get_point_count()
            active_line.add_point(event.position)

            # 创建独立计时器
            var point_timer = Timer.new()
            # 把点位索引作为额外参数绑定到回调
            point_timer.connect("timeout", self, "_on_point_timeout", [new_point_index])
            point_timer.one_shot = true
            add_child(point_timer)
            point_timer.start(3.0)

        elif !pressed:
            active_line.clear_points()

# 回调接收传入的点位索引参数
func _on_point_timeout(point_index: int):
    # 先判断索引是否合法,避免越界报错
    if active_line.get_point_count() > point_index:
        active_line.remove_point(point_index)
优化实现(解决索引偏移问题)

注意:上述基础实现存在索引错位风险——如果两个点位的超时时间存在交叉,靠前的点被提前删除后,后续所有点的索引会自动前移,原来绑定的索引值就会和实际点位不匹配。
可以通过坐标匹配的方式彻底规避这个问题:

  • 用字典维护Timer和对应点位坐标的映射关系
  • 回调触发时根据存储的坐标值,在Line2D的现有点位中匹配实际索引,再执行删除
  • 超时后自动清理映射记录、销毁Timer节点,避免资源泄漏

优化后的核心代码:

var active_line: Line2D
# 维护Timer与对应点位坐标的映射关系
var point_timer_map = {}

func _ready():
    active_line = Line2D.new()
    add_child(active_line)

func _input(event):
    if event is InputEventMouseMotion:
        if pressed:
            var point_pos = event.position
            active_line.add_point(point_pos)

            var point_timer = Timer.new()
            # 回调传入Timer自身引用和对应点位坐标
            point_timer.connect("timeout", self, "_on_point_timeout", [point_timer, point_pos])
            point_timer.one_shot = true
            add_child(point_timer)
            point_timer.start(3.0)

            # 存储映射关系
            point_timer_map[point_timer] = point_pos

        elif !pressed:
            active_line.clear_points()

func _on_point_timeout(timer: Timer, target_pos: Vector2):
    var remove_index = active_line.get_point_count()
    # 遍历现有点位找到目标坐标对应的实际索引
    for i in range(active_line.get_point_count()):
        if active_line.get_point_position(i) == target_pos:
            remove_index = i
            break
    # 索引合法就执行删除
    if remove_index < active_line.get_point_count():
        active_line.remove_point(remove_index)
    # 清理映射记录
    point_timer_map.erase(timer)
    # 销毁过期的Timer节点
    timer.queue_free()

内容的提问来源于stack exchange,提问作者developer555

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.05 01:15:06