Difference between revisions of "Sprite:hitTestPoint"

From GiderosMobile
m
 
(11 intermediate revisions by 3 users not shown)
Line 1: Line 1:
 
__NOTOC__
 
__NOTOC__
<languages />
+
'''Available since:''' Gideros 2011.6<br/>
'''<translate>Available since</translate>:''' Gideros 2011.6<br/>
+
'''Class:''' [[Sprite]]<br/>
'''<translate>Class</translate>:''' [[Special:MyLanguage/Sprite|Sprite]]<br/>
+
 
=== <translate>Description</translate> ===
+
=== Description ===
<translate><br />
+
Checks whether the given coordinates (in global coordinate system) is in bounds of the sprite.
Checks whether the given coordinates (in global coordinate system) is in bounds of the sprite.<br />
+
<syntaxhighlight lang="lua">
<br />
+
(bool) = Sprite:hitTestPoint(x,y,onlyvisible,ref)
<br /></translate>
+
</syntaxhighlight>
<source lang="lua">
+
 
(bool) = Sprite:hitTestPoint(x,y,shapeFlag)
+
=== Parameters ===
</source>
+
'''x''': (number) the x coordinate to test<br/>
=== <translate>Parameters</translate> ===
+
'''y''': (number) the y coordinate to test<br/>
'''x''': (number) <translate></translate> <br/>
+
'''onlyvisible''': (bool) hit test only for visible sprite (clipping/masking) '''optional, default = false'''<br/>
'''y''': (number) <translate></translate> <br/>
+
'''ref''': (Sprite) specifies which sprite is the reference of x,y coordinates. If nil global coordinates are assumed '''optional, default = nil''', '''new Gideros 2023.2'''<br/>
'''shapeFlag''': (bool) <translate>Take clipping/masking into consideration when testing hit point</translate> '''optional'''<br/>
+
 
=== <translate>Return values</translate> ===
+
=== Return values ===
'''<translate>Returns</translate>''' (bool) <translate>''true'' if the given global coordinates are in bounds of the sprite, ''false'' otherwise.</translate><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'''
 +
<syntaxhighlight 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)
 +
</syntaxhighlight>
 +
 
 +
{{Sprite}}

Latest revision as of 05:28, 23 April 2024

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,onlyvisible,ref)

Parameters

x: (number) the x coordinate to test
y: (number) the y coordinate to test
onlyvisible: (bool) hit test only for visible sprite (clipping/masking) optional, default = false
ref: (Sprite) specifies which sprite is the reference of x,y coordinates. If nil global coordinates are assumed optional, default = nil, new Gideros 2023.2

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)