2012-08-05 33 views
0

我正在創建遊戲,當出現開始屏幕時,某些音樂開始播放。現在我還在開始屏幕中添加了一個複選框。選中此複選框時,如果未選中,應聽到音樂,不應播放音樂。當取消選中複選框時,聲音未靜音

我對這個下面的代碼:對gameload

// Show the start screen and play some music. 
game.start(game.DEMO_MODE); 
if (document.getElementById('music').checked){ //check if checkbox is checked 
    music.play(); 
    music.loop(); //Loop the music 
} 

此代碼檢查,如果該複選框被選中,如果是的話,播放音樂。這個工程很好,但只有在遊戲加載時。如果複選框被選中,並且一個人在開始屏幕中取消選中,則音樂會繼續播放。除非他重新加載遊戲,否則聲音會消失。

顯然我不想要這個。當複選框未被選中時,聲音應該停止,反之亦然。

我認爲這個問題可以通過使用jQuery的.bind('change',function(){來解決,但我沒有成功這樣做。我希望這裏有人知道需要添加什麼...

的複選框的代碼(也存儲它的選中/取消選中狀態,Cookie或本地存儲):

jQuery(function($) { 
    // Aa key must is used for the cookie/storage 
    var storedData = getStorage('com_test_checkboxes_'); 

    $('div#musiccheck input:checkbox').bind('change',function(){ 
     // do something here tho mute or unmute the sound?? 

     // save the data on change 
     storedData.set(this.id, $(this).is(':checked')?'checked':'not'); 
    }).each(function() { 
     // on load, set the value to what we read from storage: 
     var val = storedData.get(this.id); 
     if (val == 'checked') $(this).attr('checked', 'checked'); 
     if (val == 'not') $(this).removeAttr('checked'); 
     if (val) $(this).trigger('change'); 
    }); 
}); 

如何解決這個問題?

親切的問候,

莫里斯

回答

1
$("#music").on('change', function() { 
    if (this.checked) { 
     music.play(); 
     music.loop(); 
    }else{ 
     music.stop(); 
    } 
}); 
+0

這個偉大的工程,謝謝! – Maurice 2012-08-05 08:04:47

相關問題