Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
动态分配对象二维数组lua_Lua - Fatal编程技术网

动态分配对象二维数组lua

动态分配对象二维数组lua,lua,Lua,下面是我试图编写的代码。我试图在lua中将对象动态分配到2d数组。它返回一个错误,表示尝试在线网格[i][j]=diceclass处索引全局零值。新建((i+2)/10),((j+2)/10))?有没有一种方法可以解决这个问题,或者我正在尝试做的事情,动态地将一个对象分配给数组的每个元素 local diceClass = require( "dice" ) grid={} for i =1,5 do grid[i]= {} for j =1,5 do grid[i]

下面是我试图编写的代码。我试图在lua中将对象动态分配到2d数组。它返回一个错误,表示尝试在线网格[i][j]=diceclass处索引全局零值。新建((i+2)/10),((j+2)/10))?有没有一种方法可以解决这个问题,或者我正在尝试做的事情,动态地将一个对象分配给数组的每个元素

local diceClass = require( "dice" )
grid={}
for i =1,5 do
grid[i]=  {}
    for j =1,5 do

        grid[i][j]=diceclass.new( ((i+2)/10),((j+2)/10))
    end
end




--dice class
local dice = {}
local dice_mt = { __index = dice } 


function dice.new( posx, posy) 
a=math.random(1,6)
local newdice = display.newText(a, display.contentWidth*posx,
                                display.contentHeight*posy, nil, 60)

return setmetatable( newdice, dice_mt )
end



return dice

diceClass
diceClass

的变量不同,问题出在哪里?你的问题是什么?我想创建一个for循环,它将创建一个二维对象数组。在第一线:网格[i][j]=diceclass.new((i+2)/10),((j+2)/10)我收到一个错误,说我试图索引全局,这是因为我事先没有声明对象。在lua中创建2d数组时,有没有办法修复此错误或动态分配对象?@David,一个输入错误,不是语法错误。但错误消息应该告诉您:
尝试索引全局类“(零值)