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:

1
2
3
4
5
6
7
8
9
10
11
12
13
        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)

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,

?:)

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

1
2
3
4
5
6
7
8
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

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...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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)

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,

?:)

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!! ^^

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.

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 ^^

views:1648 update:2011/10/11 15:24:38
corona forums © 2003-2011