我目前正在使用我正在创建的电晕应用程序来坚持这一点。
我的文件结构如下:App->类-> Objects-> Ships
在App文件夹中是main.lua,menu.lua,level.lua和Class.lua。在“类”文件夹中,存在Object.lua。在Objects中,ship.lua,最后在Ships中,是我不同的船,即玩家和敌人。
我遵循了本教程,我的代码几乎与他的代码完全相同(酒吧球员和敌人的职业),但是在Class.lua中仍然收到此错误。
“'setetatable'的错误参数#2(预期为零或表)”
我收到错误的代码是
function Class(Super)
Super = Super or Base
local prototype = setmetatable({}, Super) -- receive error here
prototype.class = prototype
prototype.super = Super
prototype.__index = prototype
return prototype
end
Base = Class()
function Base:new(...)
local instance = setmetatable({}, self)
instance:initialize(...)
return instance
end
function Base:initialize() end
function Base:get()
local Instances = self.Instances
if (not Instances[1]) then local obj = self:new() end
return table.remove(Instances, 1)
end
function Base:dispose()
table.insert(self.Instances, self)
end
我尝试过更改类并将“ setmetatable({},Super)”更改为“ setmetatable(Super,self)”,将所有类都放在一个文件中,我已经阅读了lua文档,要求输入Class.lua mai,菜单和level.lua等,没有任何效果。
任何帮助将不胜感激。
谢谢
function Class(Super)
Super = Super or Base
local prototype = setmetatable({}, Super) -- receive error here
prototype.class = prototype
prototype.super = Super
prototype.__index = prototype
return prototype
end
Base = Class()
按照上面的代码执行。
您先声明一个函数Class
,然后调用它(并将其返回值分配给Base
)。
Class
从Base = Class()
行开始逐步执行。
function Class(Super)
该函数接受一个名为 Super
Super = Super or Base
您允许Super
使用默认值零/不传递参数Base
。该调用Base = Class()
未传递值,因此该行Super = Super or Base
具有Super
as值nil
,因此求值为,Super = nil or Base
但是全局Base
值也为nil,因为尚未将其分配给您,因此您得到了Super = nil
。
local prototype = setmetatable({}, Super)
然后Super
,正如我们刚刚看到的那样,此行尝试仅使用它(从之前的行分配),nil
因此是您的错误。
从教程,你错过了(或者至少在您发布的片断错过)该位是至关重要 local Base
线上面的Class
函数定义。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句