0

我正在设计一款游戏,玩家将获得 4 种声音,他们必须猜测这些声音的共同点。它的工作原理与“4张图片1字”游戏完全一样,但玩家必须猜测声音而不是图片。

用户必须按 1、2、3 和 4 键才能听到声音。一旦玩家听到了这些声音,玩家就必须猜测这些声音的共同点。我如何编码玩家听到 4 个声音后输入单词来猜测的部分?

以第 1 关为例,玩家将听到猫、狗、鹦鹉和仓鼠的声音,并猜测这四种声音的共同点。玩家听到声音后会被告知该单词由 3 个字母组成,他们必须猜测它是什么。如果假设玩家插入字母“E”,则“E”将被插入丢失的单词中,玩家需要猜测剩余的单词。正确的词是“PET”。如何在 Phaser 中编码?

(顺便说一句,该游戏没有图形或动画,因为它是为盲人用户准备的)。

如果 ( game.input.keyboard.isDown ( Phaser.Keyboard.P ) { "

听到四个声音后我仍在尝试播放的输入不起作用。我不知道我哪里出错了。

我的代码:

  var game = new Phaser.Game(400,490, Phaser.AUTO, 'gameDiv');
  var key1;
  var key2;
  var key3;
  var key4;
  var speech;
  var level1;
  var PCorrect;
  var ECorrect;
  var TCorrect;
  var TryAgain;
  var mainState = {


preload: function() {
    game.stage.backgroundColor ='#71c5cf'
    game.load.audio('the_intro', 'assets/speech_intro.wav');
    game.load.audio('start', 'assets/cat_meow.wav');
    game.load.audio('second', 'assets/dog_bark.wav');
    game.load.audio('third', 'assets/mouse_squeak.wav');
    game.load.audio('fourth', 'assets/parrot.wav');
    game.load.audio('default' , 'assets/wrong_key.wav');
    game.load.audio('levelPet' , 'assets/GuessPet.wav');
    game.load.audio('P', 'assets/PforPet.wav');
    game.load.audio('T', 'assets/TforPet.wav');
    game.load.audio('E', 'assets/EforPet.wav');
    game.load.audio('Try','assets/WrongAnswer.wav');

},


create: function() {

     //introduction narration
     speech = game.add.audio('the_intro');

     speech.play();

    //Sound added 1st
    this.meowSound = game.add.audio('start');
    this.barkSound = game.add.audio('second');
    this.softSound = game.add.audio('third');
    this.talkingSound = game.add.audio('fourth');
    this.noSound = game.add.audio('default');

    //Call the sound when the key is pressed
    var self = this;
    game.input.keyboard.onDownCallback = function(e) {
        var keycode = e.keycode || e.which;

        switch(keycode) {

            case Phaser.Keyboard.ONE:
                 self.addCat();
               break;

            case Phaser.Keyboard.TWO:
               self.addDog();
               break;

            case Phaser.Keyboard.THREE:
                self.addMouse();
                break;

            case Phaser.Keyboard.FOUR:
                 self.addParrot();

                  level1 = game.add.audio('levelPet');
                  level1.play();
                 break;

            default:
                 self.addOthers();
                 break;
               break;



        }


    }


  },

update: function(){

    this.PLevel1 = game.add.audio('P');
    this.ELevel1 = game.add.audio('E');
    this.TLevel1 = game.add.audio('T');
    this.WrongAns = game.add.audion('Try');

    var self = this;

    if (acceptInput) {
    if (game.input.keyboard.isDown(Phaser.Keyboard.P) {

        self.addP();

    } else if (game.input.keyboard.isDown(Phaser.Keyboard.E)) {

        self.addE();

    } else if (game.input.keyboard.isDown(Phaser.Keyboard.T)) {
        self.addT();
    }

     else 
     {
        self.addWrong();
     }





  }

  }


addCat: function() {

    this.meowSound.play();
},

addDog: function() {
    this.barkSound.play();
},

addMouse: function() {
    this.softSound.play();
},

addParrot: function() {
    this.talkingSound.play();
},

addOthers: function() {
    this.noSound.play();
},

addP: function() {
    this.PLevel1.play();
}

addE: function() {
    this.ELevel1.play();

 }

addT: function() {
    this.TLevel1.play();
}

addWrong: function() {
    this.WrongAns.play();
}
};



game.state.add('main', mainState);
game.state.start('main');
4

1 回答 1

3

我建议你不要听特定的键,而是所有的键。你可以用这个Keyboard.addCallbacks方法做到这一点。签名是这样的:

/** * Add callbacks to the Keyboard handler so that each time a key is pressed down or released the callbacks are activated. * * @method Phaser.Keyboard#addCallbacks * @param {Object} context - The context under which the callbacks are run. * @param {function} [onDown=null] - This callback is invoked every time a key is pressed down. * @param {function} [onUp=null] - This callback is invoked every time a key is released. * @param {function} [onPress=null] - This callback is invoked every time the onkeypress event is raised. */ addCallbacks: function (context, onDown, onUp, onPress) {

你只需要知道 onPress 事件,所以你可以这样做:

game.input.keyboard.addCallbacks(this, null, null, keyPress);

keyPress按下键时调用的函数的名称在哪里。这个函数将被按下的字符作为字符串发送。因此,在此功能中,如果您要从播放器中查找单词 PET,您可以检查按下的键是 P、E 还是 T,并相应地突出显示它。这是我为您整理的一个工作示例:http ://examples.phaser.io/debug.php?f=input/word+input.js

于 2014-09-21T21:38:58.347 回答