You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Godot三消游戏点击无响应及数组索引错误求助

Godot三消游戏添加匹配检测后无响应且数组索引错误的解决方法

问题根源分析

  • 数组越界错误:在垂直匹配检测的条件中,你错误地写了all_pieces[i + 1][j + 1],正确的应该是all_pieces[i][j + 1]。这个错误会导致当i接近width最大值时,i+1超出二维数组的第一维长度,触发"Invalid get index '8' (on base: 'Array')"错误,进而导致游戏逻辑阻塞,失去点击/触摸响应。
  • 循环逻辑虽无本质问题,但保持和make_2d_array一致的range()写法会更清晰,不过这不是当前问题的核心。

修正后的find_matches()函数代码

func find_matches():
    for i in width:
        for j in height:
            if all_pieces[i][j] != null:
                var current_color = all_pieces[i][j].color
                # 水平匹配检测
                if i > 0 && i < width - 1:
                    if all_pieces[i - 1][j] != null && all_pieces[i + 1][j] != null:
                        if all_pieces[i - 1][j].color == current_color && all_pieces[i + 1][j].color == current_color:
                            all_pieces[i - 1][j].matched = true
                            all_pieces[i - 1][j].dim()
                            all_pieces[i][j].matched = true
                            all_pieces[i][j].dim()
                            all_pieces[i + 1][j].matched = true
                            all_pieces[i + 1][j].dim()
                # 垂直匹配检测(修正索引错误)
                if j > 0 && j < height - 1:
                    if all_pieces[i][j - 1] != null && all_pieces[i][j + 1] != null:
                        if all_pieces[i][j - 1].color == current_color && all_pieces[i][j + 1].color == current_color:
                            all_pieces[i][j - 1].matched = true
                            all_pieces[i][j - 1].dim()
                            all_pieces[i][j].matched = true
                            all_pieces[i][j].dim()
                            all_pieces[i][j + 1].matched = true
                            all_pieces[i][j + 1].dim()

额外建议

  • 建议在数组访问前始终确认索引范围,或者使用has_index()方法提前判断,避免类似越界错误。
  • 对于Godot新手,开启调试模式(点击运行按钮旁的调试图标),可以更精准定位错误发生的代码行,方便排查问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 03:58:15