2016-08-22 13 views
0

を働く私は、HTML5の響板に取り組んでいますが、私は思わぬ障害のビットをヒットしてきました...Javascriptのオブジェクトの関数呼び出しは文句を言わない

私はすべてで動作するように停止機能を取得しようとしていますすぐに演奏される音。残念なことに、私がbuttonpressからこの関数を呼び出すと、オブジェクトには停止機能がないようです。実際の音要素は以下の通りですのコード:

// Container to keep all sounds in one place. This is a Dictionary within a dictionary to be able to search by catagory. 
var sounds = {}; 

// Counter to keep track of unique ID's 
var lastID = 0; 


// Base class for initializing Any class 
var Base = function(methods){ 
    var base = function() {  
     this.initialize.apply(this, arguments);   
    }; 

    for (var property in methods) { 
     base.prototype[property] = methods[property]; 
    } 

    if (!base.prototype.initialize) base.prototype.initialize = function(){};  

    return base;  
}; 

//Complete class for the Sound object. Generates its own DIV's and HTML5 tags to play stuff. 
var Sound = Base({ 
    // Init all the variables. 
    initialize: function(name, file, target='Sounds') { 
     this.name = name; 
     this.file = file 
     this.button = null; 
     this.audioelement; 
     this.id = lastID + 1; 
     this.target = target; 
     lastID ++; 


     // Check if the catagory is there, if not: create it with a placeholder object 
     var catagory = sounds[this.target]; 
     if(catagory == null){ 
      sounds[this.target] = {99:null}; 

     } 
     sounds[this.target][this.id] = this; 

     // Call init function 
     this.init(); 
    }, 

    play : function() { 
     obj = this 
     if(obj.audioelement.paused == true){ 
      obj.audioelement.play();  
     }else{ 
      obj.audioelement.pause(); 
      obj.audioelement.fastSeek(0); 
     } 

    }, 
    stop : function(){ 
     obj = this; 
     obj.audioelement.pause(); 
    }, 
    init : function(){ 

     // Statement for JS class based shenanigans. 
     obj = this 

     // Create a button and add some text to it 
     obj.button = document.createElement("BUTTON"); 
     obj.button.appendChild(document.createTextNode(obj.name)); 

     // Set ID's and names to keep track of this button 
     obj.button.id = obj.id; 
     obj.button.name = obj.target; 

     // Get or create parent element. Used for catagory based display 
     var el = getOrCreateElement(obj.target) 
     el.appendChild(obj.button); 

     // Create audio element and set appropriate settings 
     obj.audioelement = document.createElement("AUDIO"); 
     obj.audioelement.src = obj.file; 
     obj.audioelement.name 
     obj.button.appendChild(obj.audioelement); 

     // Add function to play/pause to button 
     obj.button.onclick = buttonClicked; 

}); 
function buttonClicked(){ 
    // Fetch sound from dicionary container using the name and id from the button [SET AT SOUND.INIT()] 
    var sound = sounds[this.name][this.id]; 
    // Call the play function in [SOUND] 
    sound.play(); 
} 

そしてSTOPALL機能のために:

function stopAll(){ 
    // Scroll through the entire dictionary 
    for (var key in sounds){ 
     for (var id in sounds[key]){ 
      // Check if the sound is not a placeholder 
      if(id == 99){ 
       continue; 
      } 
      // Call stop function with fetched object. 
      var sound = sounds[key][id]; 
      sound.stop(); 
     } 
    } 
} 

奇妙なことがあるが、停止機能プレイ機能が動作するようには思えないということではなく、 。オブジェクトにはその特定の機能がないと言われています...

任意のアイデアが浮かび上がります!ベース・オブジェクトのプロトタイプのものを含め、単に方法、より多くのいくつかのプロパティを超える

WM

+0

あなたはいくつかの助けが必要な場合は、再生/停止を行うために使用しているコードを投稿する必要があります。 – loretoparisi

+0

実際の機能はすべてそこにあります。欠落しているのは、関数自体を呼び出すロジックだけです。それを行う関数を追加します –

+0

エラーメッセージがありますか? – inf3rno

答えて

0

for (var x in object)ループ。

その内側のループ内にconsole.log(id);がある場合は、余分なものが表示されます。ループのためにあなたの内側の内側にこれを追加すること

試してみてください。

if (typeof id !== 'function') { 
    continue; 
} 

編集:これは非常に正確ではないですが、私はそれが今どうあるべきかを見るために位置していませんよ。メモリから、ほぼそこにある!それを続けてください。

+0

これは、ES5以外の環境、つまり非常に古いブラウザでのみ該当します。 – inf3rno

+1

私がJSの仕事をした最後の時間を推測することはできません;) – jedifans

+0

ほんとうに7年前です。 :D – inf3rno

関連する問題