【问题标题】:Knowing button press in corona sdk了解电晕sdk中的按钮按下
【发布时间】:2013-10-23 13:56:25
【问题描述】:

在我的corona 应用程序中,我有一个widget button 来移动图像。我能够找到onPress 方法,但找不到检查按钮是否仍被按下的方法。这样用户就不必一遍又一遍地点击同一个按钮来移动图像...

代码:

function move( event )
  local phase = event.phase 
  if "began" == phase then
    define_robo()
    image.x=image.x+2;
  end
end

local movebtn = widget.newButton
{
  width = 50,
  height = 50,
  defaultFile = "left.png",
  overFile = "left.png",
  onPress = move,
}

任何帮助都是可观的......

【问题讨论】:

    标签: button lua coronasdk


    【解决方案1】:

    如果您的问题是您想知道用户的手指何时移动,或者他何时释放按钮,您可以为这些事件添加处理程序: “移动”手指在屏幕上移动。 “结束”了一根手指从屏幕上抬起。

    “开始”仅在他开始触摸屏幕时处理。

    所以你的移动功能是这样的:

    function move( event )
        local phase = event.phase 
        if "began" == phase then
            define_robo()
            image.x=image.x+2;
        elseif "moved" == phase then
             -- your moved code
        elseif "ended" == phase then
             -- your ended code
        end
    end
    

    --根据评论更新: 使用它,将 nDelay 替换为每次移动之间的延迟,将 nTimes 替换为您想要移动的次数:

     function move( event )
        local phase = event.phase 
        if "began" == phase then
            local nDelay = 1000
            local nTimes = 3
    
            define_robo()
            timer.performWithDelay(nDelay, function() image.x=image.x+2 end, nTimes )
        end
    end
    

    【讨论】:

    • 我只希望用户只需按一次按钮,图像就会每隔一段时间移动 2 个像素、4 个像素、6 个像素。
    【解决方案2】:

    试试这个:

    local image = display.newRect(100,100,50,50)  -- Your image
    local timer_1  -- timer
    
    local function move()
      print("move...")
      image.x=image.x+2;
      timer_1 = timer.performWithDelay(10,move,1) -- you can set the time as per your need
    end
    
    local function stopMove()
      print("stopMove...")
      if(timer_1)then timer.cancel(timer_1) end
    end
    
    local movebtn = widget.newButton {
      width = 50,
      height = 50,
      defaultFile = "obj1.png",
      overFile = "obj1.png",
      onPress = move,       -- This will get called when you 'press' the button
      onRelease = stopMove, -- This will get called when you 'release' the button
    }
    

    继续编码...... :)

    【讨论】:

    • 我不明白 timer_1 变量的使用...你能解释清楚一点吗??
    • 通过给定时器命名,我们可以在需要时轻松取消它(如:'if(timer_1)then timer.cancel(timer_1) end')。
    猜你喜欢
    • 1970-01-01
    • 2015-08-29
    • 2014-07-15
    • 2012-12-01
    • 2020-12-14
    • 2022-11-17
    • 1970-01-01
    • 2017-03-19
    • 1970-01-01
    相关资源
    最近更新 更多