table.insert没有添加参数Lua

发布于 2025-01-18 14:30:19 字数 1324 浏览 0 评论 0原文

我制作了这个小动画处理程序代码,我想将函数中定义的动画网格名称添加到另一个表(网格,位于名为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

假装爱人 2025-01-25 14:30:19

尤其是该功能很可能不会做您想要的事情:

    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

用文字:您传递andainmaxframes,但切勿使用andimation,并立即替换它用你的网格。然后,将网格添加到self.grids数组中,并且由于其第一个条目将获得索引/键1。因此,shop.grids.shopanim为零,因为您从未设置它。 shop.grids [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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文