【问题标题】:Variable is nii?变量是nii?
【发布时间】:2020-10-30 17:58:25
【问题描述】:

我最近正在使用 LOVE2D 和 Lua 制作游戏。我正在更新 Breakout,Paddle.lua 中有错误。

代码:

Paddle = Class{}

--[[
    Our Paddle will initialize at the same spot every time, in the middle
    of the world horizontally, toward the bottom.
]]

size = math.random(4)

function Paddle:init(skin, size)
    -- x is placed in the middle
    self.x = VIRTUAL_WIDTH / 2 - 32

    -- y is placed a little above the bottom edge of the screen
    self.y = VIRTUAL_HEIGHT - 32

    -- start us off with no velocity
    self.dx = 0
    
    self.size = size
    
    self.height = 16
    if self.size == 1 then
        self.width = 32
    elseif self.size == 3 then
        self.width = 96
    elseif self.size == 4 then
        self.width = 128
    else
        self.width = 64
    end

    -- the skin only has the effect of changing our color, used to offset us
    -- into the gPaddleSkins table later
    self.skin = skin
end

function Paddle:update(dt)
    -- keyboard input
    if love.keyboard.isDown('left') then
        self.dx = -PADDLE_SPEED
    elseif love.keyboard.isDown('right') then
        self.dx = PADDLE_SPEED
    else
        self.dx = 0
    end

    -- math.max here ensures that we're the greater of 0 or the player's
    -- current calculated Y position when pressing up so that we don't
    -- go into the negatives; the movement calculation is simply our
    -- previously-defined paddle speed scaled by dt
    if self.dx < 0 then
        self.x = math.max(0, self.x + self.dx * dt)
    -- similar to before, this time we use math.min to ensure we don't
    -- go any farther than the bottom of the screen minus the paddle's
    -- height (or else it will go partially below, since position is
    -- based on its top left corner)
    else
        self.x = math.min(VIRTUAL_WIDTH - self.width, self.x + self.dx * dt)
    end
end

--[[
    Render the paddle by drawing the main texture, passing in the quad
    that corresponds to the proper skin and size.
]]
function Paddle:render()
    love.graphics.draw(gTextures['main'], gFrames['paddles'][self.size + 4 * (self.skin - 1)],
        self.x, self.y)
end

错误:

Error

src/Paddle.lua:83: attempt to perform arithmetic on field 'size' (a nil value)


Traceback

src/Paddle.lua:83: in function 'render'
src/states/ServeState.lua:68: in function 'render'
src/StateMachine.lua:26: in function 'render'
main.lua:210: in function 'draw'
[C]: in function 'xpcall'

说值是 nii 即使我分配了它。我该如何解决这个问题?

【问题讨论】:

  • 如果您不希望它成为桨的变量,请将大小移动到 paddle:init 内部或将其放入 love:load()。或者尝试找出另一种在 lua 中使用全局变量的方法。

标签: lua love2d


【解决方案1】:

函数参数是函数体的局部参数。所以在Paddle.Initsize里面是一个局部变量,它会遮蔽全局变量size

当您在不提供 size 参数的情况下调用 Paddle.Init 时,size 在导致观察到错误的函数中是 nil

Wikipedia: Variable Shadowing

许多人使用前缀来表示全局变量。 g_size 例如。然后你可以这样做:

g_size = 4

function Paddle:Init(skin, size)
  self.size = size or g_size
end

如果未提供size 参数,这将使self.size 默认为g_size

另一种选择是将全局大小作为输入调用 paddle 构造函数。

【讨论】:

  • 感谢您的帮助,@Piglet。我会尝试,如果有效,我会检查你的答案。
猜你喜欢
  • 2014-09-20
  • 1970-01-01
  • 2018-01-10
  • 1970-01-01
  • 2015-08-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多