0

在我的应用程序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,
}

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

4

2 回答 2

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
于 2013-10-23T14:18:45.480 回答
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
}

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

于 2013-10-24T04:24:33.253 回答