如何解决试图索引“自我”的零值
我正在为一个项目上电,当我尝试运行它时,出现一个错误,提示“试图索引“自我”为零”。如果有人帮助我,我将不胜感激。感谢阅读! / p>
PS:是的,我在渲染,初始化和更新功能中使用冒号而不是点。
function Powerup:init()
-- simple positional and dimensional variables
self.x = x
self.y = y
self.width = 11
self.height = 11
self.dy = 0
self.dx = 0
self.inPlay = true
end
--[[
Expects an argument with a bounding box,be that a paddle or a brick,and returns true if the bounding boxes of this and the argument overlap.
]]
function Powerup:collides(target)
if self.x > target.x + target.width or target.x > self.x + self.width then
return false
end
if self.y > target.y + target.height or target.y > self.y + self.height then
return false
end
return true
end
function Powerup.trigger(paddle)
if self.inPlay then
self.inPlay = false
end
end
function Powerup:update(dt)
self.x = self.x
self.y = self.y + self.dy * dt
if self.y <= 0 then
self.y = 0
self.dy = -self.dy
gSounds['wall-hit']:play()
end
end
function Powerup:render()
if self.inPlay then
love.graphics.draw('zucc.png',self.x,self.y)
end
end```
解决方法
table.method(self)
等于table:method()
,两者都被赋予self
作为第一个参数,这意味着如果您使用冒号:
,则不需要声明self
作为参数。
如果那不能解决您的问题,您能否更详细地告诉我们哪一行正在发生错误?
编辑:
引起错误的原因是因为您没有将变量x
和y
声明为参数。错误的意思是“您正在尝试插入x
值self
的索引x
,但是变量X不存在,也就是说,您试图插入{{ 1}}放入变量”
您可以通过在函数中声明参数来解决此问题:
nil
然后调用该函数,并给出值Powerup:init(x,y)
和x
,例如:
y
现在Powerup:init(3,5)
是3,而self.x
是5
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。