phaser/src/input/gamepad/Button.js

52 lines
1.1 KiB
JavaScript
Raw Normal View History

2017-09-09 02:17:13 +00:00
// Phaser.Input.Gamepad.Button
var Class = require('../../utils/Class');
var GamepadEvent = require('./events/');
var Button = new Class({
initialize:
function Button (pad, index)
{
this.pad = pad;
this.events = pad.events;
this.index = index;
// Between 0 and 1
2017-09-09 02:17:13 +00:00
this.value = 0;
// Can be set for Analogue buttons to enable a 'pressure' threshold before considered as 'pressed'
this.threshold = 0;
this.pressed = false;
2017-09-09 02:17:13 +00:00
},
update: function (data)
{
this.value = data.value;
if (this.value >= this.threshold)
2017-09-09 02:17:13 +00:00
{
if (!this.pressed)
{
this.pressed = true;
this.events.dispatch(new GamepadEvent.DOWN(this.pad, this, this.value, data));
}
}
else
{
if (this.pressed)
{
this.pressed = false;
this.events.dispatch(new GamepadEvent.UP(this.pad, this, this.value, data));
}
}
}
});
module.exports = Button;