あなたが考えている非常に面白いゲーム。私は子供の教育に役立つものに貢献してくれることをうれしく思います。とにかくこれがあなたを助けるかもしれないと思います。質問があれば教えてください。しかし、これはOOPのアプローチの多くを必要とします。
デモ:https://jsfiddle.net/m92mf7kL/
var Animal = function(animal, audio) {
this.animal = animal;
this.audio = audio;
return this;
};
Animal.prototype = {
playSound: function() {
var audio = document.createElement('audio');
audio.src = this.audio;
audio.play();
}
};
// Create animal objects
var chimp = new Animal("chimp", "http://www.thewavsite.com/Animals/Chimp01.wav"),
dog = new Animal("dog", "http://www.thewavsite.com/Animals/Dog01.wav"),
donkey = new Animal("donkey", "http://www.thewavsite.com/Animals/Donkey01.wav"),
goat = new Animal("Goat", "http://www.thewavsite.com/Animals/Goat.wav");
var animals = [chimp, dog, donkey, goat];
function playRandomAnimal() {
var sound = animals[Math.floor(Math.random() * animals.length)];
sound.playSound();
}
HTML
<button onClick="playRandomAnimal()">
Play Sound
</button>
こんにちは、あなたの助けに感謝しますが、あなたのコードが動作するようには思えない... は、ここで私が使用JSです: var sounds = [ 新しいオーディオ( "http://www.thewavsite.com/Animals/Lion03.wav")、 新しいオーディオ( "http://www.laffertyranch.org/sounds/eagle.wav")、 の新しいオーディオ( "http://files.anitalink.com/gamecache/hl/svencoop/sound/mw/monkey1.wav") ]; var el = document.getElementsById( 'animals'); el.addEventListener(function(){ //var sound = sounds [Math.random()* sounds.length]; sound.play(); } ); そしてHTML:あなたの忍耐のために// – icenine
ここをクリック... sintaxに何か問題があると思います。 var el = document.getElementById( 'animals'); el.addEventListener(function(){ //ランダムを取る音のリストからの音: var sound = sounds [Math.random()* sounds.length]; sound.play(); }); ブラケットを使用していて、eventlistener関数の親子と中括弧が一致しません。また、このコードでは、メインのJSファイル内の他のプログラムが機能しなくなりました。それを修正しようとしたが、どこにも行かなかった。 – andlrc
事前に感謝 はありません、私は、httpを試してみた: <ボタンID =「動物」>あなたはhttpで外部URLをプレフィックスする必要が – icenine