1

我正在使用 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
4

2 回答 2

0

update这个方法怎么样:

function love.update(dt)
  if ((love.keyboard.isDown('right') or love.keyboard.isDown('d')) and person.x < 735) then
    person.x = person.x + person.speed * dt
  end
  if ((love.keyboard.isDown('left') or love.keyboard.isDown('a')) and person.x > 0) then
    person.x = person.x - person.speed * dt
  end
end

基本上你想说if a movement key is down 然后 the object can move移动。

我也将使用精灵底部边缘的中心作为枢轴点。然后你需要使用ox = 32and ox = 64(原点偏移)假设你有一个 64 x 64 的精灵。

于 2013-02-09T07:05:31.803 回答
0

我找到了答案。这是代码

function love.update(dt)

if (player.x>735) then

    if (love.keyboard.isDown('left') or love.keyboard.isDown('a') or love.keyboard.isDown('right') or love.keyboard.isDown('d')) then
        player.x=player.x-(player.speed*dt)
    end

elseif (player.x<-10) then

    if (love.keyboard.isDown('left') or love.keyboard.isDown('a') or love.keyboard.isDown('right') or love.keyboard.isDown('d')) then
        player.x=player.x+(player.speed*dt)
    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

它可能看起来不会修复它,但它为我修复了它。我只是设置它,如果他处于边缘,它不会让他走得更远。

于 2013-02-09T21:14:29.260 回答