【问题标题】:ERROR: attempt to index global 'square' (a nil value)错误:尝试索引全局“正方形”(零值)
【发布时间】:2018-11-11 01:10:49
【问题描述】:

我一直在谷歌上搜索这个错误的废话(尝试索引全局“正方形”(一个零值)),但我似乎无法理解问题是什么......我知道它应该相当容易修复,但我无法为我的生活弄清楚。错误发生在最底部,我标记了问题。

enter code here
local backdrop = display.setDefault("background", 1, 1, 1)

local physics = require("physics")
physics.start()

_W = display.contentWidth; -- Get the width of the screen
_H = display.contentHeight; -- Get the height of the screen

motionDown = 0;
motionUp = 0;
motionRight = 0;
motionLeft = 0;
motionSquare = 5

speed = 4

local left = display.newRect(0,0,_W/2,_H/1.19)
    left.x = _W/4.5; left.y = _H/2;

local right = display.newRect(0,0,_W/2,_H/1.19)
    right.x = _W/1.25; right.y = _H/2;

local top = display.newRect(0,0,_W,_H/5.5)
    top.x = _W/2; top.y =0;

local bottem = display.newRect(0,0,_W,_H/5.5)
    bottem.x = _W/2; bottem.y =_H;

    player = display.newImage("player.png", display.contentCenterX, display.contentCenterY)
        player.x = math.random(10,_W-10)
        player.y = math.random(10,_H-10)

player:toFront(player)

physics.addBody( player, "static" )


function left:tap()
        motionDown = 0;
        motionUp = 0;
        motionRight = 0;
    motionLeft = -speed;

    local square = display.newRect( 0, 0, 5, 5 )
    square.strokeWidth = 3
    square:setFillColor( 0 )
    square:setStrokeColor( 0, 0, 0 )
    square.x = player.x + 10 ; square.y = player.y;

    local function moveSquare (event)
        square.x = square.x + motionSquare;
        end
        Runtime:addEventListener("enterFrame", moveSquare)



end
    left:addEventListener("tap",left)

function right:tap()
        motionDown = 0;
        motionUp = 0;
        motionLeft = 0;
    motionRight = speed;

        local square = display.newRect( 0, 0, 5, 5 )
        square.strokeWidth = 3
        square:setFillColor( 0 )
        square:setStrokeColor( 0, 0, 0 )
        square.x = player.x - 10 ; square.y = player.y;

        local function moveSquare (event)
            square.x = square.x - motionSquare;
            end
            Runtime:addEventListener("enterFrame", moveSquare)

end
    right:addEventListener("tap",right)

function top:tap()
        motionDown = 0;
        motionRight = 0;
        motionLeft = 0;
        motionUp = -speed;
        local left = display.newRect(0,0,5,5)

        local square = display.newRect( 0, 0, 5, 5 )
        square.strokeWidth = 3
        square:setFillColor( 0 )
        square:setStrokeColor( 0, 0, 0 )
        square.x = player.x ; square.y = player.y + 10;

        local function moveSquare (event)
            square.y = square.y + motionSquare;
            end
            Runtime:addEventListener("enterFrame", moveSquare)

end
    top:addEventListener("tap",top) 

function bottem:tap(event)
        motionRight = 0;
        motionUp = 0;
        motionLeft = 0;
    motionDown = speed;

        local square = display.newRect( 0, 0, 5, 5 )
        square.strokeWidth = 3
        square:setFillColor( 0 )
        square:setStrokeColor( 0, 0, 0 )
        square.x = player.x ; square.y = player.y - 10;

        local function moveSquare (event)
        square.y = square.y - motionSquare;
        end
        Runtime:addEventListener("enterFrame", moveSquare)


end

    bottem:addEventListener("tap",bottem)

--我们的玩家还没有完全动起来。我们已经设置了监听器来左右移动角色,但现在我们实际上必须让他移动。我们使用运行时事件侦听器来做到这一点,它将沿 x 轴移动家伙。

-- 移动角色 本地函数 movePlayer(事件) 播放器.x = 播放器.x + motionRight; 如果 player.x > display.contentWidth 那么 player.x = 0 结尾 结尾 运行时:addEventListener("enterFrame", movePlayer)

local function movePlayer (event)
    player.x = player.x + motionLeft;
        if player.x < 0 then player.x = display.contentWidth
        end
    end
    Runtime:addEventListener("enterFrame", movePlayer)

local function movePlayer (event)
    player.y = player.y + motionUp;
        if player.y > display.contentHeight then player.y = 0
        end
    end
    Runtime:addEventListener("enterFrame", movePlayer)

local function movePlayer (event)
    player.y = player.y + motionDown;
        if player.y < 0 then player.y = display.contentHeight
        end
    end
    Runtime:addEventListener("enterFrame", movePlayer)
    if square.y > display.contentHeight then square.y = 0 ### This is the 
                                                                error
       end

【问题讨论】:

    标签: lua null coronasdk


    【解决方案1】:

    这是范围相关的问题。与全局变量不同,局部变量的作用域仅限于声明它们的块。所以你的 square 变量只能在这四个函数中访问:bottemtopleftright

    简单的解决方案需要使用前向声明:

    -- Top of file
    local square 
    -- Later in the code you use only name of variable without local key word
    

    阅读更多:

    【讨论】:

    • 我添加了它,它解决了这个问题,但是底部的代码(如果 square.y > display.contentHeight then square.y = 0 end)确实适用于任何其他正方形 Im试图让他们在屏幕上循环播放
    • 你应该使用表格来存储新的正方形。
    • 我在桌子上阅读并观看了一堆 youtube,但这似乎并没有解决我的问题,它只是产生了一个混乱的局部变量,可以以有趣的方式添加......跨度>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-03
    • 2015-04-09
    • 2014-01-26
    • 2013-06-07
    • 2020-03-08
    • 2013-09-04
    • 1970-01-01
    相关资源
    最近更新 更多