Lua中。尝试索引全球" a" (零值)

时间:2016-07-10 17:52:51

标签: lua love2d

   local gfx = love.graphics
    return{
        new = function( Image, Animation, Time)        
        return{
        current_frame = 1,
        current_anim = 1,
        image = Image,
        a = Animation,
        play = false,
        time = Time or 0,2,
        counter = 0,

        update = function(dt)
        if play then
                    counter = counter + dt
                    if counter >= time then
                        counter = 0
                        current_frame = current_frame + 1
                    end

                    if current_frame > #a[current_anim] then
                        current_frame = 1
                    end
                else

                end
        end,

        play = function()
        play = true
        end,

        stop = function()
        play = false    
        end,

        set_animation = function(anim)
        if anim > #a then error("there is no animation ", anim) return end
        current_anim = anim

        end,

        draw = function(data)
        gfx.draw(image, a[current_anim][current_frame], data[1], data[2])
        end

        }
        end
    }
  在我看来,我确实给了" a"一个值 - 一个包含一组图像的表,我通过第二个参数分配。

    -----

    local quad = love.graphics.newQuad
    local anim_data = {
      quad(0,0, 32, 48, 192, 256),
      quad(32,0, 32, 48, 192, 256),
      quad(64,0, 32, 48, 192, 256),
      quad(96,0, 32, 48, 192, 256),
      quad(129,0, 32, 48, 192, 256),
      quad(162,0, 32, 48, 192, 256)

    }
    local image = love.graphics.newImage("images/player_animation.png")
    image:setFilter("nearest","nearest")
  

这里我应该给出一个'一个'值

    animation = require("animations"):new(
            image,

            {
              { -- idle
                anim_data[1]
              },
              { -- walk
                anim_data[2],
                anim_data[3],
                anim_data[4],
                anim_data[5],
                anim_data[6]
              },
            },

            0.2
          )

    animation.play()
  

抱歉我的英语很差,并提前致谢

1 个答案:

答案 0 :(得分:0)

您要查找的a是您从new函数返回的表格的一部分。从这些功能的范围看不出来。

最简单的解决方案是将您放在表中的值移到new函数的范围内,封装它们。但是,这会将其从表格中移除,因此在new.play.pause.draw.update之外无法看到它们,或者.set_animation函数。

更好的解决方案是采用更典型的OOP解决方案,因此我建议阅读Lua中编程的第16章:Object-oriented Programming。一旦你阅读了那一章,为了安全起见,请先阅读其他内容(特别是5,6和11)。

local gfx = love.graphics

return {
    new = function (Image, Animation, Time)        
        local current_frame = 1
        local current_anim = 1
        local image = Image
        local a = Animation
        local play = false
        local time = Time or 0,2
        local counter = 0

        return {
            update = function (dt)
                if play then
                    counter = counter + dt
                    if counter >= time then
                        counter = 0
                        current_frame = current_frame + 1
                    end

                    if current_frame > #a[current_anim] then
                        current_frame = 1
                    end
                end
            end,

            play = function ()
                play = true
            end,

            stop = function ()
                play = false
            end,

            set_animation = function (anim)
                if anim > #a then
                    error("there is no animation ", anim)
                    return
                end

                current_anim = anim

            end,

            draw = function (data)
                gfx.draw(image, a[current_anim][current_frame], data[1], data[2])
            end
        }
    end
}