Storyboard Error

I had a go at following a tutuorial on how to make an alphabet soup game for corona (link) - http://mobile.tutsplus.com/tutorials/corona/corona-sdk-create-an-alphabet-soup-game/

however, I wanted to have a go at doing my own stuff with the code. It all works well until I put it into the storyboard. I’ve done the menu but i can’t get the scene 1 to work. The background image of scene 1 comes up however the letters do not.

in the code I have commented line 78 because that line comes up with an error.

-- my game -- scene1.lua local storyboard = require ( "storyboard" ) local scene = storyboard.newScene() --create scene function scene:createScene(event) local screenGroup = self.view local gameBg = display.newImage('bg.png') end function scene:enterScene(event) local group = self.view -- local L1 is the word that the player must find. local L1 = {'DOG'} local L1Map = {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 'D', 'O', 'G'}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}} local alphabet = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'} local correct = 0 local clueText = display.newText( "DOG", 0, 450, Arial, 16) clueText:setTextColor(255, 255, 255) --local clueText displays the the word the player must find. function buildSoup() for i = 1, 10 do for j = 1, 12 do local tf = display.newText('', 0, 0, 'Arial', 19) tf:setTextColor(102, 102, 102) tf.width = 320 tf.height = 480 tf.x = math.floor(-10 + (31.3 \* i)) tf.y = math.floor(-10 + (35 \* j)) -- Change 0's to Random Letters if(L1Map[j][i] == 0) then L1Map[j][i] = alphabet[math.floor(math.random() \* table.maxn(alphabet))+1] end tf.text = L1Map[j][i] tfs:insert(tf) end end -- Add Words List local wordsString = '' for i = 1, #L1 do wordsString = wordsString .. ' ' .. L1[i] end wordsList = display.newText(wordsString, 10000, 10000, 'Arial', 14) wordsList:setTextColor(238, 238, 238) currentWords = display.newText('', 10000, 10000, 'Arial', 14) currentWords:setTextColor(238, 146, 63) end function startDraw:touch(e) -- I get an error on this line: attempt to index global "start draw" \<a nil value\> if(e.phase == 'began') then initX = e.x initY = e.y elseif(e.phase == 'ended') then line = display.newLine(initX, initY, e.x, e.y) line.width = 16 line:setColor(255, 142, 42, 60) lines:insert(line) end end function hitTestObjects(obj1, obj2) local left = obj1.contentBounds.xMin \<= obj2.contentBounds.xMin and obj1.contentBounds.xMax \>= obj2.contentBounds.xMin local right = obj1.contentBounds.xMin \>= obj2.contentBounds.xMin and obj1.contentBounds.xMin \<= obj2.contentBounds.xMax local up = obj1.contentBounds.yMin \<= obj2.contentBounds.yMin and obj1.contentBounds.yMax \>= obj2.contentBounds.yMin local down = obj1.contentBounds.yMin \>= obj2.contentBounds.yMin and obj1.contentBounds.yMin \<= obj2.contentBounds.yMax return (left or right) and (up or down) end function detectLetters:touch(e) if(e.phase == 'ended') then -- Get selected letters local selectedWord = '' for i = 1, tfs.numChildren do if(hitTestObjects(lines[lines.numChildren], tfs[i])) then selectedWord = selectedWord .. tfs[i].text end end -- Check if word is on list for j = 0, 5 do if(selectedWord == L1[j]) then audio.play(bell) currentWords.text = currentWords.text .. ' ' .. selectedWord currentWords:setReferencePoint(display.TopLeftReferencePoint) currentWords.x = 5 correct = correct + 1 end end end if(correct == #L1) then alert() end end function gameListeners(action) if(action == 'add') then gameBg:addEventListener('touch', startDraw) gameBg:addEventListener('touch', detectLetters) else gameBg:removeEventListener('touch', startDraw) gameBg:removeEventListener('touch', detectLetters) end end function alert() gameListeners('rm') alert = display.newImage('alert.png') --alert:addEventListener('tap', restart) or next level! end end function scene:exitScene(event) local group = self.view end function scene:destroyScene(event) local group = self.view end scene:addEventListener( "createScene", scene ) scene:addEventListener( "enterScene", scene ) scene:addEventListener( "exitScene", scene ) scene:addEventListener( "destroyScene", scene ) return scene

thanks in advance if you can help me

Do you ever put your background or images into the screenGroup?

yes i remembered that after i posted the code. I placed the game background and the alert.png into the screengroup. The background displays on the screen however i can’t get the words, it think its something to do with the display.newText in buildsoup() 

Update: The background image in fine its in a group now anyway. It just the words/letters aren’t appearing on the screen, if you don’t know what it should look like, it should look like a word search/word find. I’ve been searching the internet but can’t find a solution yet. any help?

Really all your game functions should become the storyboard functions, that way you won’t have any problems with scope etc.

I’ve re-ordered your code how I would’ve done it:

[lua]

local storyboard = require ( “storyboard” )

local scene = storyboard.newScene()

local screenGroup = display.newGroup()

local tfs = display.newGroup()

local lines = display.newGroup()

local line

local L1 = {“DOG”}

local L1Map = {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},

               {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},

               {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},

               {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},

               {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},

               {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},

               {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},

               {0, 0, 0, 0, 0, 0, 0, “D”, “O”, “G”},

               {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},

               {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},

               {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},

               {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}

local alphabet = {“A”, “B”, “C”, “D”, “E”, “F”, “G”, “H”, “I”, “J”, “K”, “L”, “M”, “N”, “O”, “P”, “Q”, “R”, “S”, “T”, “U”, “V”, “W”, “X”, “Y”, “Z”}

local correct = 0

local wordsList; local clueText; local gameBg

local function hitTestObjects(obj1, obj2)

        local left = obj1.contentBounds.xMin <= obj2.contentBounds.xMin and obj1.contentBounds.xMax >= obj2.contentBounds.xMin

        local right = obj1.contentBounds.xMin >= obj2.contentBounds.xMin and obj1.contentBounds.xMin <= obj2.contentBounds.xMax

        local up = obj1.contentBounds.yMin <= obj2.contentBounds.yMin and obj1.contentBounds.yMax >= obj2.contentBounds.yMin

        local down = obj1.contentBounds.yMin >= obj2.contentBounds.yMin and obj1.contentBounds.yMin <= obj2.contentBounds.yMax

        return (left or right) and (up or down)

end

local startDraw = function (e) – I get an error on this line: attempt to index global “start draw” <a nil value>

    if(e.phase == “began”) then

        initX = e.x

        initY = e.y

    elseif(e.phase == “moved”) then

        if line then display.remove(line); line = nil; end

            line = display.newLine(initX, initY, e.x, e.y)

            line.width = 16

            line:setColor(255, 142, 42, 60)

            lines:insert(line)

    elseif(e.phase == “ended”) then

        local selectedWord = “”

        for i = 1, tfs.numChildren do

            if(hitTestObjects(lines[lines.numChildren], tfs[i])) then

                selectedWord = selectedWord … tfs[i].text

            end

        end

        – Check if word is on list

        for j = 0, 5 do

            if(selectedWord == L1[j]) then

                audio.play(bell)

                currentWords.text = currentWords.text … " " … selectedWord

                currentWords:setReferencePoint(display.TopLeftReferencePoint)

                currentWords.x = 5

                correct = correct + 1

            end

        end

        print (CORRECT)

    if(correct == #L1) then

        --alert()

        print ("CORRECT "…#L1)

    end

    end

end

local function gameListeners(action)

    if(action == “add”) then

        gameBg:addEventListener(“touch”, startDraw)

        --gameBg:addEventListener(“touch”, detectLetters)

    else

        gameBg:removeEventListener(“touch”, startDraw)

–gameBg:removeEventListener(“touch”, detectLetters)

    end

end

local function alert()

    gameListeners(“rm”)

–    alert = display.newImage(“alert.png”)

–    --alert:addEventListener(“tap”, restart) or next level!

end

function scene:createScene(event)

     screenGroup = self.view

     local createHud = function ()

        gameBg = display.newImage(“bg.png”)

        screenGroup:insert(gameBg)

        clueText = display.newText( “DOG”, 0, 450, “Arial”, 16)

        clueText:setTextColor(255, 255, 255)

        clueText.x = 160

        screenGroup:insert(clueText)

     end

     local buildSoup = function ()

        for i = 1, 10 do

            for j = 1, 12 do

                local tf = display.newText("", 0, 0, “Arial”, 19)

                tf:setTextColor(102, 102, 102)

                --tf.width = 320

                --tf.height = 480

                tf.x = math.floor(-10 + (31.3 * i))

                tf.y = math.floor(-10 + (35 * j))

            – Change 0"s to Random Letters

                if(L1Map[j][i] == 0) then

                    L1Map[j][i] = alphabet[math.floor(math.random() * table.maxn(alphabet))+1]

                end

                tf.text = L1Map[j][i]

                tfs:insert(tf)

            end

        end

     end

     local wordsList = function ()

     local wordsString = “”

    for i = 1, #L1 do

        wordsString = wordsString … " " … L1[i]

    end

    wordsList = display.newText(wordsString, 10000, 10000, “Arial”, 14)

    wordsList:setTextColor(238, 238, 238)

    screenGroup:insert(wordsList)

    currentWords = display.newText("", 10000, 10000, “Arial”, 14)

    currentWords:setTextColor(238, 146, 63)

    screenGroup:insert(currentWords)

     end

     createHud()

     buildSoup()

     wordsList()

     screenGroup:insert(tfs)

end

function scene:enterScene(event)

     local group = self.view

–function alert()

gameListeners(“add”)

–    alert = display.newImage(“alert.png”)

–    --alert:addEventListener(“tap”, restart) or next level!

–end

end

function scene:exitScene(event)

     local group = self.view

end

function scene:destroyScene(event)

     local group = self.view

end

scene:addEventListener( “createScene”, scene )

scene:addEventListener( “enterScene”, scene )

scene:addEventListener( “exitScene”, scene )

scene:addEventListener( “destroyScene”, scene )

return scene

[/lua]

Hi there.

What order have you put everything into the screen group? I have a feeling you may have put your background in the group last and then it will be displayed on top of everything else. 

Also, are there any errors in your terminal when you run the app?

Thanks guys Nick Sherman’s solution seemed to work, I understand most of the storyboard concept now :smiley:

Do you ever put your background or images into the screenGroup?

yes i remembered that after i posted the code. I placed the game background and the alert.png into the screengroup. The background displays on the screen however i can’t get the words, it think its something to do with the display.newText in buildsoup() 

Update: The background image in fine its in a group now anyway. It just the words/letters aren’t appearing on the screen, if you don’t know what it should look like, it should look like a word search/word find. I’ve been searching the internet but can’t find a solution yet. any help?

Really all your game functions should become the storyboard functions, that way you won’t have any problems with scope etc.

I’ve re-ordered your code how I would’ve done it:

[lua]

local storyboard = require ( “storyboard” )

local scene = storyboard.newScene()

local screenGroup = display.newGroup()

local tfs = display.newGroup()

local lines = display.newGroup()

local line

local L1 = {“DOG”}

local L1Map = {{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},

               {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},

               {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},

               {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},

               {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},

               {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},

               {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},

               {0, 0, 0, 0, 0, 0, 0, “D”, “O”, “G”},

               {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},

               {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},

               {0, 0, 0, 0, 0, 0, 0, 0, 0, 0},

               {0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}

local alphabet = {“A”, “B”, “C”, “D”, “E”, “F”, “G”, “H”, “I”, “J”, “K”, “L”, “M”, “N”, “O”, “P”, “Q”, “R”, “S”, “T”, “U”, “V”, “W”, “X”, “Y”, “Z”}

local correct = 0

local wordsList; local clueText; local gameBg

local function hitTestObjects(obj1, obj2)

        local left = obj1.contentBounds.xMin <= obj2.contentBounds.xMin and obj1.contentBounds.xMax >= obj2.contentBounds.xMin

        local right = obj1.contentBounds.xMin >= obj2.contentBounds.xMin and obj1.contentBounds.xMin <= obj2.contentBounds.xMax

        local up = obj1.contentBounds.yMin <= obj2.contentBounds.yMin and obj1.contentBounds.yMax >= obj2.contentBounds.yMin

        local down = obj1.contentBounds.yMin >= obj2.contentBounds.yMin and obj1.contentBounds.yMin <= obj2.contentBounds.yMax

        return (left or right) and (up or down)

end

local startDraw = function (e) – I get an error on this line: attempt to index global “start draw” <a nil value>

    if(e.phase == “began”) then

        initX = e.x

        initY = e.y

    elseif(e.phase == “moved”) then

        if line then display.remove(line); line = nil; end

            line = display.newLine(initX, initY, e.x, e.y)

            line.width = 16

            line:setColor(255, 142, 42, 60)

            lines:insert(line)

    elseif(e.phase == “ended”) then

        local selectedWord = “”

        for i = 1, tfs.numChildren do

            if(hitTestObjects(lines[lines.numChildren], tfs[i])) then

                selectedWord = selectedWord … tfs[i].text

            end

        end

        – Check if word is on list

        for j = 0, 5 do

            if(selectedWord == L1[j]) then

                audio.play(bell)

                currentWords.text = currentWords.text … " " … selectedWord

                currentWords:setReferencePoint(display.TopLeftReferencePoint)

                currentWords.x = 5

                correct = correct + 1

            end

        end

        print (CORRECT)

    if(correct == #L1) then

        --alert()

        print ("CORRECT "…#L1)

    end

    end

end

local function gameListeners(action)

    if(action == “add”) then

        gameBg:addEventListener(“touch”, startDraw)

        --gameBg:addEventListener(“touch”, detectLetters)

    else

        gameBg:removeEventListener(“touch”, startDraw)

–gameBg:removeEventListener(“touch”, detectLetters)

    end

end

local function alert()

    gameListeners(“rm”)

–    alert = display.newImage(“alert.png”)

–    --alert:addEventListener(“tap”, restart) or next level!

end

function scene:createScene(event)

     screenGroup = self.view

     local createHud = function ()

        gameBg = display.newImage(“bg.png”)

        screenGroup:insert(gameBg)

        clueText = display.newText( “DOG”, 0, 450, “Arial”, 16)

        clueText:setTextColor(255, 255, 255)

        clueText.x = 160

        screenGroup:insert(clueText)

     end

     local buildSoup = function ()

        for i = 1, 10 do

            for j = 1, 12 do

                local tf = display.newText("", 0, 0, “Arial”, 19)

                tf:setTextColor(102, 102, 102)

                --tf.width = 320

                --tf.height = 480

                tf.x = math.floor(-10 + (31.3 * i))

                tf.y = math.floor(-10 + (35 * j))

            – Change 0"s to Random Letters

                if(L1Map[j][i] == 0) then

                    L1Map[j][i] = alphabet[math.floor(math.random() * table.maxn(alphabet))+1]

                end

                tf.text = L1Map[j][i]

                tfs:insert(tf)

            end

        end

     end

     local wordsList = function ()

     local wordsString = “”

    for i = 1, #L1 do

        wordsString = wordsString … " " … L1[i]

    end

    wordsList = display.newText(wordsString, 10000, 10000, “Arial”, 14)

    wordsList:setTextColor(238, 238, 238)

    screenGroup:insert(wordsList)

    currentWords = display.newText("", 10000, 10000, “Arial”, 14)

    currentWords:setTextColor(238, 146, 63)

    screenGroup:insert(currentWords)

     end

     createHud()

     buildSoup()

     wordsList()

     screenGroup:insert(tfs)

end

function scene:enterScene(event)

     local group = self.view

–function alert()

gameListeners(“add”)

–    alert = display.newImage(“alert.png”)

–    --alert:addEventListener(“tap”, restart) or next level!

–end

end

function scene:exitScene(event)

     local group = self.view

end

function scene:destroyScene(event)

     local group = self.view

end

scene:addEventListener( “createScene”, scene )

scene:addEventListener( “enterScene”, scene )

scene:addEventListener( “exitScene”, scene )

scene:addEventListener( “destroyScene”, scene )

return scene

[/lua]

Hi there.

What order have you put everything into the screen group? I have a feeling you may have put your background in the group last and then it will be displayed on top of everything else. 

Also, are there any errors in your terminal when you run the app?

Thanks guys Nick Sherman’s solution seemed to work, I understand most of the storyboard concept now :smiley: