Lua代码报错:缺少end闭合行102函数,求排查解决
问题定位与修正
你的错误根源是多个匿名函数未用end闭合,Lua语法要求每个function()必须对应一个end,你目前只给scissorsButton的tap事件函数加了end,但前面三层函数都没闭合:
- 最外层的
button:addEventListener("tap", function() rockButton:addEventListener("tap",function()paperButton:addEventListener("tap",function()
以下是补全end并调整缩进后的完整代码:
button:addEventListener("tap", function() buttonText.isVisible=false button.isVisible=false sun.isVisible=false myText.isVisible=false myText=display.newText("Rock, Paper, Scissors",w*.5,h*.1,native.systemFont,30) myText:setFillColor(unpack(limegreen)) options={"rock","paper","scissors"} computerChoice=options[math.random(3)] local rockButton = display.newRoundedRect(w*.5,h*.3,w*.5,h*.2,25) rockButton.anchorX=.5 rockButton.anchorY=.5 rockButton:setFillColor(unpack(redorange)) local rockText=display.newText("Rock",w*.5,h*.3,systemFont,50) rockText:setFillColor(unpack(blue)) local paperButton = display.newRoundedRect(w*.5,h*.6,w*.5,h*.2,25) paperButton.anchorX=.5 paperButton.anchorY=.5 paperButton:setFillColor(unpack(redorange)) local paperText=display.newText("Paper",w*.5,h*.6,systemFont,50) paperText:setFillColor(unpack(blue)) local scissorsButton = display.newRoundedRect(w*.5,h*.9,w*.5,h*.2,25) scissorsButton.anchorX=.5 scissorsButton.anchorY=.5 scissorsButton:setFillColor(unpack(redorange)) local scissorsText=display.newText("Scissors",w*.5,h*.9,systemFont,50) scissorsText:setFillColor(unpack(blue)) rockButton:addEventListener("tap",function() rockButton.isVisible=false rockText.isVisible=false paperButton.isVisible=false paperText.isVisible=false scissorsButton.isVisible=false scissorsText.isVisible=false playerChoice="rock" local computerChoiceText=display.newText("Computer's Choice: "..computerChoice,w*.5,h*.3,native.systemfont,25) if computerChoice=="paper" then local whoWonText=display.newText("Player Choice: "..playerChoice.."You LOST!",w*.5,h*.5,native.systemFont,20) elseif computerChoice=="scissors" then local whoWontText=display.newText("Player Choice: "..playerChoice.."You WON!",w*.5,h*.5,native.systemFont,20) else local whoWontText=display.newText("Player Choice: "..playerChoice.."You TIE!",w*.5,h*.5,native.systemFont,20) end end) -- 闭合rockButton的tap函数 paperButton:addEventListener("tap",function() rockButton.isVisible=false rockText.isVisible=false paperButton.isVisible=false paperText.isVisible=false scissorsButton.isVisible=false scissorsText.isVisible=false playerChoice="paper" local computerChoiceText=display.newText("Computer's Choice: "..computerChoice,w*.5,h*.3,native.systemfont,25) if computerChoice=="paper" then local whoWonText=display.newText("Player Choice: "..playerChoice.."You Tie!",w*.5,h*.5,native.systemFont,20) elseif computerChoice=="scissors" then local whoWontText=display.newText("Player Choice: "..playerChoice.."You WON!",w*.5,h*.5,native.systemFont,20) else local whoWontText=display.newText("Player Choice: "..playerChoice.."You TIE!",w*.5,h*.5,native.systemFont,20) end end) -- 闭合paperButton的tap函数 scissorsButton:addEventListener("tap",function() rockButton.isVisible=false rockText.isVisible=false paperButton.isVisible=false paperText.isVisible=false scissorsButton.isVisible=false scissorsText.isVisible=false playerChoice="scissors" local computerChoiceText=display.newText("Computer's Choice: "..computerChoice,w*.5,h*.3,native.systemfont,25) if computerChoice=="paper" then local whoWonText=display.newText("Player Choice: "..playerChoice.."You WON!",w*.5,h*.5,native.systemFont,20) elseif computerChoice=="scissors" then local whoWontText=display.newText("Player Choice: "..playerChoice.."You TIE!",w*.5,h*.5,native.systemFont,20) else local whoWontText=display.newText("Player Choice: "..playerChoice.."You LOST!",w*.5,h*.5,native.systemFont,20) end end) -- 闭合scissorsButton的tap函数 end) -- 闭合最外层的button tap函数
额外建议:写嵌套函数时用缩进区分层级,能直观看到哪个function缺了end,避免类似语法错误。
内容的提问来源于stack exchange,提问作者Draconian
相关产品推荐
相关产品推荐

