Reputation: 1
I want to add buttons in which untill i stop pressing them it will continuously do its functionality.
For example, in the Mario game once we start pressing forward button it will continue move Mario until we leave that button, we don't have to press again and again to move.
Upvotes: 0
Views: 140
Reputation: 11
The above would run at all times because of the enterFrame listener what you are looking for would be more like this....
local function moveLeft(event)
if event.phase=="began" then
character.x=character.x+1
elseif event.phase="ended" then
--do Nothing it wont move anymore anyways
end
end
local leftbutton=display.newImage("bla bla bla.png")
leftButton:addEventListener("touch",moveLeft)
When you use the touch event whatever you tell it to do doesn't stop until you release and it's different from tap because in tap you have to release at a pretty fast rate, and the event only registers when you release.
Upvotes: 1
Reputation: 5577
I assume your question is "how do I make a button that acts continuously until release?" First add a listener for the "touch" event.
Touch events have several phases, for the beginning and end of the touch. So in the listener function use an if/else to respond to the different phases.
if event.phase=="began" then
Runtime.addEventListener("enterFrame", doSomething)
elseif event.phase=="ended" then
Runtime.removeEventListener("enterFrame", doSomething)
Now Move Mario in the doSomething function.
Upvotes: 0