phaser
Version:
A fast, free and fun HTML5 Game Framework for Desktop and Mobile web browsers from the team at Phaser Studio Inc.
124 lines (110 loc) • 3.45 kB
JavaScript
/**
* @author Richard Davey <rich@phaser.io>
* @copyright 2013-2026 Phaser Studio Inc.
* @license {@link https://opensource.org/licenses/MIT|MIT License}
*/
var Class = require('../../utils/Class');
/**
* @classdesc
* Represents a single axis on a Gamepad controller, such as one direction of an analog stick.
* Each axis has a `value` property ranging from -1 to 1 (with 0 as dead center), and a
* configurable `threshold` below which the value is treated as zero by `getValue()`. Axis
* objects are created automatically by the Gamepad as they are needed.
*
* @class Axis
* @memberof Phaser.Input.Gamepad
* @constructor
* @since 3.0.0
*
* @param {Phaser.Input.Gamepad.Gamepad} pad - A reference to the Gamepad that this Axis belongs to.
* @param {number} index - The index of this Axis.
*/
var Axis = new Class({
initialize:
function Axis (pad, index)
{
/**
* A reference to the Gamepad that this Axis belongs to.
*
* @name Phaser.Input.Gamepad.Axis#pad
* @type {Phaser.Input.Gamepad.Gamepad}
* @since 3.0.0
*/
this.pad = pad;
/**
* An event emitter to use to emit the axis events.
*
* @name Phaser.Input.Gamepad.Axis#events
* @type {Phaser.Events.EventEmitter}
* @since 3.0.0
*/
this.events = pad.events;
/**
* The index of this Axis.
*
* @name Phaser.Input.Gamepad.Axis#index
* @type {number}
* @since 3.0.0
*/
this.index = index;
/**
* The raw axis value, between -1 and 1 with 0 being dead center.
* Use the method `getValue` to get a normalized value with the threshold applied.
*
* @name Phaser.Input.Gamepad.Axis#value
* @type {number}
* @default 0
* @since 3.0.0
*/
this.value = 0;
/**
* Movement tolerance threshold below which axis values are ignored in `getValue`.
*
* @name Phaser.Input.Gamepad.Axis#threshold
* @type {number}
* @default 0.1
* @since 3.0.0
*/
this.threshold = 0.1;
},
/**
* Internal update handler for this Axis.
* Called automatically by the Gamepad as part of its update.
*
* @method Phaser.Input.Gamepad.Axis#update
* @private
* @since 3.0.0
*
* @param {number} value - The value of the axis movement.
*/
update: function (value)
{
this.value = value;
},
/**
* Returns the axis value after applying the dead zone threshold. If the absolute value
* of the axis is less than `threshold`, zero is returned instead, preventing minor stick
* drift from registering as intentional input. Otherwise the raw `value` is returned.
*
* @method Phaser.Input.Gamepad.Axis#getValue
* @since 3.0.0
*
* @return {number} The axis value, adjusted for the movement threshold.
*/
getValue: function ()
{
return (Math.abs(this.value) < this.threshold) ? 0 : this.value;
},
/**
* Destroys this Axis instance and releases external references it holds.
*
* @method Phaser.Input.Gamepad.Axis#destroy
* @since 3.10.0
*/
destroy: function ()
{
this.pad = null;
this.events = null;
}
});
module.exports = Axis;