1

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)

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.