Jump to content

How to stop dragging when mouse out of the screen?


phaserlover
 Share

Recommended Posts

I had this problem too. I had no idea how to fix it within Phaser, so I just hacked a workaround by adding my own listener for onMouseOver, something like this:

 

this.div = document.getElementById('game');

this.div.onmouseover = this.onMouseOver;

 

and then I had a function:

 

onMouseOver: function(e) {

    if (e.buttons >= 0) {

        //mouse returned with the button still pressed, continue dragging

    } else {

        //mouse returned with the button released, add code to stop dragging

    }

}

Link to comment
Share on other sites

I had this problem too. I had no idea how to fix it within Phaser, so I just hacked a workaround by adding my own listener for onMouseOver, something like this:

this.div = document.getElementById('game');this.div.onmouseover = this.onMouseOver;

Can't you just :

myDiv.onmouseout = function(){      // Stop dragging }

can you ?

Link to comment
Share on other sites

In Game Desig logic, you should cancel the action, I think that the player would prefer restart dragging action than the object going to a non desired place because of his non precise mouse :)

Link to comment
Share on other sites

I have a snippet for catching when the mouse leaves the browser. I don't know if it solves your specific problem, but here goes:

 

    function mouseLeftWindow(fn, context) {        document.addEventListener('mouseout', function(e) {            var from = e.relatedTarget || e.toElement;            if (!from || from.nodeName === 'HTML') {                fn.call(context || this);            }        });    }
Link to comment
Share on other sites

In Game Desig logic, you should cancel the action, I think that the player would prefer restart dragging action than the object going to a non desired place because of his non precise mouse :)

In my case, cancelling was undesired. In that particular game, it was much better to allow to continue dragging even after accidentally hitting the border. It is game-specific though.

 

Your code will ofc work.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...