Lua I';“我得到了错误”;尝试索引本地self(数值)
这是我调用update函数的部分,有人说这可能是错误Lua I';“我得到了错误”;尝试索引本地self(数值),lua,Lua,这是我调用update函数的部分,有人说这可能是错误 local function include_helper(to, from, seen) if from == nil then return to elseif type(from) ~= 'table' then return from elseif seen[from] then return seen[from] end seen[from]
local function include_helper(to, from, seen)
if from == nil then
return to
elseif type(from) ~= 'table' then
return from
elseif seen[from] then
return seen[from]
end
seen[from] = to
for k,v in pairs(from) do
k = include_helper({}, k, seen) -- keys might also be tables
if to[k] == nil then
to[k] = include_helper({}, v, seen)
end
end
return to
end
-- deeply copies `other' into `class'. keys in `other' that are already
-- defined in `class' are omitted
local function include(class, other)
return include_helper(class, other, {})
end
-- returns a deep copy of `other'
local function clone(other)
return setmetatable(include({}, other), getmetatable(other))
end
local function new(class)
-- mixins
class = class or {} -- class can be nil
local inc = class.__includes or {}
if getmetatable(inc) then inc = {inc} end
for _, other in ipairs(inc) do
if type(other) == "string" then
other = _G[other]
end
include(class, other)
end
-- class implementation
class.__index = class
class.init = class.init or class[1] or function() end
class.include = class.include or include
class.clone = class.clone or clone
-- constructor call
return setmetatable(class, {__call = function(c, ...)
local o = setmetatable({}, c)
o:init(...)
return o
end})
end
-- interface for cross class-system compatibility (see https://github.com/bartbes/Class-Commons).
if class_commons ~= false and not common then
common = {}
function common.class(name, prototype, parent)
return new{__includes = {prototype, parent}}
end
function common.instance(class, ...)
return class(...)
end
end
-- the module
return setmetatable({new = new, include = include, clone = clone},
{__call = function(_,...) return new(...) end})
这个错误很清楚你做错了什么 您正在索引本地self,一个数值 这意味着在某个地方,您正在执行类似于
self.dy
的操作,其中self
不是一个表,而是一个数字,不允许对数字使用索引运算符
,因为这没有任何意义
问题是为什么self不是一张桌子
function love.update(dt)
if love.keyboard.isDown('w') then
player1.dy=-PADDLE_SPEED
elseif love.keyboard.isDown('s') then
player1.dy=PADDLE_SPEED
else
player1.dy=0
end
if love.keyboard.isDown('up') then
player2.dy=-PADDLE_SPEED
elseif love.keyboard.isDown('down') then
player2.dy=PADDLE_SPEED
else
player2.dy=0
end
if gameState=='play' then
ball.update(dt)
end
player1.update(dt)
player2.update(dt)
是(语法上的糖)的缩写
以及函数调用
myTable:myFunction()
是myTable.myFunction(myTable)
请参阅Lua手册
在代码中查找一个用:
定义并用
调用的函数,并在调用过程中获取一个数字作为第一个参数
这样一来,一个数字就会出现在你期望的self
位置
我猜错误在main.lua中,您没有提供
这里有几个调用patle:update(dt)
。例如,编写mypable.update(dt)
会导致该错误。但我不能确定,因为您没有提供代码
但这对老师有效,但对你无效,通常是因为你做了一些不同的/错误的事情
编辑:
由于您提供了更多的代码,我可以确定观察到的错误是由
function myTable.myFunction(self) end
这将把dt
作为一个数字值,函数期望self
,即桌面球
换成
ball.update(ball,dt)
或ball:update(dt)
player1.update(player1,dt)
或player1:update(dt)
player2.update(player2,dt)
或player2:update(dt)
这个错误很清楚地说明了你做错了什么
您正在索引本地self,一个数值
这意味着在某个地方,您正在执行类似于self.dy
的操作,其中self
不是一个表,而是一个数字,不允许对数字使用索引运算符
,因为这没有任何意义
问题是为什么self不是一张桌子
function love.update(dt)
if love.keyboard.isDown('w') then
player1.dy=-PADDLE_SPEED
elseif love.keyboard.isDown('s') then
player1.dy=PADDLE_SPEED
else
player1.dy=0
end
if love.keyboard.isDown('up') then
player2.dy=-PADDLE_SPEED
elseif love.keyboard.isDown('down') then
player2.dy=PADDLE_SPEED
else
player2.dy=0
end
if gameState=='play' then
ball.update(dt)
end
player1.update(dt)
player2.update(dt)
是(语法上的糖)的缩写
以及函数调用
myTable:myFunction()
是myTable.myFunction(myTable)
请参阅Lua手册
在代码中查找一个用:
定义并用
调用的函数,并在调用过程中获取一个数字作为第一个参数
这样一来,一个数字就会出现在你期望的self
位置
我猜错误在main.lua中,您没有提供
这里有几个调用patle:update(dt)
。例如,编写mypable.update(dt)
会导致该错误。但我不能确定,因为您没有提供代码
但这对老师有效,但对你无效,通常是因为你做了一些不同的/错误的事情
编辑:
由于您提供了更多的代码,我可以确定观察到的错误是由
function myTable.myFunction(self) end
这将把dt
作为一个数字值,函数期望self
,即桌面球
换成
ball.update(ball,dt)
或ball:update(dt)
player1.update(player1,dt)
或player1:update(dt)
player2.update(player2,dt)
或player2:update(dt)
您的代码粘贴错误:math.max(`enter code here`0
另外,您可以发布堆栈跟踪,并指出哪些行与其中的行号相对应吗?将所有出现的.update
更改为:update
并查看问题是否消失。第二行输入错误。将类{}
替换为类{}
您粘贴的代码错误:math.max(`enter code here`0
)另外,您可以发布堆栈跟踪,并指出哪些行与其中的行号相对应吗?将所有出现的.update
更改为:update
并查看问题是否消失。第二行有输入错误。请替换类{}
对于类{}
我不明白,我如何使self.dy等于0,它确实是,但有意义吗?这是我的老师使用的完整代码的链接,如果我试图完美地使用我的老师使用的Class函数,我会错误地尝试调用全局“Class”(一个nil值)@RafaelCarvalho调用全局“class”,而不是“class”,因为您编写的是“class”,而不是“class”。当您尝试使用C时,代码使用大写字母C,这当然不起作用,因为Lua名称是区分大小写的。请注意细节!!!在我发布的代码中,是的,但当我切换到使用pable=class{}它说的是nil值,但如果我使用普通类,它就不起作用了,因为索引号的问题,我认为应该由类模块来解决,它不能正常工作。哇,不知怎的,我把它改成了player1:update(dt)我不明白,我怎样才能使self.dy等于0,但它是有意义的?这是我老师使用的完整代码的链接,如果我试图完美地使用我老师使用的class函数,我会错误地尝试调用全局“class”(一个nil值)@RafaelCarvalho调用全局“class”,而不是“class”,因为您编写的是“class”,而不是“class”。当您尝试使用C时,代码使用大写字母C,这当然不起作用,因为Lua名称是区分大小写的。请注意细节!!!在我发布的代码中,是的,但当我切换到使用pable=class{}它说的是nil值,但是如果我使用普通类,它就不起作用了,因为索引号的问题,我认为应该由mod类来解决
ball.update(dt)
player1.update(dt)
player2.update(dt)