遇到错误“attempt to index local self (a number value)"的解决方法

require 'class'
Paddle=class{}
function Paddle:init(x,y,width,height)
    self.x=x
    self.y=y
    self.width=width
    self.height=height
    self.dy=0
end  function Paddle:update(dt)
    if self.dy < 0 then
        self.y = math.max(`enter code here`0, self.y + self.dy * dt)
    else
        self.y=math.min(VIRTUAL_HEIGHT,-self.height,self.y+self.dy*dt)
    end
end
function Paddle:render()
    love.graphics.rectangle('fill',self.x,self.y,self.width,self.height)
end

我正在跟随CS50 lecture 0 pong update 5课程,这个问题对于老师来说可以工作,但对于我来说出现了这个问题。我不知道为什么会发生这种情况,也不理解这个问题,因为它是没有意义的。如果您愿意,这里是一个'类'(class)的定义。这个问题在我创建的另一个叫做'ball'的类中没有发生,'ball'和这个类做的事情完全一样。我也定义了self.dy,并且它确实有一个值“0”,所以我不知道为什么会出现这个错误,以及这个错误的含义是什么。

 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})

所以这是我调用update函数的地方,有人说这可能是出现错误的地方

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)
点赞
用户2858170
用户2858170

这个错误很明确地说明了你做错了什么。

你正在对本地的 self 进行索引,一个数值。

这意味着你在某个地方做了类似于 self.dy 这样的事情,其中 self 不是一个表,而是一个数字,并且在数字上使用索引运算符 . 是不被允许的,因为这没有任何意义。

问题是为什么 self 不是一个表。

function myTable:myFunction() end

是以下语法糖的简写形式

function myTable.myFunction(self) end

函数调用

myTable:myFunction() 等同于 myTable.myFunction(myTable)

请参阅 Lua 手册。

函数调用

函数定义

在你的代码中找到一个使用 : 定义并以 . 调用并且在调用期间得到一个数值作为第一个参数的函数。

这样一个数字就会出现在你期望的 self 的位置。

我猜错误在你没有提供的 main.lua 中。

在那里,你有几个对 Paddle:update(dt) 的调用。例如,写成 myPaddle.update(dt) 就会导致这个错误。但我不能确定,因为你没有提供你的代码。

但通常是因为你做了不同或错误的事情,所以它适用于老师,但不适用于你。

编辑:

由于你提供了更多的代码,我可以确定观察到的错误是由以下代码引起的:

ball.update(dt)
player1.update(dt)
player2.update(dt)

这会把一个数字值的 dt 放到函数期望的 self,ball 表里面。

将其替换为

ball.update(ball, dt)ball:update(dt)

player1.update(player1, dt)player1:update(dt)

player2.update(player2, dt)player2:update(dt)

2020-05-18 06:17:08