Corona Runtime Errors... I Can't Solve This One...

I’m making a main menu scene in corona, however I’ve come across an error and its driving me crazy.

Could someone please edit and fix my code?

The compiler makes it confusing for me to understand what it is but I can point out 2 problems from it:

  • attempt to call global “startButtonListeners”
  • [C] in function “startButtonListeners”

Here is the section of code:

function scene:enterScene(event)
local group = self.view
startButtonListeners(‘add’)

function startButtonListeners(action)
if(action == ‘add’) then
aboutBtn:addEventListener(‘tap’, showCredits)
startBtn:addEventListener(‘tap’, startBtn)
end

local function onSceneTouch( self, event )
if event.phase == “began” then
storyboard.gotoScene( “scene1”, fade, 500 )
return true
end
end
end

Hi there,

Try moving the statement startButtonListeners(‘add’) to appear after you define the function.  Right now, you try to call the function before the function is defined, which is why you’re getting the error.

Hope this helps!

  • Andrew

That worked thank you!

I’m not trying to move off topic or double post but is there a reason this code here for the button doesn’t work?

local function onSceneTouch( self, event )
if event.phase == “began” then
storyboard.gotoScene( “scene1”, fade, 500 )
return true
end
end 
end

Instead of: 

[lua]

startBtn:addEventListener(‘tap’, startBtn)

[/lua]

you should have:

[lua]

startBtn:addEventListener(‘tap’, onSceneTouch)

[/lua]

it still doesn’t goto the next scene, and there are no errors., heres what the code looks like now, it feels to me like I’m missing something.

function scene:enterScene(event) local group = self.view function startButtonListeners(action) if(action == 'add') then aboutBtn:addEventListener('tap', showCredits) startBtn:addEventListener('tap', onSceneTouch) end local function onSceneTouch( self, event ) if event.phase == "began" then storyboard.gotoScene( "scene1", fade, 500 ) return true end end startButtonListeners('add') end end

Thanks in advance…

Paste the full code of that file and I can take a look it for you, but only for that part of the code I saw 2 things:

  1. I think you need to define the “onSceneTouch” above the addEventListener;

  2. The fade in your gotoScene should be between “”

full code, i fixed the “fade” bit too btw

--requires local storyboard = require ( "storyboard" ) local scene = storyboard.newScene() --create scene function scene:createScene(event) local screenGroup = self.view titleBg = display.newImage('titleBg.png') title = display.newImage('title.png') title.x = display.contentCenterX title.y = 120 startBtn = display.newImage('startBtn.png') startBtn.x = display.contentCenterX startBtn.y = display.contentCenterY + 20 aboutBtn = display.newImage('aboutBtn.png') aboutBtn.x = display.contentCenterX aboutBtn.y = display.contentCenterY + 65 titleView = display.newGroup() titleView:insert(titleBg) titleView:insert(title) titleView:insert(startBtn) titleView:insert(aboutBtn) end function scene:enterScene(event) local group = self.view function startButtonListeners(action) if(action == 'add') then aboutBtn:addEventListener('tap', showCredits) startBtn:addEventListener('tap', onSceneTouch) end local function onSceneTouch( self, event ) if event.phase == "began" then storyboard.gotoScene( "scene1", "fade", 500 ) return true end end startButtonListeners('add') end end function scene:exitScene( event ) local group = self.view end function scene:destroyScene( event ) end scene:addEventListener( "createScene", scene ) scene:addEventListener( "enterScene", scene ) scene:addEventListener( "exitScene", scene ) scene:addEventListener( "destroyScene", scene ) return scene

Ok. The problem is your enterScene.

Use the one below and you should be fine.

[lua]

function scene:enterScene(event)

    local group = self.view 

    local function onSceneTouch( event )

        if event.phase == " ended" then

           storyboard.gotoScene( “scene1”, “fade”, 500 )

            return true

        end

     end

     function startButtonListeners(action)

         if(action == ‘add’) then  

              aboutBtn:addEventListener(‘touch’, showCredits)

              startBtn:addEventListener(‘touch’, onSceneTouch)

         end 

      end 

    startButtonListeners(‘add’)

    

end

[/lua]

Renato

(http://redbeachgames.com/)’]Red Beach Games

Try our new game  Cut My Puzzle  for free for (https://itunes.apple.com/us/app/cut-my-puzzle/id611051498?ls=1&mt=8)’]iOS or (https://play.google.com/store/apps/details?id=com.redbeachgames.cutmypuzzle)’]Android

well it probably would work but when i click start it still doesn’t take me to the next scene. the next scene is set up with the correct storyboard features. This is really confusing especially since I’m new to the storyboard feature.

Edit: it doesn’t work :frowning:

Hey. Sorry, I forgot to mention: you haven’t defined the function “showCredits” so the file will not work. In addition, just to check, you need to have a main.lua that calls that file that you created (that will then forward you to scene1).

So,

you should have:

main.lua (example)

[lua]

local storyboard = require( “storyboard” )

storyboard.gotoScene(“scene0”)

[/lua]

scene0.lua

[lua]

–requires

local storyboard = require ( “storyboard” )

local scene = storyboard.newScene() 

–create scene

function scene:createScene(event) 

     local screenGroup = self.view 

    

    

     titleBg = display.newImage(‘titleBg.png’)

    

     title = display.newImage(‘title.png’)

     title.x = display.contentCenterX

     title.y = 120

    

     startBtn = display.newImage(‘startBtn.png’)

     startBtn.x = display.contentCenterX

     startBtn.y = display.contentCenterY + 20

    

     aboutBtn = display.newImage(‘aboutBtn.png’)

     aboutBtn.x = display.contentCenterX

     aboutBtn.y = display.contentCenterY + 65

    

     titleView = display.newGroup()

     titleView:insert(titleBg)

     titleView:insert(title)

     titleView:insert(startBtn)

     titleView:insert(aboutBtn)

    

end 

function scene:enterScene(event)

    local group = self.view 

    local function onSceneTouch( event )

        if event.phase == “ended” then

           storyboard.gotoScene( “scene1”, “fade”, 500 )

            return true

        end

     end

     function startButtonListeners(action)

         if(action == ‘add’) then  

       –       aboutBtn:addEventListener(‘touch’, showCredits)   – I commented this line because this function is not defined

              startBtn:addEventListener(‘touch’, onSceneTouch)

         end 

      end 

    startButtonListeners(‘add’)

    

end

function scene:exitScene( event )

     local group = self.view

    

end

function scene:destroyScene( event )

end

scene:addEventListener( “createScene”, scene )

scene:addEventListener( “enterScene”, scene )

scene:addEventListener( “exitScene”, scene )

scene:addEventListener( “destroyScene”, scene )

return scene

[/lua]

scene1.lua

[lua]

local storyboard = require( “storyboard” )

local scene = storyboard.newScene()

function scene:createScene( event )

    

    local group = self.view

    

end

– Called immediately after scene has moved onscreen:

function scene:enterScene( event )

    local group = self.view

    

 print(“I am the scene1.lua”)

end

– Called when scene is about to move offscreen:

function scene:exitScene( event )

    local group = self.view

  

end

– Called prior to the removal of scene’s “view” (display group)

function scene:destroyScene( event )

    local group = self.view    

end


– END OF YOUR IMPLEMENTATION


– “createScene” event is dispatched if scene’s view does not exist

scene:addEventListener( “createScene”, scene )

– “enterScene” event is dispatched whenever scene transition has finished

scene:addEventListener( “enterScene”, scene )

– “exitScene” event is dispatched before next scene’s transition begins

scene:addEventListener( “exitScene”, scene )

– “destroyScene” event is dispatched before view is unloaded, which can be

– automatically unloaded in low memory situations, or explicitly via a call to

– storyboard.purgeScene() or storyboard.removeScene().

scene:addEventListener( “destroyScene”, scene )


return scene

[/lua]

Renato

(http://redbeachgames.com/)’]Red Beach Games

Try our new game  Cut My Puzzle  for free for (https://itunes.apple.com/us/app/cut-my-puzzle/id611051498?ls=1&mt=8)’]iOS or (https://play.google.com/store/apps/details?id=com.redbeachgames.cutmypuzzle)’]Android

That worked, thank you for your help. When i click start it prints that sentence into the output compiler (whatever you call it). I’m guessing the scene is staying on the screen because I haven’t created exitScene and destroyScene scripts, correct?

Well, in the code that you pasted for me was with the exitScene() and createScene().

oh yes… i did… would storyboard.purgeScene (“scene0”) remove it from view if i put it in one of those?

I’ve tried placing storyboard.purgeScene(“scene0”) in both exitScene and enterScene on the next scene but the objects on the screen still do not disappear. I compared my code with the sample storyboard app in corona sdk. I didn’t notice anything different except for the local function on SceneTouch event was outside of enterScene. Could anyone please help me? I’m still learning thank you.

Your objects are not removed from the screen because you have not inserted them in the storyboard group.

So, to fix that, change remove the line 28 (titleView = display.newGroup()) and change the remaining “titleView” with “screenGroup”.

Thank you, that worked. Well thats all the help i need for now so yeah :smiley:

Hi there,

Try moving the statement startButtonListeners(‘add’) to appear after you define the function.  Right now, you try to call the function before the function is defined, which is why you’re getting the error.

Hope this helps!

  • Andrew

That worked thank you!