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
4

2 回答 2

3

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

在进入local之前删除,一切都应该工作。换句话说,改变balloonscene:create()

    local balloon = display.newImageRect(...

    balloon = display.newImageRect(...
于 2017-03-18T21:58:08.370 回答
1

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

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

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

于 2017-03-19T03:19:24.463 回答