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

2017/2/2

Player Actor facing mouse similar to Hotline Miami

Tezzsyhr Tezzsyhr

2017/2/2

#
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);
             
        }
    }   
}
Super_Hippo Super_Hippo

2017/2/2

#
You could move line 27 to the end of its method (outside the 'if's). Btw, you probably want to move first and then turn towards the mouse.
Tezzsyhr Tezzsyhr

2017/2/2

#
Just tried this, it stops the character model from rotating completely.
Super_Hippo Super_Hippo

2017/2/2

#
Show what you have done. I am not sure how this could happen.
Tezzsyhr Tezzsyhr

2017/2/2

#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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);
    }
I edited the movePlayer() method so that pointerDirection() was at the end of the method too.
Tezzsyhr Tezzsyhr

2017/2/2

#
Got it to work eventually, rather than moving line 27 to outside the if statements, I copied it, and that seemed to get it working.
Super_Hippo Super_Hippo

2017/2/2

#
Hm, interesting. I don't see why though. I mean if you have it in line 13 and line 16 (of your last shown code), line 16 is always executed. Even if line 13 is executed, line 16 *should* do the same thing again.
Nosson1459 Nosson1459

2017/2/3

#
I don't see why you... 1) ...can't leave the turnTowards in the ifs, if you only want to change direction to the mouse 2) ...have that first if in the pointerDirection method. As long as mouse != null then turnTowards it, what's the difference if Greenfoot.mouseDragged(null) || Greenfoot.mouseMoved(null) or not.
Nosson1459 Nosson1459

2017/2/3

#
Tezzsyhr wrote...
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?
So to answer your question the reason it happens is because YOU added in an if to purposely make it happen, all this if does is make that happen. The if to remove is the one mentioned above.
1
if (Greenfoot.mouseDragged(null) || Greenfoot.mouseMoved(null))
when I removed this if it worked fine.
You need to login to post a reply.