Class: Input

Phaser. Input

new Input(game)

Phaser.Input is the Input Manager for all types of Input across Phaser, including mouse, keyboard, touch and MSPointer. The Input manager is updated automatically by the core game loop.

Parameters:
Name Type Description
game Phaser.Game

Current game instance.

Source:
src/input/Input.js line 15

Members

<static, constant> MAX_POINTERS : integer

The maximum number of pointers that can be added. This excludes the mouse pointer.

Type:
  • integer
Source:
src/input/Input.js line 411

<static, constant> MOUSE_OVERRIDES_TOUCH : number

Type:
  • number
Source:
src/input/Input.js line 392

<static, constant> MOUSE_TOUCH_COMBINE : number

Type:
  • number
Source:
src/input/Input.js line 404

<static, constant> TOUCH_OVERRIDES_MOUSE : number

Type:
  • number
Source:
src/input/Input.js line 398

activePointer : Phaser.Pointer

The most recently active Pointer object.

When you've limited max pointers to 1 this will accurately be either the first finger touched or mouse.

Type:
Source:
src/input/Input.js line 237

circle : Phaser.Circle

A Circle object centered on the x/y screen coordinates of the Input. Default size of 44px (Apples recommended "finger tip" size) but can be changed to anything.

Type:
Source:
src/input/Input.js line 91

doubleTapRate : number

The number of milliseconds between taps of the same Pointer for it to be considered a double tap / click.

Type:
  • number
Default Value:
  • 300
Source:
src/input/Input.js line 118

enabled : boolean

When enabled, input (eg. Keyboard, Mouse, Touch) will be processed - as long as the individual sources are enabled themselves.

When not enabled, all input sources are ignored. To disable just one type of input; for example, the Mouse, use input.mouse.enabled = false.

Type:
  • boolean
Default Value:
  • true
Source:
src/input/Input.js line 67

game : Phaser.Game

A reference to the currently running game.

Type:
Source:
src/input/Input.js line 20

gamepad : Phaser.Gamepad

The Gamepad Input manager.

Type:
Source:
src/input/Input.js line 290

hitCanvas : HTMLCanvasElement

The canvas to which single pixels are drawn in order to perform pixel-perfect hit detection.

Type:
  • HTMLCanvasElement
Source:
src/input/Input.js line 26

hitContext : CanvasRenderingContext2D

The context of the pixel perfect hit canvas.

Type:
  • CanvasRenderingContext2D
Source:
src/input/Input.js line 32

holdRate : number

The number of milliseconds that the Pointer has to be pressed down for it to fire a onHold event.

Type:
  • number
Default Value:
  • 2000
Source:
src/input/Input.js line 124

interactiveItems : Phaser.ArraySet

A list of interactive objects. The InputHandler components add and remove themselves from this list.

Type:
Source:
src/input/Input.js line 355

justPressedRate : number

The number of milliseconds below which the Pointer is considered justPressed.

Type:
  • number
Default Value:
  • 200
Source:
src/input/Input.js line 130

justReleasedRate : number

The number of milliseconds below which the Pointer is considered justReleased .

Type:
  • number
Default Value:
  • 200
Source:
src/input/Input.js line 136

keyboard : Phaser.Keyboard

The Keyboard Input manager.

Type:
Source:
src/input/Input.js line 263

maxPointers : integer

The maximum number of Pointers allowed to be active at any one time. A value of -1 is only limited by the total number of pointers (MAX_POINTERS). For lots of games it's useful to set this to 1. At least 2 Pointers will always be created, unless MAX_POINTERS is smaller.

Type:
  • integer
Default Value:
  • -1 (Limited by total pointers.)
Source:
src/input/Input.js line 106
See:

minPriorityID : number

You can tell all Pointers to ignore any Game Object with a priorityID lower than this value. This is useful when stacking UI layers. Set to zero to disable.

Type:
  • number
Source:
src/input/Input.js line 349

mouse : Phaser.Mouse

The Mouse Input manager.

You should not usually access this manager directly, but instead use Input.mousePointer or Input.activePointer which normalizes all the input values for you, regardless of browser.

Type:
Source:
src/input/Input.js line 256

mousePointer : Pointer

The mouse has its own unique Phaser.Pointer object which you can use if making a desktop specific game.

The mouse pointer is updated by Phaser.Input#mouse and Phaser.Input#mspointer.

Type:
  • Pointer
Source:
src/input/Input.js line 246

<protected> moveCallbacks : array

An array of callbacks that will be fired every time the activePointer receives a move event from the DOM. To add a callback to this array please use Input.addMoveCallback.

Type:
  • array
Source:
src/input/Input.js line 40

mspointer : Phaser.MSPointer

The MSPointer Input manager.

You should not usually access this manager directly, but instead use Input.activePointer which normalizes all the input values for you, regardless of browser.

Type:
Source:
src/input/Input.js line 283

multiInputOverride : number

Controls the expected behavior when using a mouse and touch together on a multi-input device.

Type:
  • number
Source:
src/input/Input.js line 73

onDown : Phaser.Signal

A Signal that is dispatched each time a pointer is pressed down. It is sent two arguments:

  • {Phaser.Pointer} The pointer that caused the event.
  • {Event} The original DOM event.
Type:
Source:
src/input/Input.js line 309

onHold : Phaser.Signal

A Signal that is dispatched each time a pointer is held down. It is sent one argument:

  • {Phaser.Pointer} The pointer that caused the event.
Type:
Source:
src/input/Input.js line 341

onTap : Phaser.Signal

A Signal that is dispatched each time a pointer is tapped. It is sent two arguments:

  • {Phaser.Pointer} The pointer that caused the event.
  • {boolean} True if this was a double tap.
Type:
Source:
src/input/Input.js line 331

onUp : Phaser.Signal

A Signal that is dispatched each time a pointer is released. It is sent two arguments:

  • {Phaser.Pointer} The pointer that caused the event.
  • {Event} The original DOM event.
Type:
Source:
src/input/Input.js line 320

pointer1 : Phaser.Pointer

A Pointer object.

Type:
Source:
src/input/Input.js line 173

pointer2 : Phaser.Pointer

A Pointer object.

Type:
Source:
src/input/Input.js line 178

pointer3 : Phaser.Pointer

A Pointer object.

Type:
Source:
src/input/Input.js line 183

pointer4 : Phaser.Pointer

A Pointer object.

Type:
Source:
src/input/Input.js line 188

pointer5 : Phaser.Pointer

A Pointer object.

Type:
Source:
src/input/Input.js line 193

pointer6 : Phaser.Pointer

A Pointer object.

Type:
Source:
src/input/Input.js line 198

pointer7 : Phaser.Pointer

A Pointer object.

Type:
Source:
src/input/Input.js line 203

pointer8 : Phaser.Pointer

A Pointer object.

Type:
Source:
src/input/Input.js line 208

pointer9 : Phaser.Pointer

A Pointer object.

Type:
Source:
src/input/Input.js line 213

pointer10 : Phaser.Pointer

A Pointer object.

Type:
Source:
src/input/Input.js line 218

<readonly> pointers : Array.<Phaser.Pointer>

A pool of non-mouse (contact) pointers that have been added to the game. They're activated and updated by Phaser.Input#mspointer and Phaser.Input#touch. The properties pointer1..10 are aliases for pointers[0..9].

Type:
Source:
src/input/Input.js line 228

<readonly> pollLocked : boolean

True if the Input is currently poll rate locked.

Type:
  • boolean
Source:
src/input/Input.js line 1248

pollRate : number

How often should the input pointers be checked for updates? A value of 0 means every single frame (60fps); a value of 1 means every other frame (30fps) and so on.

Type:
  • number
Source:
src/input/Input.js line 58

position : Phaser.Point

A point object representing the current position of the Pointer.

Type:
Source:
src/input/Input.js line 79

recordLimit : number

The total number of entries that can be recorded into the Pointer objects tracking history. If the Pointer is tracking one event every 100ms; then a trackLimit of 100 would store the last 10 seconds worth of history.

Type:
  • number
Default Value:
  • 100
Source:
src/input/Input.js line 159

recordPointerHistory : boolean

Sets if the Pointer objects should record a history of x/y coordinates they have passed through. The history is cleared each time the Pointer is pressed down. The history is updated at the rate specified in Input.pollRate

Type:
  • boolean
Source:
src/input/Input.js line 145

recordRate : number

The rate in milliseconds at which the Pointer objects should update their tracking history.

Type:
  • number
Default Value:
  • 100
Source:
src/input/Input.js line 151

resetLocked : boolean

If the Input Manager has been reset locked then all calls made to InputManager.reset, such as from a State change, are ignored.

Type:
  • boolean
Source:
src/input/Input.js line 298

scale : Phaser.Point

The scale by which all input coordinates are multiplied; calculated by the ScaleManager. In an un-scaled game the values will be x = 1 and y = 1.

Type:
Source:
src/input/Input.js line 96

speed : Phaser.Point

A point object representing the speed of the Pointer. Only really useful in single Pointer games; otherwise see the Pointer objects directly.

Type:
Source:
src/input/Input.js line 84

tapRate : number

The number of milliseconds that the Pointer has to be pressed down and then released to be considered a tap or click.

Type:
  • number
Default Value:
  • 200
Source:
src/input/Input.js line 112

<readonly> totalActivePointers : integers

The total number of active Pointers, not counting the mouse pointer.

Type:
  • integers
Source:
src/input/Input.js line 1278

<readonly> totalInactivePointers : number

The total number of inactive Pointers.

Type:
  • number
Source:
src/input/Input.js line 1263

touch : Phaser.Touch

The Touch Input manager.

You should not usually access this manager directly, but instead use Input.activePointer which normalizes all the input values for you, regardless of browser.

Type:
Source:
src/input/Input.js line 273

<protected> touchLockCallbacks : array

An array of callbacks that will be fired every time a native touch start or touch end event (pointerdown or pointerup when using Pointer events) is received from the browser. This is used internally to handle audio and video unlocking on mobile devices. To add a callback to this array please use Input.addTouchLockCallback.

Type:
  • array
Source:
src/input/Input.js line 168

<readonly> worldX : number

The world X coordinate of the most recently active pointer.

Type:
  • number
Source:
src/input/Input.js line 1293

<readonly> worldY : number

The world Y coordinate of the most recently active pointer.

Type:
  • number
Source:
src/input/Input.js line 1308

x : number

The X coordinate of the most recently active pointer. This value takes game scaling into account automatically. See Pointer.screenX/clientX for source values.

Type:
  • number
Source:
src/input/Input.js line 1208

y : number

The Y coordinate of the most recently active pointer. This value takes game scaling into account automatically. See Pointer.screenY/clientY for source values.

Type:
  • number
Source:
src/input/Input.js line 1228

Methods

addMoveCallback(callback, context)

Adds a callback that is fired every time the activePointer receives a DOM move event such as a mousemove or touchmove.

The callback will be sent 5 parameters:

  • A reference to the Phaser.Pointer object that moved
  • The x position of the pointer
  • The y position
  • A boolean indicating if the movement was the result of a 'click' event (such as a mouse click or touch down)
  • The DOM move event

It will be called every time the activePointer moves, which in a multi-touch game can be a lot of times, so this is best to only use if you've limited input to a single pointer (i.e. mouse or touch).

The callback is added to the Phaser.Input.moveCallbacks array and should be removed with Phaser.Input.deleteMoveCallback.

Parameters:
Name Type Description
callback function

The callback that will be called each time the activePointer receives a DOM move event.

context object

The context in which the callback will be called.

Source:
src/input/Input.js line 576

addPointer()

Add a new Pointer object to the Input Manager. By default Input creates 3 pointer objects: mousePointer (not include in part of general pointer pool), pointer1 and pointer2. This method adds an additional pointer, up to a maximum of Phaser.Input.MAX_POINTERS (default of 10).

Source:
src/input/Input.js line 690
Returns:

The new Pointer object that was created; null if a new pointer could not be added.

Type
Phaser.Pointer | null

addTouchLockCallback(callback, context [, onEnd])

Adds a callback that is fired when a browser touchstart/pointerdown or touchend/pointerup event is received.

This is used internally to handle audio and video unlocking on mobile devices.

If the callback returns 'true' then the callback is automatically deleted once invoked.

The callback is added to the touchLockCallbacks array and should be removed with removeTouchLockCallback.

Parameters:
Name Type Argument Default Description
callback function

The callback that will be called when a touchstart event is received.

context object

The context in which the callback will be called.

onEnd boolean <optional>
false

Will the callback fire on a touchstart/pointerdown (default) or touchend/pointerup event?

Source:
src/input/Input.js line 601

<protected> boot(config)

Starts the Input Manager running.

Parameters:
Name Type Description
config InputConfig
Source:
src/input/Input.js line 426

deleteMoveCallback(callback, context)

Removes the callback from the Phaser.Input.moveCallbacks array.

Parameters:
Name Type Description
callback function

The callback to be removed.

context object

The context in which the callback exists.

Source:
src/input/Input.js line 669

destroy()

Stops all of the Input Managers from running.

Source:
src/input/Input.js line 518

getLocalPosition(displayObject, pointer)

This will return the local coordinates of the specified displayObject based on the given Pointer.

Parameters:
Name Type Description
displayObject Phaser.Sprite | Phaser.Image

The DisplayObject to get the local coordinates for.

pointer Phaser.Pointer

The Pointer to use in the check against the displayObject.

Source:
src/input/Input.js line 1041
Returns:

A point containing the coordinates of the Pointer position relative to the DisplayObject.

Type
Phaser.Point

getPointer( [isActive])

Get the first Pointer with the given active state.

Parameters:
Name Type Argument Default Description
isActive boolean <optional>
false

The state the Pointer should be in - active or inactive?

Source:
src/input/Input.js line 966
Returns:

A Pointer object or null if no Pointer object matches the requested state.

Type
Phaser.Pointer

getPointerFromId(pointerId)

Get the Pointer object whos pointerId property matches the given value.

The pointerId property is not set until the Pointer has been used at least once, as its populated by the DOM event. Also it can change every time you press the pointer down if the browser recycles it.

Parameters:
Name Type Description
pointerId number

The pointerId (not 'id') value to search for.

Source:
src/input/Input.js line 1016
Returns:

A Pointer object or null if no Pointer object matches the requested identifier.

Type
Phaser.Pointer

getPointerFromIdentifier(identifier)

Get the Pointer object whos identifier property matches the given identifier value.

The identifier property is not set until the Pointer has been used at least once, as its populated by the DOM event. Also it can change every time you press the pointer down, and is not fixed once set. Note: Not all browsers set the identifier property and it's not part of the W3C spec, so you may need getPointerFromId instead.

Parameters:
Name Type Description
identifier number

The Pointer.identifier value to search for.

Source:
src/input/Input.js line 990
Returns:

A Pointer object or null if no Pointer object matches the requested identifier.

Type
Phaser.Pointer

hitTest(displayObject, pointer, localPoint)

Tests if the pointer hits the given object.

Parameters:
Name Type Description
displayObject DisplayObject

The displayObject to test for a hit.

pointer Phaser.Pointer

The pointer to use for the test.

localPoint Phaser.Point

The local translated point.

Source:
src/input/Input.js line 1062

removeTouchLockCallback(callback, context)

Removes the callback at the defined index from the touchLockCallbacks array

Parameters:
Name Type Description
callback function

The callback to be removed.

context object

The context in which the callback exists.

Source:
src/input/Input.js line 622
Returns:

True if the callback was deleted, otherwise false.

Type
boolean

reset( [hard])

Reset all of the Pointers and Input states.

The optional hard parameter will reset any events or callbacks that may be bound. Input.reset is called automatically during a State change or if a game loses focus / visibility. To control control the reset manually set Phaser.InputManager.resetLocked to true.

Parameters:
Name Type Argument Default Description
hard boolean <optional>
false

A soft reset won't reset any events or callbacks that are bound. A hard reset will.

Source:
src/input/Input.js line 767

resetSpeed(x, y)

Resets the speed and old position properties.

Parameters:
Name Type Description
x number

Sets the oldPosition.x value.

y number

Sets the oldPosition.y value.

Source:
src/input/Input.js line 820

setInteractiveCandidateHandler(callback, context)

Adds a callback that is fired every time Pointer.processInteractiveObjects is called. The purpose of processInteractiveObjects is to work out which Game Object the Pointer is going to interact with. It works by polling all of the valid game objects, and then slowly discounting those that don't meet the criteria (i.e. they aren't under the Pointer, are disabled, invisible, etc).

Eventually a short-list of 'candidates' is created. These are all of the Game Objects which are valid for input and overlap with the Pointer. If you need fine-grained control over which of the items is selected then you can use this callback to do so.

The callback will be sent 3 parameters:

  1. A reference to the Phaser.Pointer object that is processing the Items.
  2. An array containing all potential interactive candidates. This is an array of InputHandler objects, not Sprites.
  3. The current 'favorite' candidate, based on its priorityID and position in the display list.

Your callback MUST return one of the candidates sent to it.

Parameters:
Name Type Description
callback function

The callback that will be called each time Pointer.processInteractiveObjects is called. Set to null to disable.

context object

The context in which the callback will be called.

Source:
src/input/Input.js line 548

<protected> startPointer(event)

Find the first free Pointer object and start it, passing in the event data. This is called automatically by Phaser.Touch and Phaser.MSPointer.

Parameters:
Name Type Description
event any

The event data from the Touch event.

Source:
src/input/Input.js line 833
Returns:

The Pointer object that was started or null if no Pointer object is available.

Type
Phaser.Pointer

<protected> stopPointer(event)

Stops the matching Pointer object, passing in the event data.

Parameters:
Name Type Description
event any

The event data from the Touch event.

Source:
src/input/Input.js line 906
Returns:

The Pointer object that was stopped or null if no Pointer object is available.

Type
Phaser.Pointer

<protected> update()

Updates the Input Manager. Called by the core Game loop.

Source:
src/input/Input.js line 715

<protected> updatePointer(event)

Updates the matching Pointer object, passing in the event data. This is called automatically and should not normally need to be invoked.

Parameters:
Name Type Description
event any

The event data from the Touch event.

Source:
src/input/Input.js line 872
Returns:

The Pointer object that was updated; null if no pointer was updated.

Type
Phaser.Pointer

phaser-ce@2.20.1 is on GitHub and NPM

Documentation generated by JSDoc 3.6.11 on 2024-10-05 using Tomorrow.