Вопрос

When I use screenGroup:insert(img) I get "attempt to index global 'screenGroup' (a nil value)".
How do I add the img objects created in the onTouch function to the screenGroup to ensure they are removed when I change scenes?

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
Это было полезно?

Решение

You declared sceneGroup as local in createScene so it does not exist outside of that function. Move the declaration to the top of the file:

local screenGroup

Then in createScene just do

screenGroup = self.view

You can specify the group in which to put image as first parameter to newImage although the way you insert it is fine too.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top