我正在设计一款游戏,其中将向玩家提供4种声音,他们必须猜测这些声音的共同点。它与“4张图片1个单词”游戏完全一样,但玩家必须猜测声音而不是图片。
用户必须按键1,2,3和4才能听到声音。玩家听到声音后,就必须猜测声音的共同点。播放器听到4号声音后,他们会输入要猜出的单词,该如何编码?
作为第1级的一个示例,将为玩家提供猫,狗,鹦鹉和仓鼠的声音,并且必须猜测这四种声音之间的共同点。听到声音后,玩家将被告知单词由3个字母组成,他们必须猜测它是什么。如果说玩家插入了字母“E”,则“E”将插入缺失的单词中,玩家需要猜测剩余的单词。正确的单词是“PET”。如何在Phaser中编写代码?
(顺便说一句,该游戏没有图形或动画,因为它是针对盲人的)。
如果(game.input.keyboard.isDown(Phaser.Keyboard.P){“,我在“”行收到错误消息“未捕获的SyntaxError:意外 token {”
听到这四种声音后,我仍在尝试播放的输入无效。我不知道哪里出了问题。
我的代码: 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');
最佳答案
我建议您不要听特定的键,而是全部。您可以使用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
关于javascript - 音频输入相位器?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25957617/