【问题标题】:Dynamically assigning objects 2d array lua动态分配对象二维数组lua
【发布时间】:2012-07-18 22:41:47
【问题描述】:

以下是我正在尝试编写的代码。我正在尝试将对象动态分配给 lua 中的二维数组。它返回一个错误,说尝试在行 grid[i][j]=diceclass.new( ((i+2)/10),((j+2)/10)) 处索引全局 nil 值?有没有办法解决这个问题或我正在尝试做的事情,动态地将一个对象分配给数组的每个元素?

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

【问题讨论】:

    标签: lua


    【解决方案1】:

    diceClassdiceclass 不是同一个变量。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-18
      • 2021-02-03
      • 2013-11-24
      • 1970-01-01
      • 2015-02-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多