lua (love2d): 函数应该没有结束
lua (love2d): function supposedly missing an end
它说第 98 行的函数没有 'end'。我发誓没有遗漏'end',我反复验证也找不到是什么
Error
Syntax error: main.lua:142: 'end' expected (to close 'function' at
line 98) near ''
Traceback
[C]: at 0x07f9a11328f0 [C]: in function 'require' [C]: in function
'xpcall' [C]: in function 'xpcall'
player = {}
opponent = {}
ball = {}
screen ={}
function love.load()
screen.top = 0
screen.bottom = love.graphics.getHeight()
screen.left = 0
screen.right = love.graphics.getWidth()
player.x = 50
player.y = screen.bottom / 2
player.width = 20
player.height = 100
player.speed = 500
opponent.x = screen.right - 50
opponent.y = screen.bottom / 2
opponent.width = 20
opponent.height = 100
opponent.speed = 400
ball.x = screen.right / 2 - 10
ball.y = screen.bottom / 2 - 10
ball.width = 20
ball.height = 20
ball.speed = 200
ball.xVel = -ball.speed
ball.yVel = 0
end
function love.update(dt)
player.move(dt)
opponent.move(dt)
ball.move(dt)
ball.collide(dt)
end
function love.draw(dt)
player.draw()
opponent.draw()
ball.draw()
end
function checkCollision(a, b)
if a.x + a.width > b.x and a.x < b.x + b.width and a.y + a.height > b.y and a.y < b.y + b.height then
return true
else
return false
end
end
function player.draw()
love.graphics.rectangle('fill', player.x, player.y, player.width, player.height)
end
function player.move(dt)
if love.keyboard.isDown('w') then
player.y = player.y - player.speed * dt
elseif love.keyboard.isDown('s') then
player.y = player.y + player.speed * dt
end
if player.y < screen.top then
player.y = screen.top
elseif player.y + player.height > screen.bottom then
player.y = screen.bottom - player.height
end
end
function opponent.draw()
love.graphics.rectangle('fill', opponent.x, opponent.y, opponent.width, opponent.height)
end
function opponent.move(dt)
if ball.y + ball.height < opponent.y then
opponent.y = opponent.y - opponent.speed
else if ball.y > opponent.y + opponent.height then
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 = opponent.bottom - opponent.height
end
end
function ball.draw()
love.graphics.rectangle('fill', ball.x, ball.y, ball.width, ball.height)
end
function ball.move(dt)
ball.x = ball.x + ball.xVel * dt
ball.y = ball.y + ball.yVel * dt
if ball.y < screen.top then
ball.y = screen.top
ball.yVel = -ball.yVel
elseif ball.y + ball.height > screen.bottom then
ball.y = screen.bottom
ball.yVel = -ball.yVel
end
end
function ball.collide(dt)
if checkCollision(ball, player) then
ball.xVel = -ball.xVel
local middleBall = ball.y + (ball.height / 2)
local middlePlayer = player.y + (player.height / 2)
local collisionPosition = middleBall - middlePlayer
ball.yVel = collisionPosition
end
end
我将其粘贴到 vi
中并突出显示语法,第一眼看到 opponent.move(dt)
很明显缺少蓝色 end
来关闭函数块。
但我不想决定丢失的 end
属于哪个 if
更正它。
哎呀,我明白了。
你做一个 else if
而不是: elseif
它说第 98 行的函数没有 'end'。我发誓没有遗漏'end',我反复验证也找不到是什么
Error
Syntax error: main.lua:142: 'end' expected (to close 'function' at line 98) near ''
Traceback
[C]: at 0x07f9a11328f0 [C]: in function 'require' [C]: in function 'xpcall' [C]: in function 'xpcall'
player = {}
opponent = {}
ball = {}
screen ={}
function love.load()
screen.top = 0
screen.bottom = love.graphics.getHeight()
screen.left = 0
screen.right = love.graphics.getWidth()
player.x = 50
player.y = screen.bottom / 2
player.width = 20
player.height = 100
player.speed = 500
opponent.x = screen.right - 50
opponent.y = screen.bottom / 2
opponent.width = 20
opponent.height = 100
opponent.speed = 400
ball.x = screen.right / 2 - 10
ball.y = screen.bottom / 2 - 10
ball.width = 20
ball.height = 20
ball.speed = 200
ball.xVel = -ball.speed
ball.yVel = 0
end
function love.update(dt)
player.move(dt)
opponent.move(dt)
ball.move(dt)
ball.collide(dt)
end
function love.draw(dt)
player.draw()
opponent.draw()
ball.draw()
end
function checkCollision(a, b)
if a.x + a.width > b.x and a.x < b.x + b.width and a.y + a.height > b.y and a.y < b.y + b.height then
return true
else
return false
end
end
function player.draw()
love.graphics.rectangle('fill', player.x, player.y, player.width, player.height)
end
function player.move(dt)
if love.keyboard.isDown('w') then
player.y = player.y - player.speed * dt
elseif love.keyboard.isDown('s') then
player.y = player.y + player.speed * dt
end
if player.y < screen.top then
player.y = screen.top
elseif player.y + player.height > screen.bottom then
player.y = screen.bottom - player.height
end
end
function opponent.draw()
love.graphics.rectangle('fill', opponent.x, opponent.y, opponent.width, opponent.height)
end
function opponent.move(dt)
if ball.y + ball.height < opponent.y then
opponent.y = opponent.y - opponent.speed
else if ball.y > opponent.y + opponent.height then
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 = opponent.bottom - opponent.height
end
end
function ball.draw()
love.graphics.rectangle('fill', ball.x, ball.y, ball.width, ball.height)
end
function ball.move(dt)
ball.x = ball.x + ball.xVel * dt
ball.y = ball.y + ball.yVel * dt
if ball.y < screen.top then
ball.y = screen.top
ball.yVel = -ball.yVel
elseif ball.y + ball.height > screen.bottom then
ball.y = screen.bottom
ball.yVel = -ball.yVel
end
end
function ball.collide(dt)
if checkCollision(ball, player) then
ball.xVel = -ball.xVel
local middleBall = ball.y + (ball.height / 2)
local middlePlayer = player.y + (player.height / 2)
local collisionPosition = middleBall - middlePlayer
ball.yVel = collisionPosition
end
end
我将其粘贴到 vi
中并突出显示语法,第一眼看到 opponent.move(dt)
很明显缺少蓝色 end
来关闭函数块。
但我不想决定丢失的 end
属于哪个 if
更正它。
哎呀,我明白了。
你做一个 else if
而不是: elseif