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

2016/4/17

How do I make the player strafe left and right?

RedCreeper RedCreeper

2016/4/17

#
I have it so that the player turns toward the mouse and move back and forth with the W and S keys, but I am not quite sure how to make the player move left and right, while staying relative to the rotation of the player. Please help and thank you!! I have included the code for the player:
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
 
/**
 * Write a description of class Player here.
 *
 * @author (your name)
 * @version (a version number or a date)
 */
public class Player extends SmoothMover
{
    public boolean atWorldEdge = false;
    private double WalkSpeed = 3.0;
    private int TurnSpeed = 5;
    public boolean Alive;
    public double Health = 100;
    public int Ammo = 100;
    private boolean isShooting;
    public int XPos;
    public int YPos;
    private boolean Moving;
    private float Rotation;
    private int DamageDelay = 1;
    
     
    GreenfootImage[] images = new GreenfootImage[20];
    private int imageNumber;
     
    public Player()
    {
        for( int i=0; i<images.length; i++ )
        {
            images[i] = new GreenfootImage( "survivor-move_shotgun_" + i + ".png" );
        }
        setImage( images[imageNumber] );
       
    }
     
    /**
     * Act - do whatever the Player wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
       XPos = getX();
       YPos = getY();
        if(Health > 0)
       {
           KeyboardInput();
           
           Rotation = getRotation();
           CheckCollision();
           if(Moving == true)
           {
               Animate();
            }
        }
        else
        {
            getWorld().removeObject(this);
        }
    }   
     
    /**
     * KeyboardInput - Gets the keyboard keys that are pressed and acts on it.
     */
    public void KeyboardInput()
    {
         if(Greenfoot.isKeyDown("w"))
        {
            move(WalkSpeed);
            Moving = true;
        }
        else if(Greenfoot.isKeyDown("s"))
        {
            move(-WalkSpeed);
            Moving = true;
        }
        else
        {
            Moving = false;
            imageNumber = 0;
        }
        MouseInfo mouse = Greenfoot.getMouseInfo();
          
        if (mouse != null && mouse.getX() != this.getX() && mouse.getY() != this.getY())
        
           turnTowards(mouse.getX(), mouse.getY()); 
        }
        // if(Greenfoot.isKeyDown("left"))
        // {
            // turn(-TurnSpeed);
        // }
        // else if(Greenfoot.isKeyDown("right"))
        // {
            // turn(TurnSpeed);
        // }
        // if(Greenfoot.isKeyDown("space"))
        // {
            // isShooting = true;
            // Shoot();
        // }
        // else
        // {
            // isShooting = false;
        // }
    }
     
    public void Animate()
    {
        imageNumber = ( imageNumber + 1 ) % images.length;
        setImage( images[imageNumber] );
    }
     
    public void Shoot()
    {
            getWorld().addObject(new Bullet(getRotation()), getX(), getY());
    }
     
    public void CheckCollision()
    {
        if(getOneIntersectingObject(Zombie.class) != null)
        {
            DamageDelay--;
            if(DamageDelay <= 0)
            {
                Health = Health - 0.1;
                DamageDelay = 1;
            }
        }
    }
}
danpost danpost

2016/4/17

#
For left:
1
2
3
turn(-90);
move(<?>)
turn(90);
For right, reverse the order of the lines.
RedCreeper RedCreeper

2016/4/20

#
Sorry about the delay, but what do you mean by move(<?>) ? Thank You!
danpost danpost

2016/4/20

#
Just replace the '<?>' with the numeric value representing how far you want the actor to move sidesways with respect to moving toward the mouse.
RedCreeper RedCreeper

2016/4/20

#
Sorry that was a dumb questions. However, when I try to make the character move to the right it doesn't seem to work. Going to the left does however. Here is the code for the player moving right:
1
2
3
4
5
6
if(Greenfoot.isKeyDown("d"))
{
turn(90);
move(WalkSpeed);
turn(-90);
}
danpost danpost

2016/4/21

#
RedCreeper wrote...
Sorry that was a dumb questions. However, when I try to make the character move to the right it doesn't seem to work. Going to the left does however. Here is the code for the player moving right: < Code Omitted >
Does it make a difference where the mouse is? or is it always the right that has problems? and what code did you use to go left?
RedCreeper RedCreeper

2016/4/21

#
As far as moving left, it seems to work where ever the mouse is. I don't know if using the turnTowards method stops the movement to the right (line: 87). For the code to the left i am using:
1
2
3
4
5
6
if(Greenfoot.isKeyDown("a"))
{
turn(-90);
move(WalkSpeed);
turn(90);
}
danpost danpost

2016/4/21

#
Okay, show both the 'moveLeft' and 'moveRight' methods along with the 'act' method (for now).
RedCreeper RedCreeper

2016/4/21

#
Sorry, It seems that the code works now, for some reason my computer wasn't responding to the code and with a restart of greenfoot it seems to work. Thank you!!
You need to login to post a reply.