我想做一个像游戏一样的矮人要塞。 Corona SDK是否足够快?我将如何存储每个单独块的数据。由于我无法在Lua中进行枚举,我该如何定义块的类型?
答案 0 :(得分:1)
为什么不能在Lua中进行枚举?只需使用一个简单的表即可。
brickType = { A = 1,
B = 2,
C = 3,
}
myFristBrick = { weight = 500,
volume = 50,
type = brickType.C
}
您可以使用Lua表对非常复杂的数据结构进行建模。因此,存储您的实体数据是您最不应该关注的问题。
答案 1 :(得分:1)
我在元表中使用它来模拟enumTypes
local enumType_MT = {
__newindex = function (t, k, v)
if t [k] then
print ('redeclaring enum type ' .. k .. ' not permited')
else
rawset (t, k, {})
local c
for c = 1, #v do
rawset (t [k], v[c], c)
rawset (t[k], c, v[c])
end
end
end
}
local myEnumTypes = {}
setmetatable( myEnumTypes, enumType_MT )
myEnumTypes ['foo'] = {'brick_A', 'brick_B', 'brick_C'}
myEnumTypes ['doo'] = {'brick_E', 'brick_F', 'brick_G'}
myVar = myEnumTypes['foo'].brick_A
print (myVar) -- outputs 1
myVar = myEnumTypes['foo']['brick_C']
print (myVar) -- outputs 3
myVar = myEnumTypes['foo'][2]
print (myVar) -- outputs 'brick_B'
myVar = myEnumTypes['doo'].brick_A
print (myVar) -- outputs nil as brick_A not in doo
myVar = myEnumTypes['doo']['brick_C']
print (myVar) -- outputs nil as brick_C not in doo
myVar = myEnumTypes['doo'][2]
print (myVar) -- outputs 'brick_F'