错误:尝试索引全球' square' (零值)

时间:2018-05-31 23:27:40

标签: lua null corona

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

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(事件)             player.x = player.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

1 个答案:

答案 0 :(得分:0)

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

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

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

了解更多: