Phaser API Documentation

  Version: 
Filter

The Input Manager is responsible for handling the pointer related systems in a single Phaser Game instance.

Based on the Game Config it will create handlers for mouse and touch support.

Keyboard and Gamepad are plugins, handled directly by the InputPlugin class.

It then manages the events, pointer creation and general hit test related operations.

You rarely need to interact with the Input Manager directly, and as such, all of its properties and methods should be considered private. Instead, you should use the Input Plugin, which is a Scene level system, responsible for dealing with all input events for a Scene.

Constructor:

new InputManager(game, config)

Parameters:

name type description
game Phaser.Game

The Game instance that owns the Input Manager.

config object

The Input Configuration object, as set in the Game Config.

Since: 3.0.0
Source: src/input/InputManager.js (Line 19)

Members

<private> _tempHitTest: array
Focus
Focus

Description:

A re-cycled array to store hit results in.

Type:
array
Default: []
Since: 3.0.0
Focus
Focus

Description:

A re-cycled matrix used in hit test calculations.

Type:
Since: 3.4.0
Focus
Focus

Description:

A re-cycled matrix used in hit test calculations.

Type:
Since: 3.12.0
Focus
Focus
<private> _tempPoint: Object
Focus
Focus

Description:

A re-cycled point-like object to store hit test values in.

Type:
Object
Since: 3.0.0
Focus
Focus
<private> _tempSkip: boolean
Focus
Focus

Description:

An internal private var that records Scenes aborting event processing.

Type:
boolean
Since: 3.18.0
Focus
Focus
activePointer: Phaser.Input.Pointer
Focus
Focus

Description:

The most recently active Pointer object.

If you've only 1 Pointer in your game then this will accurately be either the first finger touched, or the mouse.

If your game doesn't need to support multi-touch then you can safely use this property in all of your game code and it will adapt to be either the mouse or the touch, based on device.

Type:
Since: 3.0.0
Focus
Focus
canvas: HTMLCanvasElement
Focus
Focus

Description:

The Canvas that is used for all DOM event input listeners.

Type:
HTMLCanvasElement
Since: 3.0.0
Source: src/input/InputManager.js (Line 68)
Focus
Focus

Description:

The Game Configuration object, as set during the game boot.

Type:
Since: 3.0.0
Source: src/input/InputManager.js (Line 77)
Focus
Focus
defaultCursor: string
Focus
Focus

Description:

The default CSS cursor to be used when interacting with your game.

See the setDefaultCursor method for more details.

Type:
string
Since: 3.10.0
Focus
Focus
enabled: boolean
Focus
Focus

Description:

If set, the Input Manager will run its update loop every frame.

Type:
boolean
Default: true
Since: 3.0.0
Source: src/input/InputManager.js (Line 86)
Focus
Focus

Description:

The Event Emitter instance that the Input Manager uses to emit events from.

Type:
Since: 3.0.0
Source: src/input/InputManager.js (Line 96)
Focus
Focus
<readonly> game: Phaser.Game
Focus
Focus

Description:

The Game instance that owns the Input Manager. A Game only maintains on instance of the Input Manager at any time.

Type:
Since: 3.0.0
Source: src/input/InputManager.js (Line 47)
Focus
Focus
globalTopOnly: boolean
Focus
Focus

Description:

If the top-most Scene in the Scene List receives an input it will stop input from propagating any lower down the scene list, i.e. if you have a UI Scene at the top and click something on it, that click will not then be passed down to any other Scene below. Disable this to have input events passed through all Scenes, all the time.

Type:
boolean
Default: true
Since: 3.0.0
Focus
Focus
<readonly> isOver: boolean
Focus
Focus

Description:

Are any mouse or touch pointers currently over the game canvas? This is updated automatically by the canvas over and out handlers.

Type:
boolean
Since: 3.16.0
Focus
Focus

Description:

A reference to the Keyboard Manager class, if enabled via the input.keyboard Game Config property.

Type:
Since: 3.16.0
Focus
Focus

Description:

A reference to the Mouse Manager class, if enabled via the input.mouse Game Config property.

Type:
Since: 3.0.0
Focus
Focus
<nullable> mousePointer: Phaser.Input.Pointer
Focus
Focus

Description:

The mouse has its own unique Pointer object, which you can reference directly if making a desktop specific game. If you are supporting both desktop and touch devices then do not use this property, instead use activePointer which will always map to the most recently interacted pointer.

Type:
Since: 3.10.0
Focus
Focus
<private> mousePointerContainer: Array.<Phaser.Input.Pointer>
Focus
Focus

Description:

An internal private array that avoids needing to create a new array on every DOM mouse event.

Type:
Since: 3.18.0
Focus
Focus
pointers: Array.<Phaser.Input.Pointer>
Focus
Focus

Description:

An array of Pointers that have been added to the game. The first entry is reserved for the Mouse Pointer, the rest are Touch Pointers.

By default there is 1 touch pointer enabled. If you need more use the addPointer method to start them, or set the input.activePointers property in the Game Config.

Type:
Since: 3.10.0
Focus
Focus
<readonly> pointersTotal: number
Focus
Focus

Description:

The number of touch objects activated and being processed each update.

You can change this by either calling addPointer at run-time, or by setting the input.activePointers property in the Game Config.

Type:
number
Since: 3.10.0
Focus
Focus

Description:

A reference to the global Game Scale Manager. Used for all bounds checks and pointer scaling.

Type:
Since: 3.16.0
Source: src/input/InputManager.js (Line 58)
Focus
Focus
<readonly> time: number
Focus
Focus

Description:

The time this Input Manager was last updated. This value is populated by the Game Step each frame.

Type:
number
Since: 3.16.2
Focus
Focus

Description:

A reference to the Touch Manager class, if enabled via the input.touch Game Config property.

Type:
Since: 3.0.0
Focus
Focus

Methods

addPointer([quantity])
Focus
Focus

Description:

Adds new Pointer objects to the Input Manager.

By default Phaser creates 2 pointer objects: mousePointer and pointer1.

You can create more either by calling this method, or by setting the input.activePointers property in the Game Config, up to a maximum of 10 pointers.

The first 10 pointers are available via the InputPlugin.pointerX properties, once they have been added via this method.

Parameters:

name type arguments Default description
quantity number <optional> 1

The number of new Pointers to create. A maximum of 10 is allowed in total.

Returns:
Description:

An array containing all of the new Pointer objects that were created.

Type:
Since: 3.10.0
Focus
Focus
<protected> boot()
Focus
Focus

Description:

The Boot handler is called by Phaser.Game when it first starts up. The renderer is available by now.

Since: 3.0.0
Focus
Focus
destroy()
Focus
Focus

Description:

Destroys the Input Manager and all of its systems.

There is no way to recover from doing this.

Since: 3.0.0
Focus
Focus
hitTest(pointer, gameObjects, camera, [output])
Focus
Focus

Description:

Performs a hit test using the given Pointer and camera, against an array of interactive Game Objects.

The Game Objects are culled against the camera, and then the coordinates are translated into the local camera space and used to determine if they fall within the remaining Game Objects hit areas or not.

If nothing is matched an empty array is returned.

This method is called automatically by InputPlugin.hitTestPointer and doesn't usually need to be invoked directly.

Parameters:

name type arguments description
pointer Phaser.Input.Pointer

The Pointer to test against.

gameObjects array

An array of interactive Game Objects to check.

camera Phaser.Cameras.Scene2D.Camera

The Camera which is being tested against.

output array <optional>

An array to store the results in. If not given, a new empty array is created.

Returns:
Description:

An array of the Game Objects that were hit during this hit test.

Type:
  • array
Since: 3.0.0
Focus
Focus
<private> inputCandidate(gameObject, camera)
Focus
Focus

Description:

Checks if the given Game Object should be considered as a candidate for input or not.

Checks if the Game Object has an input component that is enabled, that it will render, and finally, if it has a parent, that the parent parent, or any ancestor, is visible or not.

Parameters:

name type description
gameObject Phaser.GameObjects.GameObject

The Game Object to test.

camera Phaser.Cameras.Scene2D.Camera

The Camera which is being tested against.

Returns:
Description:

true if the Game Object should be considered for input, otherwise false.

Type:
  • boolean
Since: 3.10.0
Focus
Focus
<private> onMouseDown(event)
Focus
Focus

Description:

Processes a mouse down event, as passed in by the MouseManager.

Parameters:

name type description
event MouseEvent

The native DOM Mouse event.

Since: 3.18.0
Focus
Focus
<private> onMouseMove(event)
Focus
Focus

Description:

Processes a mouse move event, as passed in by the MouseManager.

Parameters:

name type description
event MouseEvent

The native DOM Mouse event.

Since: 3.18.0
Focus
Focus
<private> onMouseUp(event)
Focus
Focus

Description:

Processes a mouse up event, as passed in by the MouseManager.

Parameters:

name type description
event MouseEvent

The native DOM Mouse event.

Since: 3.18.0
Focus
Focus
<private> onMouseWheel(event)
Focus
Focus

Description:

Processes a mouse wheel event, as passed in by the MouseManager.

Parameters:

name type description
event WheelEvent

The native DOM Wheel event.

Since: 3.18.0
Focus
Focus
<private> onPointerLockChange(event)
Focus
Focus

Description:

Processes a pointer lock change event, as passed in by the MouseManager.

Parameters:

name type description
event MouseEvent

The native DOM Mouse event.

Since: 3.19.0
Focus
Focus
<private> onTouchCancel(event)
Focus
Focus

Description:

Processes a touch cancel event, as passed in by the TouchManager.

Parameters:

name type description
event TouchEvent

The native DOM Touch event.

Since: 3.18.0
Focus
Focus
<private> onTouchEnd(event)
Focus
Focus

Description:

Processes a touch end event, as passed in by the TouchManager.

Parameters:

name type description
event TouchEvent

The native DOM Touch event.

Since: 3.18.0
Focus
Focus
<private> onTouchMove(event)
Focus
Focus

Description:

Processes a touch move event, as passed in by the TouchManager.

Parameters:

name type description
event TouchEvent

The native DOM Touch event.

Since: 3.18.0
Focus
Focus
<private> onTouchStart(event)
Focus
Focus

Description:

Processes a touch start event, as passed in by the TouchManager.

Parameters:

name type description
event TouchEvent

The native DOM Touch event.

Since: 3.18.0
Focus
Focus
pointWithinHitArea(gameObject, x, y)
Focus
Focus

Description:

Checks if the given x and y coordinate are within the hit area of the Game Object.

This method assumes that the coordinate values have already been translated into the space of the Game Object.

If the coordinates are within the hit area they are set into the Game Objects Input localX and localY properties.

Parameters:

name type description
gameObject Phaser.GameObjects.GameObject

The interactive Game Object to check against.

x number

The translated x coordinate for the hit test.

y number

The translated y coordinate for the hit test.

Returns:
Description:

true if the coordinates were inside the Game Objects hit area, otherwise false.

Type:
  • boolean
Since: 3.0.0
Focus
Focus
pointWithinInteractiveObject(object, x, y)
Focus
Focus

Description:

Checks if the given x and y coordinate are within the hit area of the Interactive Object.

This method assumes that the coordinate values have already been translated into the space of the Interactive Object.

If the coordinates are within the hit area they are set into the Interactive Objects Input localX and localY properties.

Parameters:

name type description
object Phaser.Types.Input.InteractiveObject

The Interactive Object to check against.

x number

The translated x coordinate for the hit test.

y number

The translated y coordinate for the hit test.

Returns:
Description:

true if the coordinates were inside the Game Objects hit area, otherwise false.

Type:
  • boolean
Since: 3.0.0
Focus
Focus
<private> preRender()
Focus
Focus

Description:

Internal update, called automatically by the Game Step right at the start.

Since: 3.18.0
Focus
Focus
<private> resetCursor(interactiveObject)
Focus
Focus

Description:

Called by the InputPlugin when processing over and out events.

Tells the Input Manager to clear the hand cursor, if set, during its postUpdate step.

Parameters:

name type description
interactiveObject Phaser.Types.Input.InteractiveObject

The Interactive Object that called this method.

Since: 3.10.0
Focus
Focus
<private> setCanvasOut(event)
Focus
Focus

Description:

Internal canvas state change, called automatically by the Mouse Manager.

Parameters:

name type description
event MouseEvent | TouchEvent

The DOM Event.

Since: 3.16.0
Focus
Focus
<private> setCanvasOver(event)
Focus
Focus

Description:

Internal canvas state change, called automatically by the Mouse Manager.

Parameters:

name type description
event MouseEvent | TouchEvent

The DOM Event.

Since: 3.16.0
Focus
Focus
<private> setCursor(interactiveObject)
Focus
Focus

Description:

Called by the InputPlugin when processing over and out events.

Tells the Input Manager to set a custom cursor during its postUpdate step.

https://developer.mozilla.org/en-US/docs/Web/CSS/cursor

Parameters:

name type description
interactiveObject Phaser.Types.Input.InteractiveObject

The Interactive Object that called this method.

Since: 3.10.0
Focus
Focus
setDefaultCursor(cursor)
Focus
Focus

Description:

Tells the Input system to set a custom cursor.

This cursor will be the default cursor used when interacting with the game canvas.

If an Interactive Object also sets a custom cursor, this is the cursor that is reset after its use.

Any valid CSS cursor value is allowed, including paths to image files, i.e.:

this.input.setDefaultCursor('url(assets/cursors/sword.cur), pointer');

Please read about the differences between browsers when it comes to the file formats and sizes they support:

https://developer.mozilla.org/en-US/docs/Web/CSS/cursor https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_User_Interface/Using_URL_values_for_the_cursor_property

It's up to you to pick a suitable cursor format that works across the range of browsers you need to support.

Parameters:

name type description
cursor string

The CSS to be used when setting the default cursor.

Since: 3.10.0
Focus
Focus
transformPointer(pointer, pageX, pageY, wasMove)
Focus
Focus

Description:

Transforms the pageX and pageY values of a Pointer into the scaled coordinate space of the Input Manager.

Parameters:

name type description
pointer Phaser.Input.Pointer

The Pointer to transform the values for.

pageX number

The Page X value.

pageY number

The Page Y value.

wasMove boolean

Are we transforming the Pointer from a move event, or an up / down event?

Since: 3.10.0
Focus
Focus
updateInputPlugins(type, pointers)
Focus
Focus

Description:

Internal method that gets a list of all the active Input Plugins in the game and updates each of them in turn, in reverse order (top to bottom), to allow for DOM top-level event handling simulation.

Parameters:

name type description
type number

The type of event to process.

pointers Array.<Phaser.Input.Pointer>

An array of Pointers on which the event occurred.

Since: 3.16.0
Focus
Focus