1
votes

I am creating a game using storyboard most of it is working smoothly as far as moving between scenes but when in my game.lua the words created with the next word function will not go away. They will continue to display and function when the home touch takes me back to the start.lua.

Here is the code for the game.lua. From my research so far I've already gathered that I need to somehow put that newQuestion function into a display object so it can be inserted into the 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

What am I missing?

1
My problem seems similar to the problem here: link - user3029068

1 Answers

1
votes

The problem was much easier to fix than I thought.

All I had to do was add the following after creating the buttons in the function.

screenGroup:insert(button)

Here it is within the code:

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