尝试索引上值

Attempt to index upvalue

我今天才开始学习Lua。我一直在 coronalabs.com 网站上做教程...我试图通过将弹跳气球敲击到小行星游戏的场景模板中来调整第一个练习。谁能告诉我我过得怎么样 "attempting to index an 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

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

删除 scene:create()balloon 之前的 local,一切都应该正常。也就是说,改变

    local balloon = display.newImageRect(...

    balloon = display.newImageRect(...

: 是索引运算符之一。 .[] 是其他的。索引操作评估左侧表达式的值,期望 table 值。如果是一个,它会在 table 中寻找等于 [] 内部表达式的值或等于 : 或 [=11= 右侧的标识符的键] 作为字符串值。如果没有table,则抛出错误。

upvalue 是对在外部函数作用域中声明的非全局变量的引用 local。你有很多这样的东西,这很好,特别是对于类似全局的、本质上是单例的变量,并且实际上是 services/libraries。例如,composerscene.

桌面检查表明错误发生在 balloon:applyLinearImpulse 解释了如何更正您的代码,以便 balloon 在执行的那个点引用 table,正如预期的那样。