索引全局"self"时出错?
error indexing global "self"?
每当我通过LOVE启动游戏时,我都会收到一个错误代码,告诉我"无法索引本地"self"-一个数值"。我无论如何也找不到那个错误。这阻碍了我的游戏进程,这真的很烦人。它是用LUA/Love格式写的,有人能帮我吗?
local ent = ents.Derive("base")
function ent:load( x, y )
self:setPos( x, y)
self.w = 64
self.h = 64
end
function ent:setSize( w, h )
self.w = w
self.h = h
end
function ent:getSize()
return self.w, self.h;
end
function ent:update(dt)
self.y = self.y + 32*dt
end
function ent:draw()
local x, y = self:getPos()
local w, h = self:getSize()
love.graphics.setColor(0, 0, 0, 255)
love.graphics.rectangle("fill", x, y, w, h )
end
return ent;
我在其他一些文件中调用ent:update函数。(注意,上述代码存储在另一个文件夹中,该文件夹保存所有实体.lua文件)
function ents:update(dt)
for i, ent in pairs(ents.objects) do
if ent.update(dt) then
ent:update(dt)
end
end
end
和
function love.update(dt)
xCloud = xCloud + 64*dt
if xCloud >= (800 + 256) then
xCloud = 0
end
yCloud = yCloud + 32*dt
if yCloud >= (800 + 256) then
yCloud = 0
end
zCloud = zCloud + 16*dt
if zCloud >= (800 + 256) then
zCloud = 0
end
ents:update(dt)
end
"不能索引本地"self" -一个数值。"
你定义了。像这样更新:
function ent:update(dt)
self.y = self.y + 32*dt
end
这是语法糖
function ent.update(self, dt)
self.y = self.y + 32*dt
end
换句话说,它要求将self作为第一个参数传递。
然后像这样调用ent.update
:
if ent.update(dt) then
ent:update(dt)
end
第二行是正确的。1号线不是。你在给自己传号码。当它试图索引时,你会得到"不能索引本地'self' -一个数值"。
问题是ents:update
函数中的if ent.update(dt) then
调用。
你说的是if ent:update(dt) then
。
:
函数调用语法只是语法糖,所以ent:update(dt)
只是ent.update(ent, dt)
的糖(这显然不同于ent.update(dt)
,并解释了你得到的错误)。
参见Lua手册中的函数调用。
调用v:name(args)是v.name(v,args)的语法糖,只是v只求值一次。
相关文章:
- 访问者访问变体并返回不同类型时出错
- 在Linux for Windows上编译C++代码时出错
- 读取文件的最后一行并输入到链接列表时出错
- 重载操作程序时出错>>用于类中的字符串 memebr
- 调用专用模板时出错"no matching function for call to [...]"
- C++-试图将函数指针推回到另一个CPP文件中的矢量时出错
- LINK 尝试使用 OpenSSL evp aes 256 c++ 时出错
- 在Google Kick start中提交时出错
- 在c++中访问int到类对象的映射时出错
- 分段错误当我试图运行程序时出错
- 使用dynamic_cast和构造函数时出错
- CHECK(调用)函数在Google Colab中出错
- 用pybind11包装C++抽象类时出错
- 为x86而非x64编译时出错
- 从R调用C++函数并对其进行集成时出错
- 这个函数哪里出错了
- C++ 创建包含链表和字符串的对象的链接列表时出错
- 为重写std::exception的库生成swig接口时出错
- 导入/导出变量时出错
- 索引全局"self"时出错?