2013-12-10 66 views
1

我對Lua和Love2D完全陌生,可能根本不理解這些概念。 這是一個Love2D教程,我想改變它,所以當我按下「a」時,例如在鍵盤上,對象將交換(從倉鼠到汽車)等等。love2d簡單繪製交換圖片

你能幫我嗎?

-- Tutorial 1: Hamster Ball 
-- Add an image to the game and move it around using 
-- the arrow keys. 
-- compatible with löve 0.6.0 and up 

function love.load() 
    hamster = love.graphics.newImage("hamster.png") 
    auto = love.graphics.newImage("auto.png") 
    x = 50 
    y = 50 
    speed = 300 
end 

function love.update(dt) 
    if love.keyboard.isDown("right") then 
     x = x + (speed * dt) 
    end 
    if love.keyboard.isDown("left") then 
     x = x - (speed * dt) 
    end 

    if love.keyboard.isDown("down") then 
     y = y + (speed * dt) 
    end 
    if love.keyboard.isDown("up") then 
     y = y - (speed * dt) 
    end 
    if love.keyboard.isDown("escape") then 
     love.event.quit() 
    end 
    if love.keyboard.isDown("a") then 
     love.draw(auto,x,y) 
    end 
end 

function love.draw() 
    love.graphics.draw(hamster, x, y) 
end 

回答

0

我建議使用love.update只更新狀態。不要吸引它。然後在love.draw中完成所有繪圖。一個解決方案可能是:

local state = {} 

function love.load() 
    hamster = love.graphics.newImage("hamster.png") 
    auto = love.graphics.newImage("auto.png") 
    state.activeImage = hamster 
    state.activeImageName = "hamster" 
    -- <snip> ... 
end 

function love.update(dt) 
    -- <snip> ... 
    if love.keyboard.isDown("a") then 
     if state.activeImageName == "hamster" then 
      state.activeImage = auto 
      state.activeImageName = "auto" 
     else 
      state.activeImage = hamster 
      state.activeImageName = "hamster" 
     end 
    end 
end 

function love.draw() 
    love.graphics.draw(state.activeImage, x, y) 
end 
+0

謝謝Corbin,但它不起作用,我有與本地狀態有關的錯誤。 rror:main.lua:12:嘗試索引upvalue'state'(一個零值) 堆棧回溯: \t main.lua:12:在函數'load'中 \t [string「boot.lua」]:378: in function <[string'boot.lua']:373> \t [C]:in function'xpcall' – molni

+0

哎呦。未經測試的代碼。高興地指出你在正確的方向。 –

+0

謝謝。現在我要弄清楚屏幕邊界,所以圖像不會「掉出」屏幕。 – molni

0

非常感謝科爾賓,我想出了沒有「狀態」局部變量。你的解決方案給我鼓舞。現在它正在工作。

-- Tutorial 1: Hamster Ball 
    -- Add an image to the game and move it around using 
    -- the arrow keys. 
    -- compatible with löve 0.6.0 and up 


    function love.load() 
     hamster = love.graphics.newImage("hamster.png") 
     auto = love.graphics.newImage("auto.png") 
     activeImage = hamster 
     activeImageName = "hamster" 
     x = 50 
     y = 50 
     speed = 300 
    end 

    function love.update(dt) 
     if love.keyboard.isDown("right") then 
      x = x + (speed * dt) 
     end 
     if love.keyboard.isDown("left") then 
      x = x - (speed * dt) 
     end 

     if love.keyboard.isDown("down") then 
      y = y + (speed * dt) 
     end 
     if love.keyboard.isDown("up") then 
      y = y - (speed * dt) 
     end 
     if love.keyboard.isDown("escape") then 
      love.event.quit() 
     end 
     if love.keyboard.isDown("a") then 
      activeImage = auto 
      activeImageName = "auto" 
     end 
     if love.keyboard.isDown("h") then 
      activeImage = hamster 
      activeImageName = "hamster" 
     end 

    end 

    function love.draw() 
     love.graphics.draw(activeImage, x, y) 
    end