Hi,
So I'm working on a project that has player movement similar to that of Hotline Miami (move Up, Down, Left and right using WSAD, and face the mose at all times) and I have the actor movement down, but the part I'm stuck on is the actor facing the mouse. If the mouse is stationary, the actor stops turning to face it. Is there a way to get the actor to always face the mouse regardless?
Many Thanks
Here's my current code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | import greenfoot.*; import java.util.*; public class playerChar extends Actor { int rotationX; int rotationY; int speed = 5 ; public void act() { movePlayer(); } public void pointerDirection() { //checks if the pointer is active and is moving if (Greenfoot.mouseDragged( null ) || Greenfoot.mouseMoved( null )) { MouseInfo mouse = Greenfoot.getMouseInfo(); if (mouse != null ) { //gets the rotation of the mouse and saves the values as variables rotationX = mouse.getX(); rotationY = mouse.getY(); //rotates the player to the mouse direction turnTowards(rotationX, rotationY); } } } private void movePlayer() { pointerDirection(); if (Greenfoot.isKeyDown( "a" )) { setLocation(getX() - speed, getY()); } if (Greenfoot.isKeyDown( "d" )) { setLocation(getX() + speed, getY()); } if (Greenfoot.isKeyDown( "w" )) { setLocation(getX(), getY() - speed); } if (Greenfoot.isKeyDown( "s" )) { setLocation(getX(), getY() + speed); } } } |