Touch event handling HELP please

hello developers …
i want to add touch listener for a circular object (“img.png”) so when user touching the transparent pixels from the image the event fired so it is causes problem for me how can i avoid transparent parts from my images from being touched
thanks in advance [import]uid: 74537 topic_id: 20539 reply_id: 320539[/import]

The event will be fired when you touch the image, and all images are rectangles, it doesn’t matter if it is transparent or not.

In your case, since you’re working with a circular shape, you can compute if the X and Y you’re touching are inside the circle radius.

Lets assume your circle has radius of 50 pixels.
And also assume your using CenterReferencePoint for the image and that the circle is centered within the image.

[code]
– Image is a circle
– Circle has a radius of 50
local circle = display.newImageRect(…)

local function touchListener(e)
local deltaX = e.x - e.target.x
local deltaY = e.y - e.target.y
local dist = math.sqrt(deltaX*deltaX + deltaY * deltaY)
if(dist < 50) then
if(e.phase == “began”) then
– …
elseif(e.phase == “moved”) then
– …
elseif(e.phase == “ended”) then
– …
end
end
end

circle:addEventListener(“touch”,touchListener)
[/code] [import]uid: 61899 topic_id: 20539 reply_id: 80596[/import]

thanks @CluelessIdeas what if i have object that is not in shape of circle like (tree branch ) how can i do that ?? please
[import]uid: 74537 topic_id: 20539 reply_id: 80598[/import]

Unless there is a way to ignore transparent pixels and I’m unaware of it, the only way to do it is to be able to figure out by an X and Y coordinate if a point belongs to the transparent part of the image or not.

If you could get from an image the pixel color of a certain X and Y it would help, but I also think there is no way of doing that in Corona.

So basically you’re left with having a geometrical way of figuring out if a point is transparent or not. In the case of a circle it is very easy, since you only have to find the distance to the center, and farther than the radius, means outside the circle.

In more complex shapes, you won’t do this easily. [import]uid: 61899 topic_id: 20539 reply_id: 80600[/import]

You could create a circle using display.newCircle() and make it transparent (.isVisible = false) but make it hit testable (.isHitTestable = true) and assign your event listener to the circle instead of the graphic.

If the graphic moves, you just have to make sure to move the circle with it.

Masking might also work, but I’ve never tried it. [import]uid: 19626 topic_id: 20539 reply_id: 80626[/import]

@robmiracle thanks bug i need touch event for polygon not circle how can i do it ??

[import]uid: 74537 topic_id: 20539 reply_id: 80629[/import]

Well you did say “Circular object” :wink: :slight_smile: :slight_smile:

Polygons are not a strong suit of Corona. Besides touch isn’t that precise anyway, so unless its a really big polygon, you need to leave a little room anyway.

I unfortunately don’t have a good answer for polygons. [import]uid: 19626 topic_id: 20539 reply_id: 80634[/import]