Multiple events for 1 object [SOLVED]

Hi
I’m extremely new to programming in lua. i’m making an application that mimics the same functions as an iBook library, and i was wondering how to have 2 events to 1 object. I have 2 functions, mbHold and mbPress. right now i can drag an image anywhere, but after i let go of the image mbPress will fire. What i want is if the user is holding the image, mbHold will fire and mbPress won’t even after its release. and if the user touches the image really quick, like tapping it with their finger, i want mbPress to fire and not mbHold. here is the code that i have for that:

[lua] local mbPress = function (event)
if event.phase == “ended” then
transition.to(mbCover, { time = 100, xScale = .80, yScale=.80, x=(w/2), y=(h/2)})
end
end

local mbHold = function (event)
mbCover.x = event.x
mbCover.y = event.y
end

mbCover:addEventListener(“touch”, mbHold)
mbCover:addEventListener(“touch”, mbPress)[/lua]

i dunno if this was answered in the forums before, I have been looking for the past 3 hours and I’ve been coding for the past 24 hours straight so my search skills may be jacked up :stuck_out_tongue: if anyone can help that would be awesome! thanks in advance [import]uid: 82086 topic_id: 13939 reply_id: 313939[/import]

you do not need to attach two functions to an object. Seems that you have spend your 3 hours of searching and 24 hours of coding in vain. Head to the API page found ounder Resources-> API on the anscaMobile site.

to answer your question, the touch event has four phases
began,
moved
ended
cancelled

so you can handle either or a combination of the four in your function.

you can find the same by using the event.phase == " began" like you have checked for the ended.

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 13939 reply_id: 51256[/import]

hi jay
thanks for the fast reply :slight_smile:
i tried what you suggested, but im not sure if i’m going it properly. this is the new code i have

[lua]local mbPress = function (event)
if event.phase == “moved” then
mbCover.x = event.x
mbCover.y = event.y
elseif event.phase == “ended” then
transition.to(mbCover, { time = 100, xScale = .80, yScale=.80, x=(w/2), y=(h/2)})
end
end[/lua]

its still the same problem. i can move the image, but as soon as i let go it enlarges, and once enlarged i can still move it. what i want is if i move it it will just move it wont enlarge. and if it is enlarged i shouldnt be able to move it. if i change the “ended” to “began”, once i touch it it will enlarge. and if i make “moved” to “began” it wont move smoothly and i have to keep on touching it to get it to move. im so lost right now. [import]uid: 82086 topic_id: 13939 reply_id: 51257[/import]

Here’s an idea…it’s a little different, but may be useful…

How about you make an enterFrame function…to tell how long the button was held down for…to determine if it is a press or hold.

Here is some pseudo-code…

local buttonTicks = 0  
local buttonPressed = false  
  
local mbPress = function (event)  
  
 if event.phase == "began" then  
 buttonPressed = true  
 buttonsTicks = 0  
 elseif event.phase == "ended" then  
 if(buttonTicks \< 5) then   
 transition.to(mbCover, { time = 100, xScale = .80, yScale=.80, x=(w/2), y=(h/2)})  
 else  
 mbCover.x = event.x  
 mbCover.y = event.y  
 end  
 -- Turn off the buttonPressed logic in the timer function  
 buttonPressed = false  
 buttonTicks = 0  
 end  
  
end  
  
local function timerFn()  
  
 if(buttonPressed == true) then   
 buttonTicks = buttonTicks + 1  
 end  
end  
  
mbCover:addEventListener("touch", mbPress)  
Runtime:addEventListener("enterFrame", timerFn)  

Of course, you may be able to run the “enterFrame” when the “began” phase starts…and turn it off on the “ended” phase :slight_smile:

Enjoy :slight_smile: [import]uid: 12700 topic_id: 13939 reply_id: 51259[/import]

aaah,

>>>
What i want is if the user is holding the image, mbHold will fire and mbPress won’t even after its release. and if the user touches the image really quick, like tapping it with their finger, i want mbPress to fire and not mbHold. here is the code that i have for that:
>>>

have you had a look at the “tap” events instead of “touch” ??

I know you are trying something fancy, and indiegampod’s code could be the closest thing you can have to customise it further.

however if you want a tap, then use tap instead of touch.

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 13939 reply_id: 51262[/import]

hi guys
thank you indiegamepod for the code il have a go at it!
for some strange reason jay the “tap” wont work for me. i tried that a couple of times awhile ago. but il customize the code that indiegamepod gave.

thanks for all the help guys!! ^^ [import]uid: 82086 topic_id: 13939 reply_id: 51264[/import]

Is this something to do with event propagation, i.e. it’s still responding to events further down the chain even though you’ve already handled it? It may be that you need to check out “return true” to stop event propagation once handled.

However, I’m not sure if “return true” simply stops events propagating to other objects, or if it will, for example, stop a current event from progressing through its event chain. [import]uid: 26769 topic_id: 13939 reply_id: 51316[/import]

hi lordmooch
i got it to work now, but thanks for the advice il try take a look into what you suggested and see if i can implement it later on in my app.

thanks for the advice jay ^^

thank you so much indiegamepod!!! your my hero, that code you gave me was a life saver, i modified it a bit and i got the results i wanted!!! ^^
thanks again guys hopefully i can reach the point wer i can roam the forums and help people too ^^ [import]uid: 82086 topic_id: 13939 reply_id: 51449[/import]