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]
