Difference between revisions of "Sprite:hitTestPoint"

From GiderosMobile
(remove language stuff)
(added example)
Line 16: Line 16:
 
=== Return values ===
 
=== Return values ===
 
'''Returns''' (bool) ''true'' if the given global coordinates are in bounds of the sprite, ''false'' otherwise<br/>
 
'''Returns''' (bool) ''true'' if the given global coordinates are in bounds of the sprite, ''false'' otherwise<br/>
 +
 +
=== Example ===
 +
'''Moves a pixel with the mouse'''
 +
<source lang="lua">
 +
local pixel = Pixel.new(0xff0000, 1, 32, 32)
 +
local x0, y0, focus
 +
pixel:addEventListener(Event.MOUSE_DOWN, function(e)
 +
if pixel:hitTestPoint(e.x, e.y) then
 +
focus = true
 +
x0, y0 = e.x, e.y
 +
e:stopPropagation()
 +
end
 +
end)
 +
pixel:addEventListener(Event.MOUSE_MOVE, function(e)
 +
if focus then
 +
local dx = e.x - x0
 +
local dy = e.y - y0
 +
pixel:setX(self.camera:getX() + dx)
 +
pixel:setY(self.camera:getY() + dy)
 +
x0 = e.x
 +
y0 = e.y
 +
e:stopPropagation()
 +
end
 +
end)
 +
pixel:addEventListener(Event.MOUSE_UP, function(e)
 +
if focus then
 +
focus = false
 +
e:stopPropagation()
 +
end
 +
end)
 +
</source>
  
 
{{Sprite}}
 
{{Sprite}}

Revision as of 09:52, 6 January 2021

Available since: Gideros 2011.6
Class: Sprite

Description

Checks whether the given coordinates (in global coordinate system) is in bounds of the sprite.

(bool) = Sprite:hitTestPoint(x,y,shapeFlag)

Parameters

x: (number) the x coordinate to test
y: (number) the y coordinate to test
shapeFlag: (bool) is hidden, clipping/masking, taken into consideration when testing hit point? optional

Return values

Returns (bool) true if the given global coordinates are in bounds of the sprite, false otherwise

Example

Moves a pixel with the mouse

local pixel = Pixel.new(0xff0000, 1, 32, 32)
local x0, y0, focus
pixel:addEventListener(Event.MOUSE_DOWN, function(e)
	if pixel:hitTestPoint(e.x, e.y) then
		focus = true
		x0, y0 = e.x, e.y
		e:stopPropagation()
	end
end)
pixel:addEventListener(Event.MOUSE_MOVE, function(e)
	if focus then
		local dx = e.x - x0
		local dy = e.y - y0
		pixel:setX(self.camera:getX() + dx)
		pixel:setY(self.camera:getY() + dy)
		x0 = e.x
		y0 = e.y
		e:stopPropagation()
	end
end)
pixel:addEventListener(Event.MOUSE_UP, function(e)
	if focus then
		focus = false
		e:stopPropagation()
	end
end)