【问题标题】:Corona SDK touch eventCorona SDK 触摸事件
【发布时间】:2013-04-01 19:40:13
【问题描述】:

所以最近我一直在为我的游戏制作一个按钮,让玩家回到主菜单,但是由于某种原因,你在场景中触摸的位置并不重要,它仍然会离开菜单。我只是想要它,以便当我单击图像时它会进入菜单。

代码如下:

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

有什么帮助吗?谢谢!

【问题讨论】:

  • 您的createHud 函数已定义但从未被执行。
  • 执行它我不只是把 createHud() 放在最后吗?如果是这样,我已经这样做了。
  • 仍然有问题:(。由于某种原因,主菜单上的开始按钮可以工作,但在您播放时将您带到菜单的菜单按钮却没有。

标签: lua coronasdk


【解决方案1】:

对于每个触摸事件,使用上面的代码:

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

【讨论】:

  • 我绑定了你的代码,无论我在哪里点击游戏,它似乎仍然会进入菜单。
  • 其实我认为它是因为图像尺寸是 320x480 时它只需要左上角,我会调整它的大小并让你知道它是否有效
  • 是的,它是图像大小,不过感谢您的帮助,我现在在我的项目中使用该代码。
  • 哦,好的。我虽然这与焦点有关,但确定大小很重要:D
猜你喜欢
  • 2012-02-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-17
  • 1970-01-01
  • 2014-01-07
  • 2015-04-22
  • 1970-01-01
相关资源
最近更新 更多