ball move till button is hold

Hi,

I want ball(any object) to move with the button is pressed (button is hold) and move the ball until the button is released. I tried to keep recursive function to keep the object move until the button is released . But the simulator is crashing( i think 'cause of unending loop).

Please help me in figuring out the possible way to move ball.( i used transition function in button1Press funtion to move ball).
(anyway sorry for my english)

Thanks,
_kishore. [import]uid: 43861 topic_id: 7674 reply_id: 307674[/import]

I don`t really understand what exactly you are trying to do (sorry)
but from what I did understand and I hope this helps you, you are
trying to move the ball to a different location while button is pressed
and ball to stop when you let go of button? And you are using transition.to
method?If that is the case I think you could also give your button a

button:addEventListener(“touch”, name of your function)

and in your function set a “began” phase and while phase = “began”
you could apply Impulse force ect to your ball and in your function when
phase = “ended” you can make ball stop

im still pretty new to Corona and Lua/programming itself but I think this
approach could work for you (maybe is not the best) but can give you an idea
or help your game continue in development!

Hope this helps and if you want a sample code I`ll be more the willing to
help you out Good Luck! [import]uid: 30314 topic_id: 7674 reply_id: 27184[/import]

Hi kurosaki88 ,

here is the code i tried implementing EventListener. But simulator is crashing(hanged).

[lua]local function moveRight(event)
– ball:translate(2, 0)

print(event.phase)

while event.phase == “began” do
ball:applyLinearImpulse(0.09 , 0 , ball.x , ball.y)

end

end
navRight:addEventListener(“touch” , moveRight)[/lua]

Please help me out. If possible post code .

Thanks,
_kishore
[import]uid: 43861 topic_id: 7674 reply_id: 27191[/import]

event.phase is only “began” when you first touch your button, you can’t loop that. What you should do is probably something like this:

[code]
local isMovingRight = false

local function moveRight(event)

if(event.phase == “began”) then
isMovingRight = true
display.getCurrentStage():setFocus(event.target, event.id)
elseif(event.phase == “ended”) then
isMovingRight = false
display.getCurrentStage():setFocus(event.target, nil)
end

end

navRight:addEventListener(“touch” , moveRight)

local function CheckIfMoving (event)
if(isMovingRight) then
–move here
ball:applyLinearImpulse(0.09 , 0 , ball.x , ball.y)
end
end

Runtime:addEventListener(“enterFrame”, CheckIfMoving)
[/code] [import]uid: 13180 topic_id: 7674 reply_id: 27323[/import]