[PhaserJS] 鼠標(biāo)事件
鼠標(biāo)事件
要想監(jiān)聽事件,需要先調(diào)用GameObject的setInteractive方法,該方法會(huì)將GameObject傳給輸入管理器,以便可以響應(yīng)輸入。
GameObject繼承自EventEmitter(eventemitter3),因此具體標(biāo)準(zhǔn)的事件處理方法。
事件管理
| 事件方法 | 描述 |
|---|---|
| on(event, fn [, context]) | 新增事件 |
| off(event [, fn] [, context] [, once]) | 取消事件 |
| once(event, fn [, context]) | 增加一個(gè)單次事件 |
| emit(event [, args]) | 觸發(fā)事件 |
| addListener(event, fn [, context]) | 新增事件 |
| removeListener(event [, fn] [, context] [, once]) | 取消單個(gè)事件 |
| removeAllListeners( [event]) | 取消所有事件 |
鼠標(biāo)事件列表
| 事件名 | 描述 |
|---|---|
| pointerdown | 鼠標(biāo)按下 |
| pointerup | 鼠標(biāo)抬起 |
| pointermove | 鼠標(biāo)在游戲?qū)ο笊弦苿?dòng) |
| pointerout | 鼠標(biāo)移出游戲?qū)ο?/td> |
| pointerover | 鼠標(biāo)移入游戲?qū)ο?/td> |
| wheel | 鼠標(biāo)在游戲?qū)ο笊匣瑒?dòng)滾輪 |
簡(jiǎn)單示例
const star = this.add.star(250, 150, 5, 32, 64, 0xff0000, 1);
star.setInteractive();
star.on('pointerup', () => {
console.log('鼠標(biāo)抬起事件');
});
完整示例
class SceneA extends Phaser.Scene {
constructor() {
super({
key: 'SceneA'
});
}
create() {
const star = this.add.star(250, 150, 5, 32, 64, 0xff0000, 1);
const text = this.add.text(160, 140, '狀態(tài)', {
fixedWidth: 180,
align: 'center'
});
star.setInteractive();
star.on('pointerdown', () => {
text.setText('鼠標(biāo)按下');
}, this);
star.on('pointerup', () => {
text.setText('鼠標(biāo)抬起');
}, this);
star.on('pointermove', () => {
text.setText('鼠標(biāo)移動(dòng)' + Math.floor(Math.random() * 100));
}, this);
star.on('pointerout', () => {
text.setText('鼠標(biāo)移出');
}, this);
star.on('pointerover', () => {
text.setText('鼠標(biāo)移入');
}, this);
star.on('wheel', () => {
text.setText('滾輪滾動(dòng)' + Math.floor(Math.random() * 100));
}, this);
}
}
new Phaser.Game({
width: 500,
height: 300,
scene: [SceneA]
});
在線體驗(yàn)
posted on 2022-05-31 14:18 綠島之北 閱讀(262) 評(píng)論(0) 收藏 舉報(bào)
浙公網(wǎng)安備 33010602011771號(hào)