Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Circle hitTestPoint — Gideros Forum

Circle hitTestPoint

eezingeezing Member
edited October 2013 in Code snippets
New to Gideros here and needed a simple hitTestPoint for circle buttons. Searched the forum and the solutions posted although very good, appear to be a little too robust.

So here's the solution I came up with. Please post if something looks off or if some efficiency can be attained.

1. Duplicated the generic button class found in the Gideros 'button' example. Renamed class to 'ButtonCircle'.

2. Search and replace:
self:hitTestPoint(event.x, event.y)
-- For this:
self:ButtonCircleHitTest(event.x, event.y)
3. Add function this function to class:
-- Assumes:
-- ButtonCircle is child to a parent button
-- Parent button anchorPoint = (.5, .5)
 
function ButtonCircle:circleHitTest(eventX, eventY)
 
	-- Get these if we don't have them yet
	if not self.myParent or not self.myRadius then 
		self.myParent = self:getParent()
		self.myRadius = self.myParent:getHeight() * .5
	end
 
	local x, y = self.myParent:getPosition() -- Grab position from parent
 
	-- Is touch distance from center within radius of circle?
	if self.myRadius > math.sqrt((eventX - x)^2 + (eventY - y)^2) then 
		return true
	end
end

Likes: ar2rsawseen

+1 -1 (+1 / -0 )Share on Facebook

Comments

Sign In or Register to comment.