2

I am making a js game with a pause button. The button works fine, however if my mouse enters the button, leaves, and then clicks, the effect is triggered. Here is my code:

    document.onmousemove=function(mouse){
    mouseX=mouse.clientX-8;
    mouseY=mouse.clientY-8;
    }
    //some time later in code
    if(mouseX > Img.pause.x && mouseX < Img.pause.x+Img.pause.width && mouseY>Img.pause.y&& mouseY<Img.pause.y+Img.pause.height){//checked if mouse in button
    document.getElementById('ctx').style.cursor="pointer";
    document.onclick=function(){
    pause=true;//if mouse in button AND it clicks
    }
    }
    else{
    document.getElementById('ctx').style.cursor="default";  
    }

The issue is that the mouse doesn't have to be in the button, it just has to have been in it for some time. How can I make this so that the mouse has to be inside AND click?

shurup
  • 751
  • 10
  • 33
  • 1
    In the code shown you never remove the click handler. If you're going to implement your own "mouse in" equivalent handler within the mousemove handler then you also need to implement your own "mouse out" equivalent. And/or you could test the mouse coordinates inside the click handler. – nnnnnn Apr 02 '16 at 23:47
  • Why wouldn't my else statement mean if mouse isn't in the coordinates listed above do nothing(except change the cursor)? I tried copying the if mouse coordinate statement and put it in onclick and everything works fine now. Thanks – shurup Apr 03 '16 at 00:02

0 Answers0