I am trying to make a button that when pressed & Held, will make my character move on the screen. What i cant figure out it the correct way to code for this, all i know is that there is “began” and “release”. are there any other phases than this?
what i have right now is im creating a local variable
local accelerateActive = false
then i have my move forward code running on a EnterFrame Listener. And i say that if accelerateActive = true then … move my guy
Now here is what id like to figure out, i want it so while you hold the button down accelerateActive = true. I can get it to turn true when i tap the button once… but i need it to turn active while held down, and once let off then to turn off
any ideas?
here is what i was trying but it does not work…
local function accelerate(event)
if accelerateActive == true then
local s = tireObject.angularVelocity
tireObject.angularVelocity = s + 100
end
end
--
local function moveForward(event)
if event.phase == "began" and menuisActive == false and gameActive == true then
accelerateActive = true
elseif event.phase == "ended" and menuisActive == false and gameActive == true then
accelerateActive = false
end
end
[import]uid: 19620 topic_id: 7545 reply_id: 307545[/import]
There are better ways of doing it, but if you want to go with your above example…
Just add…
Runtime:addEventListener(“enterFrame”, accelerate)
Under your accelerate function
I presume you are already adding an event listener for your moveForward button. Which should be something like
moveforwardButton:addEventListener(“touch”, moveForward) [import]uid: 6981 topic_id: 7545 reply_id: 26780[/import]
Ok ill try that out, you said there are better ways to accomplish this, would you mind sharing? =D [import]uid: 19620 topic_id: 7545 reply_id: 26782[/import]
Also i just found out that with the UI.lua the way that you decide the different phases of your button is in the creation: example:
local button1 = ui.newButton{
default = "buttonRed.png",
over = "buttonRedOver.png",
onPress = button1Press, --this is the function it will call
onRelease = button1Release,
text = "Button 1 Label",
emboss = true
}
so then in your function you would not need to say if event.phase = “began”, because this is contained in the button creation itself, very helpful!
hope it helps someone else, for more on this, check out the buttonEvents sample code [import]uid: 19620 topic_id: 7545 reply_id: 26837[/import]
Actually, there are 4 phases…
began - The initial touch event
moved - When the touch moves without being let go
ended - When the touch stops touching the screen
cancelled - When a device event causes the touch operation to be cancelled for something more important, perhaps a popup alert, but I’m not sure.
However…
I recently committed a library to the Code Exchange which gives you an extra phase…
pre - The initial touch (the original began)
began - When the touch has been held for the single tap millisecond threshold (configurable)
moved - The usual move, but this also cancels the tap event
ended/cancelled - As normal
In the library, “Sanitised Touch Library”, I have made an effort to get the touch events (“Tap” and “Touch”) to recognise the different states of touching and tapping, so that:
Touching and moving quickly does not generate a tap and touch events
Tapping more than once within the tap millisecond threshold counts the taps, instead of firing tap events and touch events for each
Tapping does not generate a touch event
You can find the library here:
http://developer.anscamobile.com/code/sanitised-touch-library
With demo code.
I should point out that it is not tuned for multitouch and that the code to handle the logic is rather unreadable, however it is stable and I do use it in my own game. Also, I intend to produce a more readable, multi-touch friendly version soon.
Matt [import]uid: 8271 topic_id: 7545 reply_id: 26887[/import]
Great! thanks for your post, i always wondered what that was in the exchange code area =P I will check it out for sure, and ill be excited for a Multi Touch solution as well! [import]uid: 19620 topic_id: 7545 reply_id: 26912[/import]