Zac Baker
Zac Baker

Reputation: 37

Corona SDK touch event

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!

Upvotes: 0

Views: 2013

Answers (1)

Doğancan Arabacı
Doğancan Arabacı

Reputation: 3982

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

Upvotes: 1

Related Questions