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?

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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

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

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

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

views:1485 update:2011/10/16 9:47:44
corona forums © 2003-2011