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

2019/12/6

automatically move an actor

svenfrijters svenfrijters

2019/12/6

#
I want to move an actor by not hitting anything. it has to automatically move 2 cells to the right then 2 cells to the left
public void act() 
    {
        move(1);
        setRotation(180);
       // Add your action code here.
    }   
My code can make it turn 1 time but i want it to do it over and over after moving 2 cells
danpost danpost

2019/12/6

#
Using turn(180) instead of setRotation(180) will make it repeat. To go 2 cells instead of 1 may require an int counter. Being you only want it to move 2 cells, a boolean may work just as well. Even a simple:
if (getX() % 2 == 0) turn(180);
may even work (without a field at all).
You need to login to post a reply.