我正在使用Phaser.io
我想用A
,S
,D
,W
键。我知道您可以使用如下箭头键:
create(){
...
gameState.cursors = this.input.keyboard.createCursorKeys();
}
update(){
if (gameState.cursors.left.isDown) {
gameState.player1.setVelocityX(-160);
} else if (gameState.cursors.right.isDown) {
gameState.player1.setVelocityX(160);
}
}
我试图切换.left.
为.A.
与.right.
对.D.
,但它不工作。
有任何想法吗?
我找到了解决方案:
let keyA;
let keyS;
let keyD;
let keyW;
create()
函数中,将键添加到相应的变量中:keyA = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.A);
keyS = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.S);
keyD = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.D);
keyW = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.W);
update()
函数中,添加以下代码片段:if(keyA.isDown) {
console.log('A key pressed')
} else if(keyS.isDown) {
console.log('S key pressed')
} else if(keyD.isDown) {
console.log('D key pressed')
} else if(keyW.isDown) {
console.log('W key pressed')
}
您可以按每个键并检查console
消息以查看是否可以打印。
要获取所有键盘按键代码的列表以供将来参考:
console.log(Phaser.Input.Keyboard.KeyCodes)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句