知道按钮按下电晕sdk

时间:2013-10-23 13:56:26

标签: button lua corona

在我的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,
}

任何帮助都很明显......

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

试试这个:

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
}

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