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

2012/9/17

Can anyone provide me a short code for a turtle actor moving in a zigzag motion?

marbille9 marbille9

2012/9/17

#
It doesn't have to be complex, it only has to be simply about a turtle moving in short zigzag motion. I need it right now, so if anyone out there is generous enough to do it for me, please do. Thanks in advance.
erdelf erdelf

2012/9/17

#
put in actor class. not tested:
setRotation(45);
int k = 20;
while(k != 0) 
{
    move(1);
   Greenfoot.delay(5);
    k++;
}
turn(90);
k = 20;
while(k != 0) 
{
    move(1);
    Greenfoot.delay(5);
    k++;
}
davmac davmac

2012/9/17

#
It's certainly not tested! I can see one bug already (same bug occurs twice in the code). Also, Greenfoot.delay(...) will delay the whole scenario, not just the one actor, so it's often better to find another way.
groengeel groengeel

2012/9/17

#
Try adding a counter which adds one up every act, then choose on which round you'd like the zig (or the zag) to go. Like this:
public class Crab extends Actor {
    private int zCounter;
    public Crab() {
        zCounter = 0;
    }
    
    /** 
     * Act - do whatever the crab wants to do. This method is called whenever
     *  the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act()
    {
        zCounter += 1;
        if ( zCounter == 10 ) 
        {
            setRotation(45);
        } else if ( zCounter == 20)
        {
            setRotation(-45);
            zCounter = 0;
        }

        move(5);
    }
}
You need to login to post a reply.