您首先需要启用 Sprite 进行输入:
sprite.inputEnabled = true;
然后,您可以监听 Sprite 在输入时调度的任何事件,例如:
sprite.events.onInputDown.add(onDown, this);
...
function onDown(sprite, pointer) {
// do something wonderful here
}
回调发送 2 个参数:导致输入事件的 Sprite 和指针(在多输入系统中,这可能经常变化)
指针有很多你可以访问的属性,例如它被放下的时间、移动历史等。有关详细信息,请参阅指针文档。
Sprite 有很多事件,但这些是与输入相关的事件(这是直接从 Phaser 源代码中提取的):
/**
* @property {Phaser.Signal} onInputOver - This signal is dispatched if the parent is inputEnabled and receives an over event from a Pointer.
* @default null
*/
this.onInputOver = null;
/**
* @property {Phaser.Signal} onInputOut - This signal is dispatched if the parent is inputEnabled and receives an out event from a Pointer.
* @default null
*/
this.onInputOut = null;
/**
* @property {Phaser.Signal} onInputDown - This signal is dispatched if the parent is inputEnabled and receives a down event from a Pointer.
* @default null
*/
this.onInputDown = null;
/**
* @property {Phaser.Signal} onInputUp - This signal is dispatched if the parent is inputEnabled and receives an up event from a Pointer.
* @default null
*/
this.onInputUp = null;
/**
* @property {Phaser.Signal} onDragStart - This signal is dispatched if the parent is inputEnabled and receives a drag start event from a Pointer.
* @default null
*/
this.onDragStart = null;
/**
* @property {Phaser.Signal} onDragStop - This signal is dispatched if the parent is inputEnabled and receives a drag stop event from a Pointer.
* @default null
*/
this.onDragStop = null;