简体   繁体   English

Corona SDK触摸事件

[英]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! 谢谢!

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM