new InputHandler(sprite)
The Input Handler is bound to a specific Sprite and is responsible for managing all Input events on that Sprite.
Parameters:
Name | Type | Description |
---|---|---|
sprite |
Phaser.Sprite | The Sprite object to which this Input Handler belongs. |
- Source - input/InputHandler.js, line 14
Members
-
allowHorizontalDrag :boolean
-
Controls if the Sprite is allowed to be dragged horizontally.
- Default Value:
- true
- Source - input/InputHandler.js, line 69
-
allowVerticalDrag :boolean
-
Controls if the Sprite is allowed to be dragged vertically.
- Default Value:
- true
- Source - input/InputHandler.js, line 75
-
boundsRect :Phaser.Rectangle
-
A region of the game world within which the sprite is restricted during drag.
- Source - input/InputHandler.js, line 161
-
boundsSprite :Phaser.Sprite
-
A Sprite the bounds of which this sprite is restricted during drag.
- Source - input/InputHandler.js, line 167
-
bringToTop :boolean
-
If true when this Sprite is clicked or dragged it will automatically be bought to the top of the Group it is within.
- Source - input/InputHandler.js, line 81
-
<internal> checked :boolean
-
A disposable flag used by the Pointer class when performing priority checks.
- Internal:
- This member is internal (protected) and may be modified or removed in the future.
- Source - input/InputHandler.js, line 36
-
downPoint :Phaser.Point
-
A Point object containing the coordinates of the Pointer when it was first pressed down onto this Sprite.
- Source - input/InputHandler.js, line 207
-
dragDistanceThreshold :integer
-
The distance, in pixels, the pointer has to move while being held down, before the Sprite thinks it is being dragged.
- Source - input/InputHandler.js, line 197
-
dragFromCenter :boolean
-
Is the Sprite dragged from its center, or the point at which the Pointer was pressed down upon it?
- Source - input/InputHandler.js, line 182
-
draggable :boolean
-
Is this sprite allowed to be dragged by the mouse? true = yes, false = no
- Source - input/InputHandler.js, line 155
-
dragOffset :Phaser.Point
-
The offset from the Sprites position that dragging takes place from.
- Source - input/InputHandler.js, line 177
-
dragStartPoint :Phaser.Point
-
The Point from which the most recent drag started from. Useful if you need to return an object to its starting position.
- Source - input/InputHandler.js, line 192
-
dragStopBlocksInputUp :boolean
-
If enabled, when the Sprite stops being dragged, it will only dispatch the
onDragStop
event, and not theonInputUp
event. If set tofalse
it will dispatch both events.- Source - input/InputHandler.js, line 187
-
dragTimeThreshold :integer
-
The amount of time, in ms, the pointer has to be held down over the Sprite before it thinks it is being dragged.
- Source - input/InputHandler.js, line 202
-
enabled :boolean
-
If enabled the Input Handler will process input requests and monitor pointer activity.
- Source - input/InputHandler.js, line 30
-
game :Phaser.Game
-
A reference to the currently running game.
- Source - input/InputHandler.js, line 24
-
isDragged :boolean
-
true if the Sprite is being currently dragged.
- Source - input/InputHandler.js, line 63
-
pixelPerfectAlpha :number
-
The alpha tolerance threshold. If the alpha value of the pixel matches or is above this value, it's considered a hit.
- Default Value:
- 255
- Source - input/InputHandler.js, line 149
-
pixelPerfectClick :boolean
-
Set to true to use pixel perfect hit detection when checking if the pointer is over this Sprite when it's clicked or touched. The x/y coordinates of the pointer are tested against the image in combination with the InputHandler.pixelPerfectAlpha value. This feature only works for display objects with image based textures such as Sprites. It won't work on BitmapText or Rope. Warning: This is expensive so only enable if you really need it. Use a pixel perfect check when testing for clicks or touches on the Sprite.
- Source - input/InputHandler.js, line 143
-
pixelPerfectOver :boolean
-
Set to true to use pixel perfect hit detection when checking if the pointer is over this Sprite. The x/y coordinates of the pointer are tested against the image in combination with the InputHandler.pixelPerfectAlpha value. This feature only works for display objects with image based textures such as Sprites. It won't work on BitmapText or Rope. Warning: This is expensive, especially on mobile (where it's not even needed!) so only enable if required. Also see the less-expensive InputHandler.pixelPerfectClick. Use a pixel perfect check when testing for pointer over.
- Source - input/InputHandler.js, line 133
-
priorityID :number
-
The priorityID is used to determine which game objects should get priority when input events occur. For example if you have several Sprites that overlap, by default the one at the top of the display list is given priority for input events. You can stop this from happening by controlling the priorityID value. The higher the value, the more important they are considered to the Input events.
- Source - input/InputHandler.js, line 45
-
scaleLayer :boolean
-
EXPERIMENTAL: Please do not use this property unless you know what it does. Likely to change in the future.
- Source - input/InputHandler.js, line 172
-
snapOffset :Phaser.Point
-
A Point object that contains by how far the Sprite snap is offset.
- Source - input/InputHandler.js, line 87
-
snapOffsetX :number
-
This defines the top-left X coordinate of the snap grid.
- Source - input/InputHandler.js, line 117
-
snapOffsetY :number
-
This defines the top-left Y coordinate of the snap grid..
- Source - input/InputHandler.js, line 123
-
snapOnDrag :boolean
-
When the Sprite is dragged this controls if the center of the Sprite will snap to the pointer on drag or not.
- Source - input/InputHandler.js, line 93
-
snapOnRelease :boolean
-
When the Sprite is dragged this controls if the Sprite will be snapped on release.
- Source - input/InputHandler.js, line 99
-
snapPoint :Phaser.Point
-
If the sprite is set to snap while dragging this holds the point of the most recent 'snap' event.
- Source - input/InputHandler.js, line 212
-
snapX :number
-
When a Sprite has snapping enabled this holds the width of the snap grid.
- Source - input/InputHandler.js, line 105
-
snapY :number
-
When a Sprite has snapping enabled this holds the height of the snap grid.
- Source - input/InputHandler.js, line 111
-
sprite :Phaser.Sprite
-
The Sprite object to which this Input Handler belongs.
- Source - input/InputHandler.js, line 19
-
useHandCursor :boolean
-
On a desktop browser you can set the 'hand' cursor to appear when moving over the Sprite.
- Source - input/InputHandler.js, line 51
Methods
-
checkBoundsRect()
-
Bounds Rect check for the sprite drag
- Source - input/InputHandler.js, line 1657
-
checkBoundsSprite()
-
Parent Sprite Bounds check for the sprite drag.
- Source - input/InputHandler.js, line 1707
-
checkPixel(x, y, pointer) → {boolean}
-
Runs a pixel perfect check against the given x/y coordinates of the Sprite this InputHandler is bound to. It compares the alpha value of the pixel and if >= InputHandler.pixelPerfectAlpha it returns true.
Parameters:
Name Type Argument Description x
number The x coordinate to check.
y
number The y coordinate to check.
pointer
Phaser.Pointer <optional>
The pointer to get the x/y coordinate from if not passed as the first two parameters.
Returns:
boolean -true if there is the alpha of the pixel is >= InputHandler.pixelPerfectAlpha
- Source - input/InputHandler.js, line 802
-
checkPointerDown(pointer, fastTest) → {boolean}
-
Checks if the given pointer is both down and over the Sprite this InputHandler belongs to. Use the
fastTest
flag is to quickly check just the bounding hit area even ifInputHandler.pixelPerfectOver
istrue
.Parameters:
Name Type Argument Default Description pointer
Phaser.Pointer fastTest
boolean <optional>
false Force a simple hit area check even if
pixelPerfectOver
is true for this object?Returns:
boolean -True if the pointer is down, otherwise false.
- Source - input/InputHandler.js, line 713
-
checkPointerOver(pointer, fastTest) → {boolean}
-
Checks if the given pointer is over the Sprite this InputHandler belongs to. Use the
fastTest
flag is to quickly check just the bounding hit area even ifInputHandler.pixelPerfectOver
istrue
.Parameters:
Name Type Argument Default Description pointer
Phaser.Pointer fastTest
boolean <optional>
false Force a simple hit area check even if
pixelPerfectOver
is true for this object?Returns:
boolean -- Source - input/InputHandler.js, line 758
-
destroy()
-
Clean up memory.
- Source - input/InputHandler.js, line 433
-
disableDrag()
-
Stops this sprite from being able to be dragged. If it is currently the target of an active drag it will be stopped immediately; also disables any set callbacks.
- Source - input/InputHandler.js, line 1444
-
disableSnap()
-
Stops the sprite from snapping to a grid during drag or release.
- Source - input/InputHandler.js, line 1645
-
downDuration(pointerId) → {number}
-
If the pointer is currently over this Sprite this returns how long it has been there for in milliseconds.
Parameters:
Name Type Argument Default Description pointerId
integer <optional>
0 Returns:
number -The number of milliseconds the pointer has been pressed down on the Sprite, or -1 if not over.
- Source - input/InputHandler.js, line 1366
-
enableDrag(lockCenter, bringToTop, pixelPerfect, alphaThreshold, boundsRect, boundsSprite)
-
Allow this Sprite to be dragged by any valid pointer.
When the drag begins the Sprite.events.onDragStart event will be dispatched.
When the drag completes by way of the user letting go of the pointer that was dragging the sprite, the Sprite.events.onDragStop event is dispatched.
You can control the thresholds over when a drag starts via the properties:
Pointer.dragDistanceThreshold
the distance, in pixels, that the pointer has to move before the drag will start.Pointer.dragTimeThreshold
the time, in ms, that the pointer must be held down on the Sprite before the drag will start.You can set either (or both) of these properties after enabling a Sprite for drag.
For the duration of the drag the Sprite.events.onDragUpdate event is dispatched. This event is only dispatched when the pointer actually changes position and moves. The event sends 5 parameters:
sprite
,pointer
,dragX
,dragY
andsnapPoint
.Parameters:
Name Type Argument Default Description lockCenter
boolean <optional>
false If false the Sprite will drag from where you click it minus the dragOffset. If true it will center itself to the tip of the mouse pointer.
bringToTop
boolean <optional>
false If true the Sprite will be bought to the top of the rendering list in its current Group.
pixelPerfect
boolean <optional>
false If true it will use a pixel perfect test to see if you clicked the Sprite. False uses the bounding box.
alphaThreshold
boolean <optional>
255 If using pixel perfect collision this specifies the alpha level from 0 to 255 above which a collision is processed.
boundsRect
Phaser.Rectangle <optional>
null If you want to restrict the drag of this sprite to a specific Rectangle, pass the Phaser.Rectangle here, otherwise it's free to drag anywhere.
boundsSprite
Phaser.Sprite <optional>
null If you want to restrict the drag of this sprite to within the bounding box of another sprite, pass it here.
- Source - input/InputHandler.js, line 1386
-
enableSnap(snapX, snapY, onDrag, onRelease, snapOffsetX, snapOffsetY)
-
Make this Sprite snap to the given grid either during drag or when it's released. For example 16x16 as the snapX and snapY would make the sprite snap to every 16 pixels.
Parameters:
Name Type Argument Default Description snapX
number The width of the grid cell to snap to.
snapY
number The height of the grid cell to snap to.
onDrag
boolean <optional>
true If true the sprite will snap to the grid while being dragged.
onRelease
boolean <optional>
false If true the sprite will snap to the grid when released.
snapOffsetX
number <optional>
0 Used to offset the top-left starting point of the snap grid.
snapOffsetY
number <optional>
0 Used to offset the top-left starting point of the snap grid.
- Source - input/InputHandler.js, line 1617
-
globalToLocalX(x)
-
Warning: EXPERIMENTAL
Parameters:
Name Type Description x
number - Source - input/InputHandler.js, line 1527
-
globalToLocalY(y)
-
Warning: EXPERIMENTAL
Parameters:
Name Type Description y
number - Source - input/InputHandler.js, line 1545
-
isPixelPerfect() → {boolean}
-
Is this object using pixel perfect checking?
Returns:
boolean -True if the this InputHandler has either
pixelPerfectClick
orpixelPerfectOver
set totrue
.- Source - input/InputHandler.js, line 498
-
justOut(pointerId, delay) → {boolean}
-
Returns true if the pointer has left the Sprite within the specified delay time (defaults to 500ms, half a second)
Parameters:
Name Type Argument Default Description pointerId
integer <optional>
0 delay
number The time below which the pointer is considered as just out.
Returns:
boolean -- Source - input/InputHandler.js, line 1295
-
justOver(pointerId, delay) → {boolean}
-
Returns true if the pointer has entered the Sprite within the specified delay time (defaults to 500ms, half a second)
Parameters:
Name Type Argument Default Description pointerId
integer <optional>
0 delay
number The time below which the pointer is considered as just over.
Returns:
boolean -- Source - input/InputHandler.js, line 1278
-
justPressed(pointerId, delay) → {boolean}
-
Returns true if the pointer has touched or clicked on the Sprite within the specified delay time (defaults to 500ms, half a second)
Parameters:
Name Type Argument Default Description pointerId
integer <optional>
0 delay
number The time below which the pointer is considered as just over.
Returns:
boolean -- Source - input/InputHandler.js, line 1312
-
justReleased(pointerId, delay) → {boolean}
-
Returns true if the pointer was touching this Sprite, but has been released within the specified delay time (defaults to 500ms, half a second)
Parameters:
Name Type Argument Default Description pointerId
integer <optional>
0 delay
number The time below which the pointer is considered as just out.
Returns:
boolean -- Source - input/InputHandler.js, line 1329
-
overDuration(pointerId) → {number}
-
If the pointer is currently over this Sprite this returns how long it has been there for in milliseconds.
Parameters:
Name Type Argument Default Description pointerId
integer <optional>
0 Returns:
number -The number of milliseconds the pointer has been over the Sprite, or -1 if not over.
- Source - input/InputHandler.js, line 1346
-
pointerDown(pointerId) → {boolean}
-
If the Pointer is down this returns true. This only checks if the Pointer is down, not if it's down over any specific Sprite.
Parameters:
Name Type Argument Default Description pointerId
integer <optional>
0 Returns:
boolean -- True if the given pointer is down, otherwise false.
- Source - input/InputHandler.js, line 542
-
pointerDragged(pointerId) → {boolean}
-
Is this sprite being dragged by the mouse or not?
Parameters:
Name Type Argument Default Description pointerId
integer <optional>
0 Returns:
boolean -True if the pointer is dragging an object, otherwise false.
- Source - input/InputHandler.js, line 698
-
pointerOut(pointerId) → {boolean}
-
Is the Pointer outside of this Sprite?
Parameters:
Name Type Argument Default Description pointerId
integer <optional>
(check all) The ID number of a Pointer to check. If you don't provide a number it will check all Pointers.
Returns:
boolean -True if the given pointer (if a index was given, or any pointer if not) is out of this object.
- Source - input/InputHandler.js, line 637
-
pointerOver(pointerId) → {boolean}
-
Is the Pointer over this Sprite?
Parameters:
Name Type Argument Default Description pointerId
integer <optional>
(check all) The ID number of a Pointer to check. If you don't provide a number it will check all Pointers.
Returns:
boolean -- True if the given pointer (if a index was given, or any pointer if not) is over this object.
- Source - input/InputHandler.js, line 604
-
pointerTimeDown(pointerId) → {number}
-
A timestamp representing when the Pointer first touched the touchscreen.
Parameters:
Name Type Argument Default Description pointerId
integer <optional>
(check all) Returns:
number -- Source - input/InputHandler.js, line 574
-
pointerTimeOut(pointerId) → {number}
-
A timestamp representing when the Pointer left the touchscreen.
Parameters:
Name Type Argument Default Description pointerId
integer <optional>
0 Returns:
number -- Source - input/InputHandler.js, line 683
-
pointerTimeOver(pointerId) → {number}
-
A timestamp representing when the Pointer first touched the touchscreen.
Parameters:
Name Type Argument Default Description pointerId
integer <optional>
0 Returns:
number -- Source - input/InputHandler.js, line 668
-
pointerTimeUp(pointerId) → {number}
-
A timestamp representing when the Pointer left the touchscreen.
Parameters:
Name Type Argument Default Description pointerId
integer <optional>
0 Returns:
number -- Source - input/InputHandler.js, line 589
-
pointerUp(pointerId) → {boolean}
-
If the Pointer is up this returns true. This only checks if the Pointer is up, not if it's up over any specific Sprite.
Parameters:
Name Type Argument Default Description pointerId
integer <optional>
0 Returns:
boolean -- True if the given pointer is up, otherwise false.
- Source - input/InputHandler.js, line 558
-
pointerX(pointerId) → {number}
-
The x coordinate of the Input pointer, relative to the top-left of the parent Sprite. This value is only set when the pointer is over this Sprite.
Parameters:
Name Type Argument Default Description pointerId
integer <optional>
0 Returns:
number -The x coordinate of the Input pointer.
- Source - input/InputHandler.js, line 510
-
pointerY(pointerId) → {number}
-
The y coordinate of the Input pointer, relative to the top-left of the parent Sprite This value is only set when the pointer is over this Sprite.
Parameters:
Name Type Argument Default Description pointerId
integer <optional>
0 Returns:
number -The y coordinate of the Input pointer.
- Source - input/InputHandler.js, line 526
-
reset()
-
Resets the Input Handler and disables it.
- Source - input/InputHandler.js, line 384
-
setDragLock(allowHorizontal, allowVertical)
-
Restricts this sprite to drag movement only on the given axis. Note: If both are set to false the sprite will never move!
Parameters:
Name Type Argument Default Description allowHorizontal
boolean <optional>
true To enable the sprite to be dragged horizontally set to true, otherwise false.
allowVertical
boolean <optional>
true To enable the sprite to be dragged vertically set to true, otherwise false.
- Source - input/InputHandler.js, line 1600
-
start(priority, useHandCursor) → {Phaser.Sprite}
-
Starts the Input Handler running. This is called automatically when you enable input on a Sprite, or can be called directly if you need to set a specific priority.
Parameters:
Name Type Argument Default Description priority
number <optional>
0 Higher priority sprites take click priority over low-priority sprites when they are stacked on-top of each other.
useHandCursor
boolean <optional>
false If true the Sprite will show the hand cursor on mouse-over (doesn't apply to mobile browsers)
Returns:
The Sprite object to which the Input Handler is bound.
- Source - input/InputHandler.js, line 284
-
startDrag(pointer)
-
Called by Pointer when drag starts on this Sprite. Should not usually be called directly.
Parameters:
Name Type Description pointer
Phaser.Pointer - Source - input/InputHandler.js, line 1467
-
stop()
-
Stops the Input Handler from running.
- Source - input/InputHandler.js, line 413
-
stopDrag(pointer)
-
Called by Pointer when drag is stopped on this Sprite. Should not usually be called directly.
Parameters:
Name Type Description pointer
Phaser.Pointer - Source - input/InputHandler.js, line 1563
-
<internal> update(pointer) → {boolean}
-
Internal Update method. This is called automatically and handles the Pointer and drag update loops.
Parameters:
Name Type Description pointer
Phaser.Pointer Returns:
boolean -True if the pointer is still active, otherwise false.
- Internal:
- This member is internal (protected) and may be modified or removed in the future.
- Source - input/InputHandler.js, line 871
-
<internal> validForInput(highestID, highestRenderID, includePixelPerfect) → {boolean}
-
Checks if the object this InputHandler is bound to is valid for consideration in the Pointer move event. This is called by Phaser.Pointer and shouldn't typically be called directly.
Parameters:
Name Type Argument Default Description highestID
number The highest ID currently processed by the Pointer.
highestRenderID
number The highest Render Order ID currently processed by the Pointer.
includePixelPerfect
boolean <optional>
true If this object has
pixelPerfectClick
orpixelPerfectOver
set should it be considered as valid?Returns:
boolean -True if the object this InputHandler is bound to should be considered as valid for input detection.
- Internal:
- This member is internal (protected) and may be modified or removed in the future.
- Source - input/InputHandler.js, line 459