【发布时间】:2017-09-10 04:12:09
【问题描述】:
我在我的 Map 模块中创建了一个名为 Map 的 lua 表对象,这个函数创建了一个新实例:
function Map:new (o)
o = o or {
centers = {},
corners = {},
edges = {}
}
setmetatable(o, self)
self.__index = self
return o
end
在我的 island 模块中,我在前几行中输入了这段代码:
local map = require (*map module location*)
Island = map:new ()
当我打印中心、角和表格的数量时,它们都为 0。
我有单独的模块用于 Corner:new ()、Center:new () 和 Edge:new ()
为什么中心、角、边的长度输出为0?
编辑:
例如,这是我输入到中心表中的内容(角和边缘相似)
function pointToKey(point)
return point.x.."_"..point.y
end
function Map:generateCenters(centers)
local N = math.sqrt(self.SIZE)
for xx = 1, N do
for yy = 1, N do
local cntr = Center:new()
cntr.point = {x = 0.5+xx - 1, y = 0.5+yy - 1}
centers[pointToKey(cntr.point)] = cntr
end
end
return centers
end
大小总是一个完美的正方形
【问题讨论】:
-
map:new()返回一个表,其中包含指向空表的键corners等。为什么你会期望大小不为零? -
在检查
Island.corners中的元素数量之前,你是在做table.insert( Island.corners, Corner:new() )吗? -
@GoojajiGreg 再检查一次,我明白你的意思,所以我更深入了一点。很抱歉造成混乱
标签: lua scripting instantiation lua-table