Javasamurai
Javasamurai

Reputation: 686

Knowing button press in corona sdk

In my corona aplication, I've a widget button to move an image. I was able to find the onPress method, but failed to find a method to check whether the button is still pressed. So that user don't have to tap the same button over and over again for moving the image...

Code:

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,
}

Any help is appreciable...

Upvotes: 0

Views: 2265

Answers (2)

Krishna Raj Salim
Krishna Raj Salim

Reputation: 7390

Try this:

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
}

Keep coding................ :)

Upvotes: 0

Teddy Engel
Teddy Engel

Reputation: 1006

If your question is that you would like to know when the user's finger is moved, or when he releases the button, you can add handlers for those events: "moved" a finger moved on the screen. "ended" a finger was lifted from the screen.

"began" only handles when he starts touching the screen.

So your move function would be like:

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

-- Updated according to comment: Use this, replacing nDelay by the delay between each move, and nTimes by the number of times you wanna do the move:

 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

Upvotes: 1

Related Questions