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)
Especially that function is most likely not doing what you want:
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
In words: you pass animation
and maxFrames
, but never use animation
and immediately replace it with your grid. Then you add your grid to the self.grids
array, and since its the first entry it will get the index/key 1
. Therefore shop.grids.shopAnim
is nil, because you never set it. shop.grids[1]
would work.