[Resolved] Running a function

So I have quite a lot of buttons in my app. Therefor I made a lot of

if event.phase == "ended" then  
end  

for touch events. But sometimes I want to run the function without the touch. What do I do? I tried figuring it out for a whole 2 minutes and couldn’t come up with anything =D hoping someone can just give me the answer.
Something like

if event.phase == "ended" or functionIsCalled then  
bla blaa  
end  

Anyone? [import]uid: 77199 topic_id: 33652 reply_id: 333652[/import]

Hello,
This sounds like a case for “object:dispatchEvent”. This useful function allows you to pass a “pseudo event” to an object, i.e. a button, along with a theoretical phase… it can actually do more than just that, but in your case, let’s keep it simple.

Basically, you want to “dispatch” an event to the button with a name of “touch”, a phase of “ended”, and a target of the button itself. This causes the button to THINK that it has received a touch ended event, even though the user didn’t actually touch the screen at all. Convenient, yes? :wink:

http://docs.coronalabs.com/api/type/EventListener/dispatchEvent.html

Brent [import]uid: 200026 topic_id: 33652 reply_id: 133805[/import]

Thank you very much kind sir! Exactly what I was looking for. Let me give an example for others since the documentation doesn’t tell you directly.

For numTaps = 2 or touch events you just do this

whateverName = { name = "tap", numTaps = 2, target = buttonName }  
whateverName = { name = "touch", phase = "ended", target = buttonName }  

[import]uid: 77199 topic_id: 33652 reply_id: 133952[/import]

Yep, looks like you’ve got it! The keys in the table like “numTaps” or “phase” are extended to the event listener as “event.numTaps” or “event.phase”, so you’re making Corona think that actually happened. As you can see, it’s an API with alot of potential, and in some cases, it has been a life saver for me (because honestly, no other method would have done it as well or as efficiently as this one).

Brent
[import]uid: 200026 topic_id: 33652 reply_id: 133967[/import]

Hello,
This sounds like a case for “object:dispatchEvent”. This useful function allows you to pass a “pseudo event” to an object, i.e. a button, along with a theoretical phase… it can actually do more than just that, but in your case, let’s keep it simple.

Basically, you want to “dispatch” an event to the button with a name of “touch”, a phase of “ended”, and a target of the button itself. This causes the button to THINK that it has received a touch ended event, even though the user didn’t actually touch the screen at all. Convenient, yes? :wink:

http://docs.coronalabs.com/api/type/EventListener/dispatchEvent.html

Brent [import]uid: 200026 topic_id: 33652 reply_id: 133805[/import]

Thank you very much kind sir! Exactly what I was looking for. Let me give an example for others since the documentation doesn’t tell you directly.

For numTaps = 2 or touch events you just do this

whateverName = { name = "tap", numTaps = 2, target = buttonName }  
whateverName = { name = "touch", phase = "ended", target = buttonName }  

[import]uid: 77199 topic_id: 33652 reply_id: 133952[/import]

Yep, looks like you’ve got it! The keys in the table like “numTaps” or “phase” are extended to the event listener as “event.numTaps” or “event.phase”, so you’re making Corona think that actually happened. As you can see, it’s an API with alot of potential, and in some cases, it has been a life saver for me (because honestly, no other method would have done it as well or as efficiently as this one).

Brent
[import]uid: 200026 topic_id: 33652 reply_id: 133967[/import]