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

2015/4/6

Enemy moving on Y-axis

Cathy3210 Cathy3210

2015/4/6

#
Trying to get the Boss in my game to only move up and down in my game, however I've also set a boundary because it's underwater. So I would like it to move up and down within the boundary. This is what I have, how can I fix it?
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
public void move()
{
    if(getX() > 716)
    {
        move(-speed_);
    }
    else if(getX() == 716)
    {
        setLocation(716, getY() + speed_);
    }
    else if(getY() == 465)
    {
        setLocation(716, getY() - speed_);
    }
    else if(getY() == 237)
    {
        setLocation(716, getY() + speed_);
    }
}
 
public void boundary()
{
    if(getY() == 465)
    {
        setLocation(getX(), 465);
    }
    if(getY() == 237)
    {
        setLocation(getX(), 237);
    }
}
The top boundary is at Y = 237, and the bottom boundary is at Y = 465. I wanted it to stop moving left, and to move up and down when it reached 716, which is why I have it so it stops moving at that coordinate.
azazeel azazeel

2015/4/6

#
1
2
3
4
5
6
7
8
9
10
11
public void boundary()
{
    if(getY() >= 465)
    {
        setLocation(getX(), 465);
    }
    if(getY() <= 237)
    {
        setLocation(getX(), 237);
    }
}
or just simply bounce it when it reach the boundary
1
2
3
4
if(isAtEdge()){
    turn(100);
    move(2);
}
danpost danpost

2015/4/6

#
Following your logic, lines 3 through 6 will execute and the boss will move left until 'getX' return a value of 716 or less (it is possible to skip over 716 if the value of 'speed_' is greater than one). Then, if 716 is not skipped over, lines 7 through 10 will have the boss move down and lines 11 through 18 will never execute because 'else' would require that the previous condition was not met (but, it was!). If 716 was skipped over, then lines 7 through 10 would not execute and the condition on lines 11 would be checked. If the boss is not at either of those y coordinates, 237 or 465, then no action will occur, otherwise it will move off that coordinate and stop (since it is no longer at that coordinate). The 'boundary' method logic should be looked at also. It says to place the boss at the y-coordinate location that it is already at, in either case of being at 237 or 465. Since you have two direction to possibly move vertically, it would seem prudent to include a toggle of some kind. You could use a boolean, which has two states, true or false; however, since you are dealing with coordinates (numbers), it would be better to use an int that you can negate back and forth between -1 and 1, for 'up' and 'down' respectively. Then you would have this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// instance field
private int yDir = 1;
 
// the method
private void move()
{
    if (getX() >716)
    { // move left
        move(-speed_);
    }
    else
    { // move vertically
        setLocation(getX(), getY()+yDir*speed_);
        if ((yDir > 0 && getY() >= 465) || (yDir < 0 && getY() <= 237))
        { // reverse directions
            yDir = -yDir;
        }
    }
}
You need to login to post a reply.