Add eventListener to a physics body?

Hi,

Within a display object, there is an opaque-irregular shape surrounded by transparent space extending to the object’s boundaries. Only the opaque portion of the image should respond to a touch event. I’ve attached a physics body to the opaque portion of the display object, but I can’t figure out how to add an eventListener to the physics body.

When the opaque portion of the object is touched, a sound plays. Touching the transparent portion of the object should do nothing.

The following code plays the sound when a touch is registered anywhere within the object. I’ve tried to attach an event listener to the physics body, but that doesn’t seem to work. Does anyone know how do this? Is it possible?

module(..., package.seeall);  
  
 display.setStatusBar( display.HiddenStatusBar )  
  
function new()  
  
 local localGroup = display.newGroup();  
  
 local na\_sound = audio.loadSound("media/na.aiff");  
  
 local physics = require( "physics" )  
 physics.start()  
 physics.setDrawMode( "hybrid" )  
  
 ---------------------------  
 -- Irregular-shaped hot spot  
 ---------------------------  
  
 local na = display.newImageRect( "images/na.png", 220, 204 )  
 na\_shape = { -87,-62, 38,-100, 109,-81, 109,100, 53,100, -20,46 }  
 na.x = 278  
 na.y = 233  
 physics.addBody( na, "static", {density = 1, friction = 1, bounce = 0, shape = na\_shape});  
 function na:touch(e)  
 if(e.phase == "ended") then  
 --play name  
 audio.play(na\_sound);  
 end  
 end   
  
--[[  
 function na\_shape:(e)  
 if(e.phase == "ended") then  
 --play name  
 audio.play(na\_sound);  
 end  
 end   
--]]  
  
 na:addEventListener("touch", na);  
-- na\_shape:addEventListener("touch", na\_shape);  
  
 localGroup:insert(na);  
-- localGroup:insert(na\_shape);  
  
 return localGroup;  
end  

Thanks very much for any help you can give.

Best,

LB [import]uid: 11631 topic_id: 15787 reply_id: 315787[/import]

No, the whole display image will respond and not just the physics body, so it cannot be done in this way.

You could use two images or alternatively check the x and y of the touch event in relation to the object.

Peach [import]uid: 52491 topic_id: 15787 reply_id: 58345[/import]

Hi Peach,

Thanks _very_ much for the quick response.

I can imagine cases where these would be perfect solutions; unfortunately, I can’t foresee multiple images working this case. Imagine highly irregular shapes (like Europe, Africa, and Asia) that need to seamlessly intersect. I’m new to Lua, but I think it’s impossible to align the objects visually without their transparent areas overlapping.

Is there any facility that allows Lua to ignore transparent areas of an object (like Flash does or [enter way-back machine] SuperCard’s irregular-shaped hot spots)?

I saw something called a ‘polyline.’ Is it possible to create a polyline object, set its alpha to nearly transparent, and attach a touch-based eventListener to that? Would that object ignore touches to its transparent region.

Customized-x,y-based solutions has worked well for me in a puzzle app with a limited number of inconsistently-irregular instances; but, I would eventually need to create custom formulae for hundreds of objects.

Thanks very much for all of the great feedback that you regularly provide in the forum area. Your suggestions are always clear and actionable. Thanks for any help that you can give on this.

Best,

Lynne [import]uid: 11631 topic_id: 15787 reply_id: 58360[/import]

@lbolduc, you might want to look into using 3rd party physics editor tool for irregular shapes. I find it extremely time consuming to plot out the polyline shape (and in some cases, impossible without software tool to achieve it.) [import]uid: 67217 topic_id: 15787 reply_id: 58376[/import]

Hey Lynne,

Thanks for the kind words - I do my best :slight_smile:

Ask was correct in his suggestion; although you can use custom shapes something like Physics Editor would be ideal. There is a trial version available if I recall correctly so perhaps you might give that a go and see if it meets your needs?

Peach :slight_smile: [import]uid: 52491 topic_id: 15787 reply_id: 58553[/import]

Thanks for the feedback. Sorry for delayed response. Was sick, just getting back to work.

Naomi, good point. I discovered Gumbo through the forums. It’s a little primitive, but still fantastic for quickly plucking vertices. It looks like you can’t attach a touch eventListener to a polyline either, so those are out.

Peach, I’m sorry, I’m too new to understand the difference between custom shapes and physics bodies. When I search custom shapes, all answers point to physics body discussions.

All I really need is some way to pick up an object touch in object’s opaque area, while ignoring a touch in object’s transparent areas. This is so basic; it has to be possible, right? If it’s not built in, is there a humane kluge? For example …

Is there some way to pick up the color value held by the touch x,y of the ‘target,’ then test to see whether it’s color value is transparent? A better way?

Thanks for any thoughts or research suggestions.

Best,

Lynne [import]uid: 11631 topic_id: 15787 reply_id: 59764[/import]

Lynne, could you look at Physics Editor (paid third party program, very popular) for custom shapes?

You can’t tell the difference between color and transparency, no, because all the app sees is a square image, until you tell it otherwise.
Peach [import]uid: 52491 topic_id: 15787 reply_id: 59846[/import]

At first glance, it wasn’t immediately obvious how PE is more than a suped-up Gumbo, but I’ll trust your judgement. I’m off to check it out. Thanks, Peach. [import]uid: 11631 topic_id: 15787 reply_id: 59875[/import]

I’m quite certain it has a free trial version to help you get your head around and see if it is for you.

It’s a good tool either way and many people swear by it :slight_smile:

(Gumbo is great but no, they are very different things.)

Peach :slight_smile: [import]uid: 52491 topic_id: 15787 reply_id: 60004[/import]

I agree that Physics Editor is an _elegant_ shape tracer and publisher. Excellent tip. Thank you.

The bigger problem:

Where is documentation that explains how to address the resulting shape in the context of a touch event? Meaning, a touch _only_ within the boundaries of the shape’s defined vertices.

Thanks for any help that you can give.

Best,

Lynne
[import]uid: 11631 topic_id: 15787 reply_id: 60040[/import]

Post related to “ignore touch events on transparent sections of a PNG”


http://developer.anscamobile.com/forum/2011/05/10/touch-events-transparent-png

As of 5/11, short answer seems to be construct a group of best-fit invisible rectangles to cover the opaque portion of your png and have them listen for a touch. Constructing a point in polygon detection algorithm is also mentioned.

Ansca’s elegant, ease-of-use rhetoric falls off the cliff right here. I’m sure they’re busy. Hopefully, they’ll catch up with irregular-shaped-hot-spot state-of-the-art (in 1989) soon. This is a gaping hole in an otherwise brilliant implementation. [import]uid: 11631 topic_id: 15787 reply_id: 60142[/import]