Love2d “bad argument #2 to 'draw' (Quad expected, got nil)”

我目前正在尝试制作一个类似flappy bird的游戏,并且在尝试生成管道时遇到了问题(当应该生成管道时,我会得到以下错误:“bad argument #2 to 'draw' (Quad expected, got nil)”)。

导致问题的函数如下(它们位于三个不同的类中):

Pipe = Class{}

local PIPE_IMAGE = love.graphics.newImage('FlappyBirdPipe.png')

PIPE_SCROLL = -60

PIPE_WIDTH = 20
PIPE_HEIGHT = 160

function Pipe:init(orientation, y)
    self.x = VIRTUAL_WIDTH
    self.y = y

    self.width = PIPE_IMAGE:getWidth()
    self.height = PIPE_HEIGHT

    self.orientation = orientation
end

function Pipe:render()
    love.graphics.draw(PIPE_IMAGE, self.x,
        (self.orientation == 'top' and self.y + PIPE_HEIGHT or self.y),
        0, 1, (self.orientation == 'top' and -1 or 1))
end
function PipePair:update(dt)
    if self.x > -PIPE_WIDTH then
        self.x = self.x - PIPE_SCROLL * dt
        self.pipes['lower'].x = self.X
        self.pipes['upper'].x = self.x
    else
        self.remove = true
    end
end

如果有任何不清楚的地方,或者我忽略了一些关键信息,我非常愿意提供更多信息(我是Stack Overflow的新手,所以我不太清楚这里的一切是如何工作的)。

(我在VSCode中使用love2d版本11.3)

编辑:我将错误定位到了我从PipePair类的update函数中更新self.x时的问题。某种程度上,这种对self.x的更改似乎使其为nil。

点赞
用户15787577
用户15787577
我刚刚搞清楚了!

function PipePair:update(dt) if self.x > -PIPE_WIDTH then self.x = self.x - PIPE_SCROLL * dt self.pipes['lower'].x = self.X self.pipes['upper'].x = self.x else self.remove = true end end

```

self.pipes['lower'].x = self.X 上有一个大写字母 X。我一直试图弄清楚它,结果是一个 X 弄得我这样。哇,我感觉好蠢。

2021-04-28 21:10:46
用户17783369
用户17783369

错误提示表明一个变量是 nil,这可能是由于拼写错误引起的。问题是由 self.X 中的大写 X 导致的。

尝试:

function PipePair:update(dt)
    if self.x > -PIPE_WIDTH then
        self.x = self.x - PIPE_SCROLL * dt
        self.pipes['lower'].x = self.x -- 这里有个拼写错误
        self.pipes['upper'].x = self.x
    else
        self.remove = true
    end
end
2022-05-01 10:53:55