0
votes

so recentlty for my game I've been working on a button that takes the player back to the main menu, however for some reason it doesn't matter where you touch on the scene, it still goes o the menu. I just want it so that when i click the image that it goes to the menu.

heres the code:

function scene:createScene(event)

  screenGroup = self.view

  local createHud = function ()

    gameBg = display.newImage("bg.png")
    lvlnumber = display.newImage("lvlnumber.png", 0, -6)
    menubutton = display.newImage("menubutton1.png", -10, -6)

    screenGroup:insert(gameBg)
    screenGroup:insert(lvlnumber)
    screenGroup:insert(menubutton)
  end
end 

function scene:enterScene(event)

  local group = self.view

  local function onSceneTouch( event )
    if event.phase == "ended" then
      storyboard.gotoScene( "menu", "slideRight", 500 )
      return true
    end
  end

  function startButtonListeners(action)
    if(action == 'add') then  
      menubutton:addEventListener('touch', onSceneTouch)
    end 
  end 

  startButtonListeners('add')

  gameListeners("add")

end

any help? Thanks!

1
Your createHud function is defined but never gets executed. - Egor Skriptunoff
to execute it don't i just put createHud() at the end? if so i have already done that. - Zac Baker
Still having trouble :(. For some reason the start button on the main menu works but the menu button that takes you to the menu while you are playing doesn't. - Zac Baker

1 Answers

1
votes

For every touch event, use the code above:

local function touchHandler( event )
    if event.phase == "began" then
        -- Some code here --
        display.getCurrentStage():setFocus( event.target )
        event.target.isFocus = true
    elseif event.target.isFocus then
        if event.phase == "moved" then
            -- Some code here --
        elseif event.phase == "ended" then
            -- Some code here --
            display.getCurrentStage():setFocus( nil )
            event.target.isFocus = false
        end
    end
    return true
end