Attaching a newly spawned display object to a touch event

Hey guys and gals

I have several buttons that you touch that will “spawn” a display object directly above the button that you just touched. The newly spawned object can then be touched and dragged around as desired. The issue is that you have to raise your finger after touching the button to then touch the newly spawn object to drag it.

Can anyone think of a way to make it so that when you press the button, the newly “spawned” object becomes the focus of the touch event? Could this be accomplished using setFocus? Hmm im going to try that now but please post any thoughts or comments… thank you [import]uid: 19620 topic_id: 31875 reply_id: 331875[/import]

Hmm I attempted the following after the “spawned object” is created and has a touch even listener added to it…

[lua] --To cancel out the focus of the button being pressed
display.getCurrentStage():setFocus( nil )
event.target.isFocus = false

–Now trying to put focus on the newly spawned object
display.getCurrentStage():setFocus( activeStickers[aSize] )
activeStickers[aSize].isFocus = true[/lua]

No luck [import]uid: 19620 topic_id: 31875 reply_id: 127171[/import]

Good question! It’s achievable by the use of the “dispatchEvent()” API. This is a somewhat unknown feature that lets you “send” (dispatch) an event to an object without the event actually happening. The “event” can be anything you want, even a custom non-Corona-native event like “swipeLeft”… and of course it can be a recognized event like ended, began, moved, etc.

What you basically need to do is create (spawn) your object, apply a touch listener, then dispatch a “touch began” event to it. The listener will assume that the object received a touch, even though your finger never left the screen surface.

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

It can be a bit tricky to get accustomed to this API’s usage, but once you do, it can be among the most powerful in your arsenal.

Brent Sorrentino [import]uid: 9747 topic_id: 31875 reply_id: 127173[/import]

Hey Brent,
Glad to hear that corona supports such a feature, very cool. Been trying to get this working but having trouble. Here is what I am doing…

[lua]–Add our touch event listener to the newly spawned object
activeStickers[aSize]:addEventListener( “touch”, pinchTouch )

–Dispatch a “began” event to the newly spawned object
local thisEvent = { name=“began”, target=activeStickers[aSize] }
activeStickers[aSize]:dispatchEvent( thisEvent )[/lua]

This is what I am doing and it doesn’t seem to be doing anything… what am I doing wrong here? [import]uid: 19620 topic_id: 31875 reply_id: 127206[/import]

Hmm I attempted the following after the “spawned object” is created and has a touch even listener added to it…

[lua] --To cancel out the focus of the button being pressed
display.getCurrentStage():setFocus( nil )
event.target.isFocus = false

–Now trying to put focus on the newly spawned object
display.getCurrentStage():setFocus( activeStickers[aSize] )
activeStickers[aSize].isFocus = true[/lua]

No luck [import]uid: 19620 topic_id: 31875 reply_id: 127171[/import]

Good question! It’s achievable by the use of the “dispatchEvent()” API. This is a somewhat unknown feature that lets you “send” (dispatch) an event to an object without the event actually happening. The “event” can be anything you want, even a custom non-Corona-native event like “swipeLeft”… and of course it can be a recognized event like ended, began, moved, etc.

What you basically need to do is create (spawn) your object, apply a touch listener, then dispatch a “touch began” event to it. The listener will assume that the object received a touch, even though your finger never left the screen surface.

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

It can be a bit tricky to get accustomed to this API’s usage, but once you do, it can be among the most powerful in your arsenal.

Brent Sorrentino [import]uid: 9747 topic_id: 31875 reply_id: 127173[/import]

I am continuing to try and figure this out. Should I need to dispatch a END event to my button before I dispatch a began event to my newly spawned sticker? [import]uid: 19620 topic_id: 31875 reply_id: 127349[/import]

Hey Brent,
Glad to hear that corona supports such a feature, very cool. Been trying to get this working but having trouble. Here is what I am doing…

[lua]–Add our touch event listener to the newly spawned object
activeStickers[aSize]:addEventListener( “touch”, pinchTouch )

–Dispatch a “began” event to the newly spawned object
local thisEvent = { name=“began”, target=activeStickers[aSize] }
activeStickers[aSize]:dispatchEvent( thisEvent )[/lua]

This is what I am doing and it doesn’t seem to be doing anything… what am I doing wrong here? [import]uid: 19620 topic_id: 31875 reply_id: 127206[/import]

Hi again,
It’s confusing at first. Try supplying the type of pseudo-event as the “name” parameter… it should be something like “tap” or “touch”, not “began”… but you should also supply the phase as its own parameter.

Here’s an example from my code:

sliderGroup:dispatchEvent( { name="touch", phase="began", x=event.x } )

Try this approach and see what happens, it should work. :slight_smile:

Brent
[import]uid: 9747 topic_id: 31875 reply_id: 127446[/import]

Thanks Brent,
Yea I finally got it working last night doing as you suggested. I didn’t realize that I needed to send parameters like that. Thanks for your help [import]uid: 19620 topic_id: 31875 reply_id: 127454[/import]

@Brent, also if you have a minute would you mind looking at this topic? http://developer.coronalabs.com/forum/2012/10/07/help-two-finger-rotation#comment-126844

Thanks [import]uid: 19620 topic_id: 31875 reply_id: 127456[/import]

I am continuing to try and figure this out. Should I need to dispatch a END event to my button before I dispatch a began event to my newly spawned sticker? [import]uid: 19620 topic_id: 31875 reply_id: 127349[/import]

Hi again,
It’s confusing at first. Try supplying the type of pseudo-event as the “name” parameter… it should be something like “tap” or “touch”, not “began”… but you should also supply the phase as its own parameter.

Here’s an example from my code:

sliderGroup:dispatchEvent( { name="touch", phase="began", x=event.x } )

Try this approach and see what happens, it should work. :slight_smile:

Brent
[import]uid: 9747 topic_id: 31875 reply_id: 127446[/import]

Thanks Brent,
Yea I finally got it working last night doing as you suggested. I didn’t realize that I needed to send parameters like that. Thanks for your help [import]uid: 19620 topic_id: 31875 reply_id: 127454[/import]

@Brent, also if you have a minute would you mind looking at this topic? http://developer.coronalabs.com/forum/2012/10/07/help-two-finger-rotation#comment-126844

Thanks [import]uid: 19620 topic_id: 31875 reply_id: 127456[/import]