3

Hy there! currently i'm working on a "simple" animation system for my game. And i have some problems with tables. I made a settings file, to load the informations of the animations (to be specific, it loads the alias of the animation, and the number of frames it contains) so, my settings file looks like this: (animsettings.lua)

animlist = {idle, run}

animlist.idle = { frames, alias }
animlist.idle.frames = 1
animlist.idle.alias = "idle"

animlist.run = { frames, alias }
animlist.run.frames = 5
animlist.run.alias = "run"

And i want to access each animation's properties (frames, and alias) with their indexes like this: animlist[1][1], and that should be the value of animlist.idle.frames, which is 1. So, am I misunderstanding something, or this thing should work? because, when I try to print animlist[1][1] or animlist[1].frames, it prints a "nil" but when i print animlist.idle.frames, it prints the actual value which is 1.

So it loads the file correctly, but i can't seem to use their indexes.

Edit: I forgot to show, the function which tries to access these tables: The run animation contains 5 frames (per direction, so 1-r.png 2-r.png etc.) This function's purpose is to load the animation files and to add a table to player.anims so it'll be like this: player.anims.run.left , which gets its name from variables from the function which uses the resources from the animsettings.lua

function initAnims()
    player.anims = {}
    getAnimSettings('gfx/players/'..player.modelname..'/animsettings.lua')

    for i = 1, #animlist do
        for j = 1, #animlist[i][1] do
                animlist[i][2] = {
                    left = ('gfx/players/'..player.modelname..'/'..animlist[i][2]..'/'..animlist[i]..j..'-l.png'),
                    right = ('gfx/players/'..player.modelname..'/'..animlist[i][2]'/'..animlist[i]..j..'-r.png')
                }
                table.insert(player.anims, animlist[i][2])
        end
    end
end

Edit2: now i realised that with this function every iteration i replace the actual frame with the next frame, so i'll make another table inside the that table that conatins each frame's data, but my question is still about the tables, and indexing, i think i'll be able to correct the function after that D:

4

2 回答 2

3
animlist = {idle, run}

此行初始化animlist,使其成为具有键 1 和 2 的表的数组,它们对应的值是表idlerun

现在,当你写

animlist.idle = { frames, alias }

您尝试访问与 的名称(字符串)对应的成员"idle"animlist该成员不是键 1 处的成员。

您要做的是在键 1 和 2 处初始化两个子表,而不是使用名称。首先,删除以下行:

animlist.idle = { frames, alias }

这是多余的,因为您已经在索引 1 处获得了一个表。然后将其余的更改为

animlist[1].frames = 1
animlist[1].alias = "idle"

等等,也可以使用第二个内表来执行此操作。

于 2013-10-28T16:44:26.517 回答
0

如果您将表中的值作为主表值,那么在表初始化后向表中添加值也是一个好主意:

animlist = {}
animlist.run = {}
animlist.frames = 5
animlist.alias = "run"
print(animlist.run.alias)

如果 animlist[1][1] 返回 nil,那么最好的方法是使用此方法:

print(animlist.run.frames)
于 2013-10-28T16:43:52.823 回答