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

2018/6/7

make actor complete a loop?

kevoncmlc2000 kevoncmlc2000

2018/6/7

#
hey everyone i'm a physics nerd and i'm working on some project that uses greenfoot as a visual aid. it's not complete yet but for testing purposes i'm trying to make an electron actor complete a loop around a circuit. i used a switch statement with a default setting but it will not complete the last part because i only have one case stated so once the actor moves out of the y value it no longer moves in the desired direction. i know i can specify hundreds of cases but that's highly inconvenient. i also tried using a for loop in the range of my desired y coordinates but it says i must use a constant. any ideas on how to make this work correctly?
 public void move()
    {
        switch (getY())
        {
            case 532:
            {
                if (getX() != 359)
                {
                    setLocation (getX() - 3, getY());
                }
                if (getX() == 359)
                {
                    setLocation (getX(), getY() - 3);
                }
                break;
            }
            default:
                if (getX() != 620)
                {
                    setLocation (getX() + 3, getY());
                }
                if (getX() == 620)
                {
                    setLocation (getX(), getY() + 3);
                }
                break;
        }
danpost danpost

2018/6/7

#
kevoncmlc2000 wrote...
i'm trying to make an electron actor complete a loop around a circuit. i used a switch statement with a default setting but it will not complete the last part because i only have one case stated so once the actor moves out of the y value it no longer moves in the desired direction. i know i can specify hundreds of cases but that's highly inconvenient. i also tried using a for loop in the range of my desired y coordinates but it says i must use a constant. any ideas on how to make this work correctly? << Code Omitted >>
You are right that stating each case would be ridiculous. You need something more generalized. What you need are fields for its states (speed, direction, and possibly others) or there may be some short-cuts you can make. Is the circuit that is to be made circular or elliptical (or just back and forth, as viewed)?
kevoncmlc2000 kevoncmlc2000

2018/6/7

#
it's a rectangular shape (made using pixels). the electron starts at the top and the default case covers the top and right sides of the loop. the case 532 covers the bottom side of the loop. now i just need to find a way for the actor to successfully travel up the left side and reenter the switch default case.
danpost danpost

2018/6/7

#
kevoncmlc2000 wrote...
it's a rectangular shape (made using pixels). the electron starts at the top and the default case covers the top and right sides of the loop. the case 532 covers the bottom side of the loop. now i just need to find a way for the actor to successfully travel up the left side and reenter the switch default case.
I was not talking about the image of the electron -- I was asking about the path that it is to take as it travels around. What shape does the path trace out?
kevoncmlc2000 kevoncmlc2000

2018/6/7

#
a rectangle.
danpost danpost

2018/6/7

#
kevoncmlc2000 wrote...
a rectangle.
Okay. Use the turn and move methods. Then you can use rotation in conjunction with location to determine when to turn.
public void move()
{
    move(3);
    if (getRotation() == 0 && getX() > 620 ||
        getRotation() == 90 && getY() > 532 ||
        getRotation() == 180 && getX() < 359 ||
        getRotation() == 270 && getY() < 69) turn(90);
}
You can adjust the coordinates for turning as needed.
kevoncmlc2000 kevoncmlc2000

2018/6/8

#
ok, i'll work with this. thanks so much
You need to login to post a reply.