Corona Storyboard:删除在外部函数中创建的场景对象

时间:2013-12-07 15:19:12

标签: lua corona corona-storyboard

我正在使用故事板创建一个游戏,大部分游戏都在场景之间移动,但在我的 game.lua 中,使用下一个单词功能创建的单词不会消失。当家庭触摸将我带回 start.lua 时,它们将继续显示和运行。

以下是 game.lua 的代码。从我到目前为止的研究中我已经收集到了我需要以某种方式将newQuestion函数放入显示对象中,以便可以将其插入到screenGroup中。

--lua for game

--creates the storyboard variable and calls the storyboard api
local storyboard = require ("storyboard")

local content = require "content"
local operations = require "operations"
local defaultWidth = 1024
local defaultHeight = 768
local displayWidth = display.viewableContentWidth
local displayHeight = display.viewableContentHeight
local centerX = defaultWidth/2;
local centerY = defaultHeight/2;
local maxSightwords = 3
local currQuestion = 0
local playOrder
local randWord1
local randWord2
local wordButtons
local wrongGraphic
local correctButton
local nextQuestion
local homeButton

--tells storyboard to create a new scene
local scene = storyboard.newScene()

    -- assign random order for words
    playOrder = operations.getRandomOrder(#content)
    randWord1 = operations.getRandomOrder(#content)
    randWord2 = operations.getRandomOrder(#content)

    local function onHomeTouch(event)
        if event.phase == "began" then
        storyboard.gotoScene("start")
        end
    end



function scene:createScene(event)
    local gameScreen = self.view

        --creates a transparent background image centered on the display
    local gameBackground = display.newImage("images/graphics/jungle1.jpg")
        gameBackground.x = display.contentWidth/2
        gameBackground.y = display.contentHeight/2
        gameScreen:insert(gameBackground)

    homeButton = display.newImage("images/buttons/home.png")
        homeButton.alpha = .8
        homeButton.y = 70
        gameScreen:insert(homeButton)
end



function scene:enterScene(event)
    local screenGroup = self.view

    homeButton:addEventListener("touch", onHomeTouch)   

    nextQuestion ()

end 


function scene:exitScene(event)
    homeButton:removeEventListener("touch", onHomeTouch)

end



function scene:destroyScene(event)

end


--***** game functions *****--

function nextQuestion()

    -- update question number index
    currQuestion = currQuestion+1
    if currQuestion > #playOrder then
        currQuestion = 1
    end

    local questionNumber = playOrder[currQuestion]
    print("Question# "..currQuestion)
    print("id "..content[questionNumber].id)

    -- make word buttons
    wordButtons = {}

    -- make word button for correct word
    --picks a word from the content table
    local word = content[playOrder[currQuestion]].word
    --inserts the word in the form of an image into the wordButtons table
    table.insert(wordButtons, operations.newWordButton(word))
    --identifies the first item in the wordButtons.graphics array as the correct answer
    correctButton = wordButtons[1].graphics
    local buttonWidth = 150
    playWord ()

    -- ****make random word button 1*** 
    local Rword1 = content[randWord1[currQuestion]].word
    print (Rword1)

    for i=1, 1 do
        table.insert(wordButtons, operations.newWord1Button(Rword1))
    end

    -- ****make random word button 2*** 
    local Rword2 = content[randWord2[currQuestion]].word
    print (Rword2)

    for i=1, 1 do
        table.insert(wordButtons, operations.newWord2Button(Rword2))
    end

    -- position letter buttons and add touch event listener
    local randomWordOrder = operations.getRandomOrder(#wordButtons)
    local buttonSpacing = buttonWidth * 1.5
    local buttonsWidth = (#wordButtons * buttonWidth) + ((#wordButtons-1) * (buttonSpacing/4))
    local buttonsX = centerX - (buttonWidth)
    for i=1, #wordButtons do
        local button = wordButtons[i].graphics
        button.xScale = 1.5
        button.yScale = 1.5
        button.y = centerY
        button.x = buttonsX + (buttonSpacing * (randomWordOrder[i]-1))
        button:addEventListener("touch", onWordTouch)
        --local randomDelay = transitionDuration + (math.random(1,10) * 10)
        --transition.from(button, {time = 500, delay = randomDelay, y = defaultHeight + button.height})
    end
    return wordButtons
end


function clearQuestion()
    -- remove wrongGraphic if present
    if wrongGraphic then
        wrongGraphic:removeSelf()
        wrongGraphic = nil
    end

    -- remove all word buttons
    for i=1,#wordButtons do
        wordButtons[i].graphics:removeSelf()
        wordButtons[i].graphics = nil
    end
end


function onWordTouch(event)
    local t = event.target
    if "ended" == event.phase then
        if t == correctButton then
            onCorrect()
        else
            onIncorrect(t)
        end

    end
end

function onIncorrect(incorrectButton)
    media.playSound("sounds/splat.wav")
    wrongGraphic = display.newImageRect("images/graphics/wrong.png", 137, 136)
    wrongGraphic.x = incorrectButton.x 
    wrongGraphic.y = incorrectButton.y + incorrectButton.height/2
    transition.to(incorrectButton, {time=100, delay=500, alpha=0})
    transition.to(wrongGraphic, {time=200, delay=500, alpha=0, onComplete=wrongCompleteListener})
    local wrongCompleteListener = function(obj)
       obj:removeSelf()
       obj = nil
       incorrectButton:removeSelf()
       incorrectButton = nil
    end
end

function onCorrect()
    -- play correct sound then display word
    media.playSound("sounds/correct.mp3", playWord)

    -- remove the letter buttons
    clearQuestion()

    nextQuestion ()
    -- disable the home button until new screen is shown
    homeEnabled = false
end

function playWord()
    local audioFile = "sounds/words/"..content[playOrder[currQuestion]].id..".wav"
    media.playSound(audioFile, showWord)
end

--the actual event listeners that make the functions work
scene:addEventListener("createScene", scene)
scene:addEventListener("enterScene", scene)
scene:addEventListener("exitScene", scene)
scene:addEventListener("destroyScene", scene)


return scene

我错过了什么?

1 个答案:

答案 0 :(得分:1)

问题比我想象的要容易得多。

我需要做的就是在功能中创建按钮后添加以下内容。

screenGroup:insert(button)

这是在代码中:

local randomWordOrder = operations.getRandomOrder(#wordButtons)
        local buttonSpacing = buttonWidth * 2
        local buttonsWidth = (#wordButtons * buttonWidth) + ((#wordButtons-1) * (buttonSpacing/4))
        local buttonsX = centerX - (buttonWidth)
            for i=1, #wordButtons do
                local button = wordButtons[i].graphics
                button.xScale = 1.5
                button.yScale = 1.5
                button.y = centerY
                button.x = buttonsX + (buttonSpacing * (randomWordOrder[i]-1))
                button:addEventListener("touch", onWordTouch)
                screenGroup:insert(button)
            end