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

2019/11/14

move, turn, move down, turn and repeat that movement until it hits a corner

thatonegirl thatonegirl

2019/11/14

#
So I'm trying to make a movement in which the actor starts at 0,0 and moves until it hits the edge. Once it hits the edge I want it to turn 90 degrees, move down 5, turn another 90 degrees and continue this movement until it hits the bottom left corner. Once it hits the bottom left corner, I want my actor to teleport to the center of the world. I figured out how to get the actor to teleport but I'm struggling with getting the movement to work. I was trying to use the isAtEdge method but I realized that technically the actor is starting at the edge which is the part I'm stuck on.
thatonegirl thatonegirl

2019/11/14

#
This is my code so far...
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
import greenfoot.*; 
public class Ball extends Actor
{
    private int speed;
     
    public Ball()
    {
        speed = 0;
    }
     
    public Ball( int initialSpeed )
    {
        speed = initialSpeed;
    }
 
    public int getSpeed()
    {
        return speed;
    }
     
    public void setSpeed( int newSpeed )
    {
        speed = newSpeed;
    }
    
    public void act()
    {
        if( isAtEdge() )
        {
            turn(90);
            move(5);
            turn(90);
        }
        else
        {
            move(speed);
        }
         
        if(getX() == 0 && getY() == getWorld().getHeight() )
        {
            int xCoord = (getWorld().getWidth()) / 2;
            int yCoord = (getWorld().getHeight())/ 2;
            setLocation(xCoord,yCoord);
        }
         
    }
}
danpost danpost

2019/11/14

#
thatonegirl wrote...
I realized that technically the actor is starting at the edge which is the part I'm stuck on.
That would be an indication that you need another condition to limit turning. The current rotation along with the current vertical position should be enough data to regulate the turning properly. Using isAtEdge is not sufficient -- use the horizontal position (compare current x to 0 and one less than world width). If the ball does not have to turn physically, but just in its moving, I would suggest you use your speed field with a direction field. The direction field should be 1 or -1, indicating the direction of travel. At the end, when the actor is placed in the middle, set it to zero. The speed field should be 5, except for when an edge is hit, then set it to zero. After moving vertically, set the speed field back to 5. Use the speed field to determine moving horizontally or vertically and only move vertically when the direction field is not zero. You can check both edges at once after moving with:
1
2
move(dir*speed);
if (getX()%(getWorld().getWidth()-1) == 0)
thatonegirl thatonegirl

2019/11/14

#
I don't know how to check edges though, I know you use getHeight() but that's it. Could you show it to me?
danpost danpost

2019/11/14

#
thatonegirl wrote...
I don't know how to check edges though, I know you use getHeight() but that's it. Could you show it to me?
My line 2 above checks for left and right edges (at the same time). Your line 39 checks for path completion (lower-left corner) and it does use getHeight. Those are the main position checks you need.
You need to login to post a reply.