Move Object while holding Button

Hello there, I started to use Corona and I really like it, though I ran into a problem:
You see, I want to make the object move to the left, so I made a button that when tapped, the object goes left.
But now, the problem is, I want that it constantly moves to the right while the player holds the button.

Is there any easy way to do this? The one thing I use is like this:

function leftbutton:tap (event) playertank.x = (playertank.x)-5 playertank.rotation = -180 end [import]uid: 8901 topic_id: 2071 reply_id: 302071[/import]

Maybe I didn’t explain this clearly, and there is no edit button. :S

Well, the idea is, that like on the joystick. If you hold the left button, the character moves to the left.
Now, I want to do this too with a button. But the problem is, that even if I’m holding the button, the character won’t move left anymore. You have to repeateadly press the button to move.

Now the question is, is there any possible way, so that Corona checks if the player is holding the button? Like onHold and onRelease or something similar. [import]uid: 8901 topic_id: 2071 reply_id: 6197[/import]

Sorry you didn’t get a reply… it’s not cause you didn’t explain yourself, it’s cause the game engine forum is down. I’ve posted questions in the other forums and got quick replies, after almost 17 hours I’ve gotten no reply (except myself). I wish more staff would view these forums, it’s really annoying :frowning:

Anyways, I believe for touch events you can figure out wether you’re touching or releasing, so why not just do while touched do then move your character. Not sure if it’ll work cause I’m kinda new, but theoretically it should :slight_smile: [import]uid: 9033 topic_id: 2071 reply_id: 6201[/import]

Yeah, I noticed this in your threads too. Game Edition needs some more love. :frowning:

Hmm, I don’t seem to get this work with “while button touched do then”
I first get the error “excepted = near do” and after trying a lot, I still get random errors that don’t help me much, hmm. [import]uid: 8901 topic_id: 2071 reply_id: 6207[/import]

Well obviously you don’t type exactly what I wrote… you’ll probably need to get a property of event… maybe like while event.name = “touched” do… im not positive, just go look in the examples in the API and see how they’re printing the type of event (touch or release) then just do a while it’s touched… [import]uid: 9033 topic_id: 2071 reply_id: 6220[/import]

Well your suggestion didn’t work for me, I still get errors.

I’m already trying to do this for few hours now. Searched through the site, through YouTube, no solution. I’m starting to get mad, the buttons still don’t want to work like I want! [import]uid: 8901 topic_id: 2071 reply_id: 6247[/import]

For detecting that a button or object is pressed, I would recommend using “touch” events for the object instead of “tap”. The touch event gives you a event.phase that tells you what’s happening: began, moved, ended. You can use the began and ended phases to move your object. You need to set up an enterFrame event and use that to move the object when the button is still pressed.

You can find examples of using these events in the Corona SDK sample code, /Interface folder. Look at DragMe as an example. If you don’t have Corona SDK, you can download a trial version and look at the sample code.

-Tom [import]uid: 7559 topic_id: 2071 reply_id: 6262[/import]

Another day, another try.

Thanks for your replay. I looked at the code but I don’t seem to find out, how to work with my button. I always get random errors or it won’t work at all.

Searching a little further, I came across the thread “Button onHold”. Now after a little reading, I tried to use this code:

if (lb == true) then  
playertank.x = (playertank.x-5)  
timer.performWithDelay(5, self, 0)  
end  
  
local leftbutton = function(event)  
 if (event.phase == "began") then  
 if (lb == false) then  
 lb = true  
 end  
 elseif (event.phase == "release") then  
 if (lb == true) then  
 lb = false  
 end  
 end  
end  

Well, I don’t get errors, but it won’t work. Nothing happens. So, I’m still at the beginning. [import]uid: 8901 topic_id: 2071 reply_id: 6272[/import]

I don’t see all your code so I don’t know everything that you’re doing but here are some things that might help:

  1. Corona (and Lua) is event driven so your “if (lb == true) then …” code will only be executed once. If you want it to be called in a “loop” so it can move your object, you need to put it into a “onFrame” function. That way it will be called every frame (typically 30 times a second).

  2. Your “timer.performWithDelay” will not work the way you’re using it. This is not needed if you use onFrame event.

  3. If your “leftbutton” function is called from a “touch” event, the “release” phase should be “ended”

The sample code is a great way to learn how to program Lua and Corona. If you study the code you’ll get a good understanding how to get things done. You can also check the code samples in the Code section of this site for more examples.
-Tom [import]uid: 7559 topic_id: 2071 reply_id: 6278[/import]