2

代码非常简单。声明两个表,一个简单的和一个多维的:

Player = {X_Pos = 1, Y_Pos = 1, Current_Sprite_Num = 100}


    for j=1, Max_col_length do -- value ofMax_col_length doesn't matter here; positive integer anyway

        MapLayer_B[j] = {}

        for i=1, Max_row_length do --same here
            MapLayer_B[j][i] = 1
        end
    end

然后我尝试做这个操作:

MapLayer_B[Player[X_Pos]][Player[Y_Pos]] = Player[Current_Sprite_Num]

它应该替换 中的第 th 行表的第 thPlayer[X_Pos]元素。相反,我在使用 LÖVE 编译器时遇到了这个错误:Player[Y_Pos]MapLayer_B

  • 错误:尝试索引字段“?” (零值)

我真的不明白为什么会发生这种情况,因为MapLayer_BPlayer表的所有元素都被声明并且没有保持为零。

有任何想法吗?

4

1 回答 1

2

You need to use Player.X_Pos instead of Player[X_Pos] and so on.

The brackets notation will interpret "X_Pos" as a variable and try to access taht key instead (the reason for the error is that undefined variables default to null)

t = {a = 17}

print( t.a ) --dot notation is simpler

print( t["a"] ) --bracket notation expects a string

key = "a" --that string can be from a variable
print( t[key] )
于 2013-08-14T21:38:17.553 回答