Attempt to call global function

error says what it can’t index cutScene function in continueTap function

local function continueTap(event)
    if event.phase == "began" then
      if pressed == 1 then

        pressed = 0

        if storyNum < 5  then

          storyNum = storyNum + 1
          cutScene() -- Error happends here

          background:removeEventListener("touch", continueTap)

        end

      end
    end
  end

  local function cutScene()
    if storyNum == 1 then

      background = display.newImageRect(sceneGroup,"Images/SpaceBackground.png", actualContentWidth, actualContentHeight)
      background.x = contentCenterX
      background.y = contentCenterY

      story = display.newImageRect(sceneGroup,"Images/story1Template.png", actualContentWidth, actualContentHeight)
      story.x = contentCenterX
      story.y = contentCenterY

      storyText.text = "The rocket has just taken into space from a town called Rockville."

      storyText:toFront()
      continueText:toFront()
      pressed = 1
      continue()
      background:addEventListener("touch", continueTap)

    elseif storyNum == 2 then

      storyText.text = "It's already the second their rocket this year!\n The rocket crew flies in search of other life forms."
      pressed = 1
      continue()
      background:addEventListener("touch", continueTap)

    end

  end

That’s because cutScene is out of scope for continueTap.

With local functions, in order for continueTap to be able to call cutScene, you need to have placed or forward declared it before continueTap.

For instance,

local cutScene

local function continueTap(event)
	....
end

function cutScene()
	....
end
1 Like

thanks for help! :smiley:

If you are working inside a scene, better put everything as fields of the scene.

function scene:cutScene()

Then in the continueTap() function you could do self:cutScene()

2 Likes

Solar (lua) is single pass so code only knows what is above it, not below it (unless it is global).

Adding functions to the scene is how I do it too as they are always available throughout the life cycle of the scene.

1 Like