简体   繁体   English

如何关闭音效

[英]How to turn sound effects off

I have some code that isn't working correctly. 我有一些无法正常工作的代码。 I have two radio buttons on a game, one that is sound on the other is sound off. 我在游戏上有两个单选按钮,一个在另一个声音上关闭。

At the moment the sound off button doesn't turn the sound off when it is selected. 此刻,选择“关闭声音”按钮不会关闭声音。

The code I have so far for the sound is: 到目前为止,我的声音代码是:

var soundon = 1;
var soundoff = 0;

var soundon = opener.document.getElementById("sound_on");
if(element.checked === true)
{
soundon =1;
}

var soundoff = opener.document.getElementById("sound_off");
if(element.checked === true)
{
soundoff =0;
} 

Can someone please tell me what I am doing wrong and what I need to do to correct this. 有人可以告诉我我做错了什么,我需要做些什么来纠正这个问题。

You are using same variable for both the value and the element. 您正在为值和元素使用相同的变量。

In the IF statements you are using element , while the getElementById stores in sound variable. 在IF语句中,您正在使用element,而getElementById存储在声音变量中。

Change it and let us know 更改并告知我们

It's a better idea to have a listener on the object, like so: 最好在对象上有一个侦听器,如下所示:

var soundon = document.getElementById("sound_on");
soundon.onclick = function(){
    sound = 1;
};
var soundoff = document.getElementById("sound_off");
soundoff.onclick = function(){
    sound = 0;
};

It's also important to note that getElementById() won't work on objects not loaded yet, so you will want to get the objects after window.onload . 还需要注意的是, getElementById()不适用于尚未加载的对象,因此,您需要在window.onload之后获取对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM