尝试索引up值

时间:2017-03-18 20:55:13

标签: lua corona upvalue

我今天刚开始学习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

2 个答案:

答案 0 :(得分:2)

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

local balloon之前删除scene:create(),一切都行之有效。换句话说,改变

    local balloon = display.newImageRect(...

    balloon = display.newImageRect(...

答案 1 :(得分:1)

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

upvalue是对外部函数作用域中声明为local的非全局变量的引用。你有很多这样的东西,这很好,特别是对于类似全局的变量,单独使用的变量,并且是有效的服务/库。例如,composerscene

桌面检查表明错误在balloon:applyLinearImpulse处抛出。 GoojajiGreg's answer解释了如何更正代码,以便balloon在执行过程中引用表,如预期的那样。