【问题标题】:Attempt to index upvalue尝试索引 upvalue
【发布时间】:2017-08-10 07:46:39
【问题描述】:

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

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

【问题讨论】:

  • 在哪一行出现错误?
  • 上面的代码不会在我的模拟器中产生任何错误。

标签: lua coronasdk upvalue


【解决方案1】:

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

scene:create() 中删除balloon 之前的local,一切都会正常。换句话说,改变

    local balloon = display.newImageRect(...

    balloon = display.newImageRect(...

【讨论】:

  • 非常感谢!
【解决方案2】:

: 是索引运算符之一。 .[] 是其他人。索引操作计算左表达式的值,需要一个表值。如果是 1,它会在该表中查找与 [] 内的表达式的值相等或等于 :. 右侧的标识符的键作为字符串值。如果没有表,则抛出错误。

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-16
    • 2019-10-10
    • 1970-01-01
    • 2019-10-16
    • 2017-04-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多