Move a statict object help

Hi I need to use a static object in my game and I need to make a left and right continues movement with two button.  But my object only move once. Help

here is the code of the left button:

local function leftBtnTouch( event )  local function moveLeft()   box.x = boxX- 10       --where boxX is the inicial position of the box in scene:create()  end  if event.phase == "began" then    print( "left button touch" )    moveBoxToLeft = timer.performWithDelay(100, moveLeft(), -1)  elseif event.phase == "ended" then   if moveBoxToLeft ~= nil then    timer.cancel( moveBoxToLeft )    moveBoxToLeft = nil   end     end  return true end

I set -1 timer interactions to repeat till “ended” phase come but the box only move once.  To give a better example, I am trying to make a game where there is a box below that picks up the objects that fall from above and I need to move it from left to right to catch the objects. The box needs to be static, is the only way I can do my idea using the physics engine. I would not like the player to make multiple taps to move the box, rather I look for a continuous movement that stops at the “ended” phase.

Thanks in advance DoDi

If I understand your problem correctly you want to achieve continuous movement?

box.x = boxX-10 means the object is moving to the same place every time moveLeft() is called.

Instead try

[lua] box.x = box.x -10 [/lua]

Thanks for your quick response @sporkfin

It moves but not continuously. What I want is when the user leaves the button pressed, the box keeps moving and when the user releases the button, the movement stops.

How about dropping the timer and the “began” phase and having the moveLeft() function trigger continuously when the button is touched?  You can then set the rate of movement inside moveLeft() and the action will stop when the button is released.

@sporkfin

Could you show me correcting the code I have? I try to understand what you explain to me and if the language does not betray me, I consider it the same thing I am doing. Sorry but I’m not seeing what you want to explain to me. I really appreciate your help.

[lua]

local movementSpeed = 2 – speed for moveLeft()

local function moveLeft()

  box.x = box.x - movementSpeed – box moves -2 pixels on the x-axis every frame

end

local function leftBtnTouch( event )

  moveLeft() – while the left button is touched, moveLeft() is called every frame

  return true

end

leftBtn:addEventListener( “touch”, leftBtnTouch ) – add a “touch” listener - NOT “tap”

[/lua]

with this code the box only move 2px when the button is pressed and 2px when button release  :wacko:

I don’t know how to make the timer interactions works well within the function

Yes and ooops!  That’s what should happen, of course. 

Use Runtime listeners instead of timers

[lua]

local movementSpeed = 2 – speed for moveLeft()

local function moveLeft()

  tubey.x = tubey.x - movementSpeed – box moves -2 pixels on the x-axis every frame

end

local function leftBtnTouch( event )

  if event.phase == ‘began’ then

    Runtime:addEventListener(‘enterFrame’, moveLeft)

  elseif event.phase == ‘ended’ or event.phase == ‘cancelled’ then

    Runtime:removeEventListener(‘enterFrame’, moveLeft)

  end

  return true

end

leftBtn:addEventListener( “touch”, leftBtnTouch ) – add a “touch” listener - NOT “tap”

[/lua]

Of course, “tubey” was my test object and should be replaced with “box” in your example.

removing () makes the timer work properly. I missed that one. Thanks again! I will try the “runtime” it looks simpler than the timers.