【问题标题】:ERROR: "attempt to index global 'self' (a nil value)错误:“尝试索引全局“自我”(零值)
【发布时间】:2020-11-03 22:23:59
【问题描述】:

我在 cs50 的游戏轨道上非常缓慢地工作,我被卡在了 Mario3 上。在视频中,他只讨论了代码,但我想把它写成我自己的,我在视频旁边写。我有与他完全相同的代码,但我不断收到这个错误,我不知道为什么。下面贴出代码,但据我所知,我已经赋值了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

提前感谢您的任何帮助。

【问题讨论】:

  • while 循环中,您在任何函数之外引用self。我假设这是第 54 行。

标签: lua love2d


【解决方案1】:

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

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

【讨论】:

  • 非常感谢,我明白了
猜你喜欢
  • 2015-02-18
  • 2020-04-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-11
  • 2015-04-09
  • 2014-01-26
相关资源
最近更新 更多