试图索引本地'up'(无值)(LUA)

问题描述 投票:0回答:1

Main.lua:131尝试索引本地'up'(nil值)

嗨,希望您过得愉快。如果我犯的错误真的很简单,我表示歉意。我是lua的新手,现在仍在弄清楚。如果您有空余时间,请看一下代码。我得到的错误是:Main.lua:131尝试索引本地“ up”(一个nil值)我在第131行加上了我认为会引起星号错误的行,因此您不必计算每一行。我认为controls.displayGroup尚未正确初始化,但不确定如何修复。

local screenWidth = display.contentWidth
local screenHeight = display.contentHeight
local controllerWidth = screenWidth / 6
local rightMargin = 30
local maze = {
    { 0, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
    { 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1 },
    { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1 },
    { 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1 },
    { 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1 },
    { 0, 1, 1, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1 },
    { 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 0, 0 },
    { 0, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 1, 1 },
    { 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 1 },
    { 0, 1, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1 },
    { 0, 1, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1 },
    { 0, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1 },
}
maze.rows = table.getn(maze)
maze.columns = table.getn(maze[1])
maze.xStart, maze.yStart = 1, 1
maze.xFinish, maze.yFinish = 24, 7

local grid = {}
grid.xSquares = maze.columns
grid.ySquares = maze.rows
grid.totalWidth = screenWidth - controllerWidth - rightMargin
grid.squareSize = grid.totalWidth / grid.xSquares
grid.xStart = controllerWidth
grid.yStart = 60
grid.displayGroup = display.newGroup()
grid.displayGroup.x = grid.xStart
grid.displayGroup.y = grid.yStart

grid.functions = {
  left = function(gridSquare)
        if gridSquare.x == 0 then
            return gridSquare
        else
            return grid[gridSquare.y][gridSquare.x - 1]
        end
    end,
  right = function(gridSquare)
        if gridSquare.x + 1 == grid.xSquares then
            return gridSquare
        else
            return grid[gridSquare.y][gridSquare.x + 1]
        end
    end,
  above = function(gridSquare)
        if gridSquare.y == 0 then
            return gridSquare
        else
            return grid[gridSquare.y - 1][gridSquare.x]
        end
    end,
  below = function(gridSquare)
        if gridSquare.y + 1 == grid.ySquares then
            return gridSquare
        else
            return grid[gridSquare.y + 1][gridSquare.x]
        end
    end,
}

for y = 0, grid.ySquares - 1 do
  grid[y] = {y = y}
  for x = 0, grid.xSquares - 1 do
    grid[y][x] = {x = x, y = y}
    local rect = display.newRect(grid.displayGroup,
        grid.squareSize * x, grid.squareSize * y,
        grid.squareSize, grid.squareSize)
    if maze[y + 1][x + 1] == 0 then
            rect:setFillColor(245, 215, 98)
        else
            grid[y][x].wall = true
            rect:setFillColor(32, 96, 32, 255)
        end
    grid[y][x].displayObject = rect
    grid[y][x].left = grid.functions.left
        grid[y][x].right = grid.functions.right
        grid[y][x].above = grid.functions.above
        grid[y][x].below = grid.functions.below
  end
end
grid[maze.yStart - 1][maze.xStart - 1].start = true
grid[maze.yStart - 1][maze.xStart - 1].displayObject:setFillColor(192, 192, 255)
grid[maze.yFinish - 1][maze.xFinish - 1].displayObject:setFillColor(192, 128, 128)
grid[maze.yStart - 1][maze.xStart - 1].start = true
grid[maze.yFinish - 1][maze.xFinish - 1].finish = true

local runner = { image = "runner.png" }
function runner:enter(gridSquare)
  if self.displayObject == nil then
        self.displayObject = display.newImageRect(grid.displayGroup,
        self.image, grid.squareSize, grid.squareSize)
        self.displayObject:setFillColor(92, 92, 92)
    end
  self.displayObject.x = gridSquare.displayObject.x
    self.displayObject.y = gridSquare.displayObject.y
  self.gridSquare = gridSquare
  self.x = gridSquare.x
    self.y = gridSquare.y
  if self.gridSquare.finish then
        finish()
    end
end
function runner:canEnter(gridSquare)
    return gridSquare.wall == nil
end

local controlCenterX = controllerWidth / 2
local controlCenterY = screenHeight - screenHeight / 5
local controlCenterRadius = controllerWidth / 2 - rightMargin
local upDownWidth = 27
local upDownHeight = 60
local leftRightWidth = 60
local leftRightHeight = 27
local controls = {
    up    = {},
    down  = {},
    right = {},
    left  = {},
}

****
controls.displayGroup = display.newGroup()
****

local circlePad = display.newCircle(controls.displayGroup,
    controlCenterX, controlCenterY, controlCenterRadius)
  circlePad:setFillColor(128, 128, 128)

*****
local up = display.newImageRect(controls.displayGroup, "arrow_up.png",
    upDownWidth, upDownHeight)
up.x = controlCenterX
up.y = controlCenterY - upDownHeight / 2
controls.up.displayObject = up
*****

local down = display.newImageRect(controls.displayGroup, "arrow_down.png",
  upDownWidth, upDownHeight)
down.x = controlCenterX
down.y = controlCenterY + upDownHeight / 2
controls.down.displayObject = down
local right = display.newImageRect(controls.displayGroup, "arrow_right.png",
    leftRightWidth, leftRightHeight)
right.x = controlCenterX + leftRightWidth / 2
right.y = controlCenterY
controls.right.displayObject = right
local left = display.newImageRect(controls.displayGroup, "arrow_left.png",
    leftRightWidth, leftRightHeight)
left.x = controlCenterX - leftRightWidth / 2
left.y = controlCenterY
controls.left.displayObject = left
controls.hide = function(controls)
    controls.displayGroup.isVisible = false
end
controls.show = function(controls)
    controls.displayGroup.isVisible = true
end

local function pressLeft(event)
    if event.phase == "began" then
        local nextSquare = runner.gridSquare:left()
        if runner:canEnter(nextSquare) then
            runner:enter(nextSquare)
        end
    end
end
local function pressRight(event)
    if event.phase == "began" then
        local nextSquare  = runner.gridSquare:right()
        if runner:canEnter(nextSquare) then
            runner:enter(nextSquare)
        end
    end
end
local function pressUp(event)
    if event.phase == "began" then
        local nextSquare  = runner.gridSquare:above()
        if runner:canEnter(nextSquare) then
            runner:enter(nextSquare)
        end
    end
end
local function pressDown(event)
    if event.phase == "began" then
        local nextSquare  = runner.gridSquare:below()
        if runner:canEnter(nextSquare) then
            runner:enter(nextSquare)
        end
    end
end
controls.left.displayObject:addEventListener("touch", pressLeft)
controls.right.displayObject:addEventListener("touch", pressRight)
controls.up.displayObject:addEventListener("touch", pressUp)
controls.down.displayObject:addEventListener("touch", pressDown)
local startButton = {}
startButton.displayGroup = display.newGroup()
startButton.displayObject = display.newCircle(startButton.displayGroup,
    controlCenterX, controlCenterY, controlCenterRadius)
  startButton.displayObject.strokeWidth = 6
startButton.displayObject:setStrokeColor(244, 244, 64)
startButton.text = display.newText(startButton.displayGroup,
    "Start", controlCenterX - controlCenterRadius + 20, controlCenterY - 18,
    native.systemFont, 24)
  startButton.text:setTextColor(0, 0, 0)
  startButton.touch = function(event)
    if event.phase == "began" then
        startButton:hide()
        start()
    end
end
startButton.displayGroup:addEventListener("touch", startButton.touch)
startButton.show = function(button)
    button.displayGroup.isVisible = true
end
startButton.hide = function(button)
    button.displayGroup.isVisible = false
end
local playAgainButton = {}
playAgainButton.displayGroup = display.newGroup()

playAgainButton.displayObject = display.newCircle(playAgainButton.displayGroup,
    controlCenterX, controlCenterY, controlCenterRadius)
  playAgainButton.displayObject.strokeWidth = 6
playAgainButton.displayObject:setStrokeColor(244, 244, 64)
playAgainButton.text = display.newText(playAgainButton.displayGroup,
    "Again", controlCenterX - controlCenterRadius + 20, controlCenterY - 18,
    native.systemFont, 24)
  playAgainButton.text:setTextColor(0, 0, 0)
  playAgainButton.touch = function(event)
    if event.phase == "began" then
        playAgainButton:hide()
        play()
    end
end
playAgainButton.displayGroup:addEventListener("touch", playAgainButton.touch)
playAgainButton.show = startButton.show
playAgainButton.hide = startButton.hide

function play()
  runner:enter(grid[maze.yStart - 1][maze.xStart - 1])
  playAgainButton:hide()
  controls:hide()
  startButton:show()
end

function start()
    controls:show()
end

function finish()
    controls:hide()
    playAgainButton:show()
end
play()
lua runtime-error corona
1个回答
0
投票
[display.newImageRect将不返回任何内容,即变量

nil,如果图像文件不存在,您可能已经通过检查控制台注意到了这一点,以解决此问题,就像将缺少的资产添加到项目文件夹!

欢迎使用堆栈溢出〜
© www.soinside.com 2019 - 2024. All rights reserved.