我需要限制图像运动LOVE2D

时间:2013-02-08 22:42:34

标签: lua love2d

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

2 个答案:

答案 0 :(得分: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然后移动。

另外,我会使用精灵的下边缘的中心作为枢轴点。假设您有64 x 64精灵,则需要使用ox = 32ox = 64(原点偏移)。

答案 1 :(得分: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

它似乎无法修复它,但它为我修复了它。我只是说,如果他在边缘,它不会让他走得更远。