2017-03-18 59 views
1

我今天刚刚开始学习Lua。我一直在做coronalabs.com网站上的教程......我尝试着将弹跳气球轻击到小行星游戏的场景模板中,以适应第一个练习。有人能告诉我我是如何“尝试索引价值”吗?尝试索引upvalue

local composer = require("composer") 

local scene = composer.newScene() 


-- ----------------------------------------------------------------------------------- 
-- Code outside of the scene event functions below will only be executed ONCE unless 
-- the scene is removed entirely (not recycled) via "composer.removeScene()" 
-- ----------------------------------------------------------------------------------- 

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

local tapCount = 0 
local platform 
local balloon 
local tapText 

local function pushBalloon() 
    balloon:applyLinearImpulse(0, -0.75, balloon.x, balloon.y) 
    tapCount = tapCount + 1 
    tapText.text = tapCount 

end 



-- ----------------------------------------------------------------------------------- 
-- Scene event functions 
-- ----------------------------------------------------------------------------------- 

-- create() 
function scene:create(event) 

    local sceneGroup = self.view 
    -- Code here runs when the scene is first created but has not yet appeared on screen 
physics.pause() 
local background = display.newImageRect("background.png", 360, 570) 
background.x = display.contentCenterX 
background.y = display.contentCenterY 
local platform = display.newImageRect("platform.png", 300, 50) 
platform.x = display.contentCenterX 
platform.y = display.contentHeight-25 
local balloon = display.newImageRect("balloon.png", 112, 112) 
balloon.x = display.contentCenterX 
balloon.y = display.contentCenterY 
balloon.alpha = 0.8 

local tapText = display.newText(tapCount, display.contentCenterX, 20, native.systemFont, 40) 
tapText:setFillColor(0, 0, 0) 


physics.addBody(platform, "static") 
physics.addBody(balloon, "dynamic", { radius=50, bounce=0.6 }) 

balloon:addEventListener("tap", pushBalloon) 

end 


-- show() 
function scene:show(event) 

    local sceneGroup = self.view 
    local phase = event.phase 

    if (phase == "will") then 
     -- Code here runs when the scene is still off screen (but is about to come on screen) 

    elseif (phase == "did") then 
     -- Code here runs when the scene is entirely on screen 
physics.start() 
    end 
end 


-- hide() 
function scene:hide(event) 

    local sceneGroup = self.view 
    local phase = event.phase 

    if (phase == "will") then 
     -- Code here runs when the scene is on screen (but is about to go off screen) 

    elseif (phase == "did") then 
     -- Code here runs immediately after the scene goes entirely off screen 
physics.pause() 
    end 
end 


-- destroy() 
function scene:destroy(event) 

    local sceneGroup = self.view 
    -- Code here runs prior to the removal of scene's view 

end 


-- ----------------------------------------------------------------------------------- 
-- Scene event function listeners 
-- ----------------------------------------------------------------------------------- 
scene:addEventListener("create", scene) 
scene:addEventListener("show", scene) 
scene:addEventListener("hide", scene) 
scene:addEventListener("destroy", scene) 
-- ----------------------------------------------------------------------------------- 

return scene 
+0

在哪一行#错误提出? –

+0

上面的代码在我的模拟器中不会产生任何错误。 – ldurniat

回答

2

上述问题是该函数外部的局部变量。当您在scene:create()中初始化balloon时,您声明它为local,这将的范围限制为balloon的功能。在scene:create()之外,在文件顶部附近声明的balloon仍然是nil

删除local之前balloonscene:create()和一切应该工作。换句话说,更改

local balloon = display.newImageRect(... 

balloon = display.newImageRect(... 
+0

非常感谢! – Atrag

1

:是索引运营商之一。 .[]是其他。索引操作会评估左表达式的值,并期望表值。如果是1,它将在该表中查找等于[]内部表达式的值的键或等于:.右边的标识符作为字符串值。如果没有表格,则会抛出错误。

upvalue是对在外部函数范围内声明为local的非全局变量的引用。你有很多这样的东西,这很好,特别是对于类似全局的变量,单身属性和有效的服务/库。例如,composerscene

桌面检查表明错误发生在balloon:applyLinearImpulseGoojajiGreg's answer解释了如何更正您的代码,以便balloon在执行时引用表中的某个表,如预期的那样。