简体   繁体   English

我在 LOVE2D 中的四边形如何具有零值?

[英]How does my quad in LOVE2D have a nil value?

I'm making a 2nd powerup in my game in LOVE2D what I expect it to do is to grow the paddle that it last collided with.我在 LOVE2D 的游戏中进行了第二次加电,我期望它做的是增加它最后一次碰撞的桨。 However, it gave me an error:但是,它给了我一个错误:

Error

powerups/PaddleGrow.lua:64: bad argument #2 to 'draw' (Quad expected, got nil)


Traceback

[C]: in function 'draw'
powerups/PaddleGrow.lua:64: in function 'render'
main.lua:630: in function 'draw'
[C]: in function 'xpcall'

My quad as a nil value in the draw function, though I actually assigned it in a Util function:我的四边形在绘图 function 中作为零值,尽管我实际上在 Util function 中分配了它:

--[[
    Simple function for making powerups.
]]
function GenerateQuadsPowerups(atlas)
    local x = 0
    local y = 0
    local counter = 1
    local quads = {}
    
    for i = 1, 3 do
        quads[counter] = love.graphics.newQuad(x, y, 8, 8, atlas:getDimensions())
    end
    
    return quads
end

My draw function has this frames and textures thingy in the Dependencies, and I added powerups as a sort.我的绘图 function 在 Dependencies 中有这个框架和纹理,我添加了 powerups 作为一种排序。 My frames is a table of 3 powerups but my 2nd powerup doesn't want to show up.我的框架是一个包含 3 个通电的表格,但我的第二个通电不想出现。

function PaddleGrow:render()
    if self.inPlay then
        love.graphics.draw(textures['powerups'], frames['powerups'][2], self.x, self.y)
    end
end

Why did it turn like this?为什么会变成这样?

Your code seems to be assigning the same index of quads table.您的代码似乎分配了quads表的相同索引。

counter is defined once at the beginning of the function as 1, and you're doing a for loop that only assigns the new quad to 1, three times. counter在 function 的开头定义一次为 1,并且您正在执行一个 for 循环,该循环仅将新四边形分配给 1,三次。

Did you mean quads[i] = love.graphics.newQuad(x, y, 8, 8, atlas:getDimensions()) ?您的意思是quads[i] = love.graphics.newQuad(x, y, 8, 8, atlas:getDimensions())吗?

--[[
    Simple function for making powerups.
]]
function GenerateQuadsPowerups(atlas)
    local x = 0
    local y = 0
    local quads = {}
    
    for i = 1, 3 do
        quads[i] = love.graphics.newQuad(x, y, 8, 8, atlas:getDimensions())
    end
    
    return quads
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM