This site requires JavaScript, please enable it in your browser!
Greenfoot back
JyspeKV2
JyspeKV2 wrote ...

2023/5/3

I need Help Programming smooth Mouse Moving for a Parking Jam type game

JyspeKV2 JyspeKV2

2023/5/3

#
the code I have for mouse movement is as follows
if(Greenfoot.mousePressed(this))
        {
            setLocation(Greenfoot.getMouseInfo().getX(), getY());
        }
now the issue is this is really jittery I would like it to be smooth, and depending on the orientation of the actor(there maybe more then one and in different orientations)
danpost danpost

2023/5/3

#
JyspeKV2 wrote...
the code I have for mouse movement is as follows << Code Omitted >> this is really jittery I would like it to be smooth, and depending on the orientation of the actor
Maybe you are using the wrong methods For moving, the move(int) method might be better than setLocation(int, int), as you seem to only move in straight lines forward or backward. As far as the method used for the mouse, Greenfoot.mousePressed(Actor) does not always return true while a button is pressed on an actor. It is true when the button goes down (the pressing action is performed), but not when held down. You probably need to save the coordinates of where the mouse was originally pressed (to determine its movements). Then, use Greenfoot.mouseDragged(Actor) to test for mouse moving the actor. Maybe something like this:
private int mX, mY; // for mouse coordinates

public void act() {
    MouseInfo mouse = Greenfoot.getMouseInfo();
    if (mouse == null) return;
    if (Greenfoot.mousePressed(null)) {
        mX = mouse.getX();
        mY = mouse.getY();
    }
    if (Greenfoot.mouseDragged(this)) {
        int dx = mouse.getX()-mX;
        int dy = mouse.getY()-mY;
        move(getRotation()%180 == 0 ? dx : dy);
        mX = mouse.getX();
        mY = mouse.getY();
    }
}
JyspeKV2 JyspeKV2

2023/5/4

#
Well everything works great with every actor, but on the Player Character its inverted So when I move the mouse Left it goes right vice versa, it may have to do with the direction of the PC facing the Left side of the screen
danpost danpost

2023/5/4

#
JyspeKV2 wrote...
Well everything works great with every actor, but on the Player Character its inverted So when I move the mouse Left it goes right vice versa, it may have to do with the direction of the PC facing the Left side of the screen
Then, try the following for line 13:
move((getRotation()%180 == 0 ? dx : dy)*(1-2*(getRotation()/180)));
Or (same thing, but not calling getRotation twice):
int rot = getRotation();
move((rot%180 == 0 ? dx : dy)*(1-2*(rot/180)));
JyspeKV2 JyspeKV2

2023/5/5

#
The first Fix works Perfectly thank you danpost
You need to login to post a reply.