Reputation: 11
Newbie here! :P
I'm trying to create a game in Corona where animals falls from the top of the screen and keep bouncing. When you touch an animal it disappears.
I've drawn all the animals like circles, then, I added a circular physics body to it. The images are PNG with transparency.
The problem is, some details of the animals, such as ears and paws, they are outside of the physics body (what I want, because it seems a better collision this way). Moreover, when I touch outside the animal image sometimes it is pressed on the alpha region of my image and it is counted as a tap, but I have not actually tapped in the animal.
I would like to it disappear when i click only on its physical body region.
Anyone knows how to handle this? Is there a way to add a touch handler for the physics body? (The collision works pretty fine, it's just the touch that is related to the image instead of the physics body).
local rect = display.newImage("img/Animals/cow_a1.png");
rect.x = 60 + math.random( 160 )
rect.y = -100
physics.addBody( rect, { density=9, friction=0.3, bounce=0.3,radius=27} )
function rect:touch(e)
-- Remove the animals from screen and memory
removeAnimal(self);
end
-- Add event listener to the cow
rect:addEventListener("touch", rect);
Upvotes: 1
Views: 392
Reputation: 124
This is because of the rectangular image space. try using MASK on your animal objet with HIT TEST option on (true): try this link for masking images
here is an example:
local displayGroupTmp = display.newGroup( )
displayGroupTmp.id = id + 1
-- creating a slice
local circleSize = Constants.screenX*3.8/4 - 20
local background = display.newImageRect( displayGroupTmp, "images/slice.png", circleSize/2, circleSize*1.5/2 )
background.anchorX = 0
background.anchorY = 0.66
background.x = Constants.screenX/2 + deltaX
background.y = Constants.screenY/2 + deltaY
background:setFillColor( color[1], color[2], color[3] )
-- setting mask of an object to identify the true bounding of the background
local mask = graphics.newMask( "images/sliceMask.png" )
-- mask.anchorX = 0
background.maskX = background.x
background.maskY = background.y
background:setMask( mask )
background.maskScaleX, background.maskScaleY = 0.38,0.38
background.isHitTestMasked = true
Upvotes: 2