2013-02-08 47 views
0

我使用Love2d和Lua來製作遊戲。目前,我有一個人,從左到右「滑行」。我希望能夠限制他的動作,所以他不會掉出屏幕。我試着做一個if語句來檢測他的X是否大於800,(因爲我的窗口大小是800x600),但它只是結束了毛刺。這是代碼。請幫忙?我需要限制圖片的移動LOVE2D

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 

回答

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 

它似乎不會修復它,但它爲我解決了它。我只是說,如果他處於邊緣,它不會讓他走得更遠。

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 downthe object can move然後移動。

此外,我會使用精靈的底部邊緣的中心作爲支點。那麼假設你有一個64×64的精靈,你將需要使用ox = 32ox = 64(原點偏移)。

+0

感謝您的回答,但昨晚我找到了自己的方式。我現在要回答我自己的問題。感謝您的嘗試,雖然:) – hexagonest 2013-02-09 21:11:23