Javascript 输入。键盘是未定义的PhaserJs
我正在使用此代码尝试向游戏对象添加键盘事件。所有示例,甚至Phaser API都说我应该添加如下键盘事件: Game.StartMenu.prototype={Javascript 输入。键盘是未定义的PhaserJs,javascript,keyboard,phaser-framework,Javascript,Keyboard,Phaser Framework,我正在使用此代码尝试向游戏对象添加键盘事件。所有示例,甚至Phaser API都说我应该添加如下键盘事件: Game.StartMenu.prototype={ create: function () { startBG = this.add.image(0, 0, 'titlescreen'); startBG.inputEnabled = true; startBG.events.onInputDown.addOnce(this.startGame, this);
create: function () {
startBG = this.add.image(0, 0, 'titlescreen');
startBG.inputEnabled = true;
startBG.events.onInputDown.addOnce(this.startGame, this);
console.log("startBG.input: " + startBG.input);
console.log("startBG.input.keyboard: " + startBG.input.keyboard);
startPrompt = this.add.bitmapText(this.world.centerX-155, this.world.centerY+180, 'eightbitwonder', 'Touch to Start!', 24);
},
startGame: function (pointer) {
this.ding.play();
this.state.start('Game');
}
})
“addOnce”输入单击侦听器正在工作。然而,当我注销时,“startBG.input.keyboard”只是返回未定义的值。为什么会这样
注意:以下是我的预加载状态:
Game.Preloader = function(game) {
this.preloadBar = null;
this.titleText = null;
this.ready = false;
};
Game.Preloader.prototype = {
preload: function () {
this.preloadBar = this.add.sprite(this.world.centerX, this.world.centerY, 'preloaderBar');
this.preloadBar.anchor.setTo(0.5, 0.5);
this.load.setPreloadSprite(this.preloadBar);
this.titleText = this.add.image(this.world.centerX, this.world.centerY-220, 'titleimage');
this.titleText.anchor.setTo(0.5, 0.5);
this.load.image('titlescreen', 'images/TitleBG.png');
this.load.bitmapFont('eightbitwonder', 'fonts/eightbitwonder.png', 'fonts/eightbitwonder.fnt');
this.load.image('hill', 'images/hill.png');
this.load.image('sky', 'images/sky.png');
this.load.atlasXML('bunny', 'images/spritesheets/bunny.png', 'images/spritesheets/bunny.xml');
this.load.atlasXML('spacerock', 'images/spritesheets/SpaceRock.png', 'images/spritesheets/SpaceRock.xml');
this.load.image('explosion', 'images/explosion.png');
this.load.image('ghost', 'images/ghost.png');
this.load.audio('explosion_audio', 'audio/explosion.mp3');
this.load.audio('hurt_audio', 'audio/hurt.mp3');
this.load.audio('select_audio', 'audio/select.mp3');
this.load.audio('game_audio', 'audio/bgm.mp3');
},
create: function () {
this.preloadBar.cropEnabled = false;
},
update: function () {
if(this.cache.isSoundDecoded('game_audio') && this.ready == false) {
this.ready = true;
this.state.start('StartMenu');
}
}
};
似乎无法像连接单击/点击侦听器那样将键盘事件侦听器连接到Phaser中的任何旧精灵或图像。相反,您只能将“.input.keyboard”附加到主游戏对象。现在我想起来了,这是有道理的。键盘输入被发送到游戏的最高级别对象,在其回调方法中,用户可以根据按下的键操作任何游戏资源