table.insert没有添加参数Lua
我制作了这个小动画处理程序代码,我想将函数中定义的动画网格名称添加到另一个表(网格,位于名为 sprite 的表内),但是当我尝试将新网格插入到 < code>sprites.grids 表不起作用,代码在 anim8.newAnimation(grid('1-'...
sattin) 处崩溃,我给出了一个 nil 网格。我不'不明白:(代码:
require("table")
function createSprite(x, y, path)
love.graphics.setDefaultFilter("nearest", "nearest")
local sprite = {
position = {
x = x,
y = y
},
sprite = love.graphics.newImage(path),
currentAnimation,
animations,
grids = {}
}
function sprite:createSpriteGrid(animation, maxFrames)
animation = anim8.newGrid(self.sprite:getWidth() / maxFrames, self.sprite:getHeight(), self.sprite:getWidth(), self.sprite:getHeight())
table.insert(self.grids, animation)
end
function sprite:setAnimation(grid, maxFrames, time)
self.currentAnimation = anim8.newAnimation(grid('1-'.. maxFrames .. '', 1), time)
end
function sprite:update(dt)
self.currentAnimation:update(dt)
end
function sprite:draw(scale)
self.currentAnimation:draw(self.sprite, self.position.x, self.position.y, nil, scale)
end
return sprite
end
shop = createSprite(630, 160, "assets/shop.png")
shop:createSpriteGrid(shopAnim, 6)
shop:setAnimation(shop.grids.shopAnim, 6, 0.17)
I made this little animation handler code, and I want to add the animation grid name, defined in a function, to another table (grids, which is inside a table called sprite), but when I try to insert the new grid into the sprites.grids
table it does not work and the code crashes at anim8.newAnimation(grid('1-'...
sattin that I gave a nil grid. I don't understand. :( Code:
require("table")
function createSprite(x, y, path)
love.graphics.setDefaultFilter("nearest", "nearest")
local sprite = {
position = {
x = x,
y = y
},
sprite = love.graphics.newImage(path),
currentAnimation,
animations,
grids = {}
}
function sprite:createSpriteGrid(animation, maxFrames)
animation = anim8.newGrid(self.sprite:getWidth() / maxFrames, self.sprite:getHeight(), self.sprite:getWidth(), self.sprite:getHeight())
table.insert(self.grids, animation)
end
function sprite:setAnimation(grid, maxFrames, time)
self.currentAnimation = anim8.newAnimation(grid('1-'.. maxFrames .. '', 1), time)
end
function sprite:update(dt)
self.currentAnimation:update(dt)
end
function sprite:draw(scale)
self.currentAnimation:draw(self.sprite, self.position.x, self.position.y, nil, scale)
end
return sprite
end
shop = createSprite(630, 160, "assets/shop.png")
shop:createSpriteGrid(shopAnim, 6)
shop:setAnimation(shop.grids.shopAnim, 6, 0.17)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尤其是该功能很可能不会做您想要的事情:
用文字:您传递
andain
和maxframes
,但切勿使用andimation
,并立即替换它用你的网格。然后,将网格添加到self.grids
数组中,并且由于其第一个条目将获得索引/键1
。因此,shop.grids.shopanim
为零,因为您从未设置它。shop.grids [1]
将起作用。Especially that function is most likely not doing what you want:
In words: you pass
animation
andmaxFrames
, but never useanimation
and immediately replace it with your grid. Then you add your grid to theself.grids
array, and since its the first entry it will get the index/key1
. Thereforeshop.grids.shopAnim
is nil, because you never set it.shop.grids[1]
would work.