我一直在谷歌上搜索这个错误的废话(尝试索引全局"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 结束 结束 Runtime: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
这是与范围相关的问题。与全局变量不同,局部变量的作用域仅限于声明它们的块。所以你的square
变量只能在这四个函数中访问:bottem
、top
、left
和right
。
简单的解决方案需要使用前向声明:
-- Top of file
local square
-- Later in the code you use only name of variable without local key word
阅读更多:
- 初学者的范围,
- 在 Corona SDK 中使用前向声明的更快 Lua 代码(youtube 视频(,
- 前瞻声明。