I create a game on Canvas / Javascript. There is a sound button when clicking should change the sound icon (on / off). onklik does not work

var vol = true; if (vol == true) { ctx.drawImage(volume, 900, 20, 35, 35); } if (vol == false) { ctx.drawImage(volume_muted, 900, 20, 35, 35); } volume.onclick = function () {vol=false;}; volume_muted.onclick = function () {vol=true;}; 

does not work through click

 volume.addEventListener("click", vold); function vold() { vol = false; } 

but through keydaun works

 document.addEventListener("keydown", vold); function vold(b) { if (b.keyCode) {vol = false;} } 
  • see volume without pointer-events or is closed by something - Stranger in the Q
  • Insert the launched snippet to see the problem, so not very clear from the question - Kvilios
  • that is, click does not work on the sound icon. When you click on the volume, the value of vol = true changes to false. When vol = true an icon with a sound is drawn, when vol = false the other way around is an icon without sound. But through keydaun works, I need a click. Or do you need something else to implement? - Nurbek Nazarbay

1 answer 1

Everything, solved the problem, it is necessary like this through this

 if (vol == true) { ctx.drawImage(volume, 900, 20, 35, 35); this.onclick = function () {vol=false;}; } if (vol == false) { ctx.drawImage(volume_muted, 900, 20, 35, 35); this.onclick = function () {vol=true;}; }