我正在设计一个游戏,玩家将得到4种声音,他们必须猜测声音有什么共同之处。它的工作原理是"4图片1字“游戏,但不是图片,玩家必须猜出声音。
用户必须按1、2、3和4键才能听到声音。一旦玩家听到了声音,玩家就必须猜出这些声音有什么共同之处。我如何编码的部分,在玩家听到了4的声音后,他们会输入这个词来猜测它?
作为第1级的一个例子,玩家将被给予猫,狗,鹦鹉和仓鼠的声音,并且必须猜测这四种声音之间的共同之处。玩家在听到声音后会被通知,这个单词由3个字母组成,他们必须猜出它是什么。如果玩家插入字母"E“,则"E”将被插入到缺失的单词中,而玩家需要猜测剩下的单词。正确的词将是"PET“。我怎么用Phaser编码这个?
(顺便说一句,这款游戏没有图形或动画,因为它是为盲人用户准备的)。
我收到错误消息"Uncaught :意外令牌{“一行"if SyntaxError {”。
在听到这四种声音后,我仍在尝试播放的输入不起作用。我不知道我哪里出了问题。
我的代码:
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');发布于 2014-09-21 21:38:58
我建议你不要听特定的键,而是所有的键。您可以使用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
https://stackoverflow.com/questions/25957617
复制相似问题