Corona error: attempt to call global “startButtonListeners” <a nil value>

倾然丶 夕夏残阳落幕 提交于 2019-12-11 16:37:45

问题


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

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

回答1:


Change the location of your function startButtonListeners to the end; after your function definition is complete:

scene:enterScene(event)
    local group = self.view 

    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 
    startButtonListeners('add')
end


来源:https://stackoverflow.com/questions/15760576/corona-error-attempt-to-call-global-startbuttonlisteners-a-nil-value

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!