Java game how to fix offset move with mouse drag and reset back to start position

by Coded5   Last Updated April 15, 2019 09:13 AM

I'm try to make offset x and offset y move correspond with mouse drag movement I tested it. Its work but after I released the mouse and do it again the offset will reset to 0, 0

I tried to fix it by adding old offset x, y and the result is the offset will keep increasing.

Here my code :

public void updateOffset() {
    if(Game.getMouseInput().mouseState == MouseEvent.BUTTON1 && !closeOrigin) {
        ox = Game.getMouseInput().x;
        oy = Game.getMouseInput().y;
        ex = Game.getMouseInput().x;
        ey = Game.getMouseInput().y;
        closeOrigin = true;
    } 

    if(Game.getMouseInput().drag) {
        ex = Game.getMouseInput().x;
        ey = Game.getMouseInput().y;
    }

    if(ex - ox == 0 && ey - oy == 0)
        return;

    offsetX = ex - ox;
    offsetY = ey - oy;

    if(Game.getMouseInput().mouseState == MouseEvent.NOBUTTON && closeOrigin) {
        offsetX = ex - ox;
        offsetY = ey - oy;
        closeOrigin = false;
    }
}

And this code is called every frames

Tags : java camera mouse


Related Questions


Updated May 16, 2015 21:05 PM

Updated January 23, 2019 15:13 PM

Updated June 23, 2015 00:05 AM

Updated November 05, 2018 23:13 PM

Updated December 03, 2018 17:13 PM