无法在本地函数中将图像添加到屏幕组[LUA,CORONA SDK STORYBOARD]

时间:2014-02-20 00:07:22

标签: lua corona corona-storyboard

当我使用screenGroup:insert(img)时,我得到“尝试索引全局'screenGroup'(一个零值)”。 如何将onTouch函数中创建的img对象添加到screenGroup以确保在更改场景时将其删除?

local storyboard = require( "storyboard" )
local scene = storyboard.newScene()

local function goToTitle(event)
    if event.phase == "began" then
        storyboard.gotoScene( "titleScreen",  "fade", 400  )
    end
    return true
end

local onTouch = function(event)
    if event.phase == "began" then
        local img = display.newImage("shut_up_black.jpg")
        img:addEventListener( "touch", goToTitle ) 
        img.width = 100
        img.height = 100
        img.x = event.x
        img.y = event.y
        screenGroup:insert(img)
        return true
    end
end

function scene:createScene( event )
    local screenGroup = self.view
    local bg = display.newImage("bg1.jpg")
    screenGroup:insert( bg )
    Runtime:addEventListener("touch", onTouch)
end

function scene:enterScene( event )
end

function scene:exitScene( event )
end

function scene:destroyScene( event )
end

scene:addEventListener( "createScene", scene )
scene:addEventListener( "enterScene", scene )
scene:addEventListener( "exitScene", scene )
scene:addEventListener( "destroyScene", scene )

return scene

1 个答案:

答案 0 :(得分:2)

您在createScene中将sceneGroup声明为local,因此它不存在于该函数之外。将声明移到文件顶部:

local screenGroup

然后在createScene中执行

screenGroup = self.view

您可以指定将图像作为第一个参数放入newImage的组,尽管插入它的方式也很好。