【问题标题】:love2d simple draw exchange picturelove2d简单画交换图片
【发布时间】:2013-12-28 01:12:11
【问题描述】:

我对 Lua 和 Love2D 完全陌生,可能根本不理解这些概念。 嗯,这是一个 Love2D 教程,我想改变它,例如,当我在键盘上按“a”时,对象会交换(从仓鼠到汽车)等等。

你能帮帮我吗?

-- 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

【问题讨论】:

    标签: lua love2d


    【解决方案1】:

    我建议使用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
    

    【讨论】:

    • 谢谢 Corbin,但它不起作用,我有与当地状态有关的错误。错误:main.lua:12:尝试索引 upvalue 'state'(一个 nil 值)堆栈回溯:main.lua:12:在函数“加载”[string“boot.lua”]:378:在函数 [C]: 在函数'xpcall'中
    • 哎呀。未经测试的代码。很高兴它为您指明了正确的方向。
    • 谢谢。现在我要弄清楚屏幕边界,所以图像不会“掉出”屏幕。
    【解决方案2】:

    感谢 Corbin,我发现它没有“状态”局部变量。你的解决方案对我很有启发。现在它正在工作。

      -- 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
    

    【讨论】:

      猜你喜欢
      • 2013-01-25
      • 1970-01-01
      • 2022-10-23
      • 2016-04-13
      • 2020-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多