Input.Keyboard是Undefined PhaserJs

时间:2017-03-27 16:33:03

标签: 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);

    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"只是返回undefined。这是为什么?

注意:这也是我的预加载状态:

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');
        }
    }
};

1 个答案:

答案 0 :(得分:0)

您似乎无法将键盘事件侦听器附加到Phaser中的任何旧精灵或图像,就像您可以附加点击/点击侦听器一样。相反,你只能附加" .input.keyboard"到主游戏对象。现在我考虑一下,这是有道理的。键盘输入被发送到游戏的最高级别对象,并且在其回调方法中,可以根据按下的键来操纵任何游戏资产。

相关问题