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

Love2d Lua项目报错提示第98行函数缺end 反复核查未找到原因求助

问题根因

你遇到的语法报错是两个错误共同导致的:

  • 第一是Lua多分支语法使用错误:Lua的多条件分支关键字是连写的elseif,而非空格分隔的else if。你在opponent.move函数中写的else if会被Lua解析为else分支内部嵌套了一个独立的if语句,这个嵌套的if仅对应一个end,缺少了关闭外层else所属的一级if的end,导致整个函数的end匹配错位,编译器才会提示98行定义的函数缺少end。
  • 第二是存在笔误:opponent.y = opponent.bottom - opponent.height中误用了不存在的opponent.bottom字段,正确应该是全局定义的screen.bottom。

修正方案

直接替换opponent.move函数的内容即可:

function opponent.move(dt)
    if ball.y + ball.height < opponent.y then
        opponent.y = opponent.y - opponent.speed
    elseif ball.y > opponent.y + opponent.height then -- 把空格分隔的else if改为连写的elseif
        opponent.y = opponent.y + opponent.speed
    end

    if opponent.y < screen.top then
        opponent.y = screen.top
    elseif opponent.y + opponent.height > screen.bottom then
        opponent.y = screen.bottom - opponent.height -- 把opponent.bottom改为screen.bottom
    end
end

内容的提问来源于stack exchange,提问作者A.L.DOC_042

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.01 20:57:03