我正在使用 Love2d 和 Lua 制作游戏。目前,我有一个人,从左到右“滑行”。我希望能够限制他的动作,这样他就不会掉出屏幕。我试着做一个 if 语句来检测他的 X 是否大于 800,(因为我的窗口大小是 800x600)但它最终还是出现了故障。这是代码。请帮忙?
function love.load()
love.graphics.setBackgroundColor(92,217,255)
person={}
person.image=love.graphics.newImage('/sprites/spriteTest.png')
person.x=400
person.y=303
person.speed=200
hills=love.graphics.newImage('/sprites/spriteHills.png')
end
function love.update(dt)
if (person.x>735) then
if (love.keyboard.isDown('right') or love.keyboard.isDown('d')) then
if (love.keyboard.isDown('left') or love.keyboard.isDown('a')) then
person.x=person.x+(person.speed*dt)
else
person.x=person.x
end
elseif (love.keyboard.isDown('left') or love.keyboard.isDown('a')) then
if (love.keyboard.isDown('right') or love.keyboard.isDown('d')) then
person.x=person.x+(person.speed*dt)
else
person.x=person.x
end
end
elseif (person.x<0) then
if (love.keyboard.isDown('right') or love.keyboard.isDown('d')) then
if (love.keyboard.isDown('left') or love.keyboard.isDown('a')) then
person.x=person.x+(person.speed*dt)
else
person.x=person.x
end
elseif (love.keyboard.isDown('left') or love.keyboard.isDown('a')) then
if (love.keyboard.isDown('right') or love.keyboard.isDown('d')) then
person.x=person.x+(person.speed*dt)
else
person.x=person.x
end
end
else
if (love.keyboard.isDown('right') or love.keyboard.isDown('d')) then
person.x=person.x+(person.speed*dt)
elseif (love.keyboard.isDown('left') or love.keyboard.isDown('a')) then
person.x=person.x-(person.speed*dt)
end
end
end
function love.draw()
love.graphics.draw(hills, 0, 0)
love.graphics.draw(person.image, person.x, person.y)
end