ERROR: "attempt to index global 'self' (a nil value)

ERROR: "attempt to index global 'self' (a nil value)

我在 cs50 的游戏轨道上非常缓慢地工作,我被困在 Mario3 上。在视频中他只是谈论代码,但我想把它写成我自己的代码,我正在和视频一起写。我的代码和他的完全一样,但我一直收到这个错误,我不知道为什么。我将 post 下面的代码,但据我所知,我已经分配了 self.mapWidth = 30,所以我不知道为什么它一直说它是一个 nil 值。

这是错误 Error code

这是map.lua代码


require 'Util'
Map = Class{}

TILE_BRICK = 1
TILE_EMPTY = 4

--cloud tiles
CLOUD_LEFT = 6
CLOUD_RIGHT = 7

SCROLL_SPEED = 62

function Map:init()
    self.spritesheet = love.graphics.newImage('graphics/spritesheet.png')
    self.tileWidth = 16
    self.tileHeight = 16
    self.mapWidth = 30
    self.mapHeight = 28
    self.tiles = {}

    self.camX = 0
    self.camY = 0

    self.mapWidthPixels = self.mapWidth * self.tileWidth
    self.mapHeightPixels = self.mapHeight * self.tileHeight

    self.tileSprites = generateQuads(self.spritesheet, self.tileWidth, self.tileHeight)

    --fill map with empty tiles--
    for y = 1, self.mapHeight do
        for x = 1, self.mapWidth do
            self:setTile(x, y, TILE_EMPTY)
        end
    end

    for y =self.mapHeight / 2, self.mapHeight do
        for x = 1, self.mapWidth do
            self:setTile(x, y, TILE_BRICK)
        end
    end

end

function Map:setTile(x, y, tile)
    self.tiles[(y - 1) * self.mapWidth + x] = tile
end

function Map:getTile(x, y)
    return self.tiles[(y -1) * self.mapWidth + x]
end

local x = 1
while x < self.mapWidth do
    if math.random(20) == 1 then
        local cloudStart = math.random(self.mapHeight / 2 -6)

        self.setTile(x, cloudStart, CLOUD_LEFT)
        self:setTile(x + 1, cloudStart, CLOUD_RIGHT)
    end
end


function Map:update(dt)
    if love.keyboard.isDown('w') then
        self.camY = math.max(0, math.floor(self.camY - SCROLL_SPEED * dt))
    elseif love.keyboard.isDown('s') then
        self.camY = math.min(self.mapHeightPixels - VIRTUAL_HEIGHT, math.floor(self.camY + SCROLL_SPEED * dt))
    elseif love.keyboard.isDown('a') then
        self.camX = math.max(0, math.floor(self.camX - SCROLL_SPEED *dt))
    elseif love.keyboard.isDown('d') then
        self.camX = math.min(self.mapWidthPixels - VIRTUAL_WIDTH, math.floor(self.camX + SCROLL_SPEED * dt))
    end
end

function Map:render()
    for y = 1, self.mapHeight do
        for x = 1, self.mapWidth do
            love.graphics.draw(self.spritesheet, self.tileSprites[self:getTile(x, y)],
                (x - 1) * self.tileWidth, (y - 1) * self.tileHeight)
        end
    end
end

如果您需要,这是我的 main.lua 代码

WINDOW_WIDTH = 1200
WINDOW_HEIGHT = 720

VIRTUAL_WIDTH = 432
VIRTUAL_HEIGHT = 243

Class = require 'class'
push = require 'push'

require 'Util'

require 'Map'

function love.load()
   
   map = Map()
    
   love.graphics.setDefaultFilter('nearest', 'nearest')

    push:setupScreen(VIRTUAL_WIDTH,VIRTUAL_HEIGHT,WINDOW_WIDTH,WINDOW_HEIGHT, {
        fullscreen = false,
        resizable = false,
        vsync = true
    })

end

function love.update(dt)
    map:update(dt)

    if love.keyboard.isDown('escape') then
        love.event.quit()
    end

end

function love.draw()
    push:apply('start')

    love.graphics.translate(math.floor(-map.camX),math.floor(-map.camY))

    love.graphics.clear(108/255, 140/255, 1, 1)
    map:render()

    push:apply('end')
end

在此先感谢您提供的所有帮助。

function Map:SomeFunction() endMap["SomeFunction"] = function(self) end

的语法糖

因此您可以在用冒号语法定义的函数中使用 self

这样的函数self之外就是nil.

然而你在 self 为 nil 的 while 循环中多次索引 self

local x = 1
while x < self.mapWidth do
    if math.random(20) == 1 then
        local cloudStart = math.random(self.mapHeight / 2 -6)

        self.setTile(x, cloudStart, CLOUD_LEFT)
        self:setTile(x + 1, cloudStart, CLOUD_RIGHT)
    end
end

顺便说一句,这是一个无限循环。