Moving Characters using buttons

Hello community,

I’m quite new in corona and i hope someone could help. I have this game that i have been working on that involves two buttons that allows a character to move left to right or vice versa.

Currently i am using Runtime, attaching a listener in the runtime as the button is pressed, which is not a good idea because after releasing the left/right button and pressing again after 3-4 seconds my character jumps(more like teleports) to another location.

Can someone give me a reference that is the same with my issue?. left and right button that makes my character goes left or right.

Regards,
Ronel [import]uid: 141531 topic_id: 25060 reply_id: 325060[/import]

I would suggest you add your listener to the button object rather than runtime. A fast example would be:

[lua]local function moveLeft(event)
if event.phase = “began” then
myCharacter.x = myCharacter.x + 1
end
end

local function moveRight(event)
if event.phase = “began” then
myCharacter.x = myCharacter.x - 1
end
end

leftButton:addEventListener(“touch”, moveLeft)
RightButton:addEventListener(“touch”, moveRight)[/lua]

The above was written on the fly so it might have some mistakes but should give the general idea on how to control a character via on screen buttons.
[import]uid: 33866 topic_id: 25060 reply_id: 101803[/import]

Found a link that maybe solve my problem. For all beginners that is having similar issues see this link:
http://techority.com/2011/05/01/using-sprites-in-your-corona-iphone-application/
by: Peach Pellen

I hope this solves my problem. [import]uid: 141531 topic_id: 25060 reply_id: 101806[/import]

Hello cl-apps,

First of all thank you for the reply, appreciate it. I think i already did this, it only moves when your tapping the button. I’m was aiming for like holding the button then it moves and after releasing the button it stops something like that.

But, I will try it. Thanks.

PS. by the way I’m using Physics…

Regards
Ronel [import]uid: 141531 topic_id: 25060 reply_id: 101808[/import]

You can also use this to keep moving as the button is pressed:

[lua]local function activate( event ) – Called whenever a button is pressed

if “began” == phase then – If the button has just been pressed

display.getCurrentStage():setFocus( event.target , event.id )
event.target.isFocus = true – Keep event focus on the button
Runtime:addEventListener( “enterFrame”, moveLeft) – Start the event listener to move the sprite
elseif event.target.isFocus then
if “ended” == event.phase or “cancelled” == event.phase then – If the button is no longer pressed
– End Focus
display.getCurrentStage():setFocus( event.target , nil )
event.target.isFocus = false
Runtime:removeEventListener( “enterFrame”, moveLeft) – Remove the event listener
end
end
end[/lua] [import]uid: 33866 topic_id: 25060 reply_id: 101815[/import]

I used somewhat like that but it teleports, and I think I know why.
I used the event.time which continously running and sets it in the x-coordinate, which is a big mistake.

Thanks for the reply and I finally got it to work. Thanks!

Regards,
Ronel [import]uid: 141531 topic_id: 25060 reply_id: 101818[/import]