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

2015/12/14

Move Up and Down

ironphoenix20 ironphoenix20

2015/12/14

#
What is the code for moving up and down? Move(1) and move(-1) is right and left? What is up and down?
keizerbob keizerbob

2015/12/16

#
I don't yet have a running Greenfoot installation, but I have this suggestion: move(x) applies to whatever direction the object is facing. Initial placement of object in World (example: "turtle" in video) sets direction of motion for move() command. To move backwards, use negative argument. To move in a different direction apply a turn() command before the move().
danpost danpost

2015/12/17

#
ironphoenix20 wrote...
What is the code for moving up and down? Move(1) and move(-1) is right and left? What is up and down?
You could 'turn(90)' before moving and 'turn(-90)' after moving, adjusting the direction the actor is facing for the move and returning to original orientation afterwards. Alternatively, you could (as this applies to horizontal and vertical movement) use the 'setLocation' method. The following four lines (not to be used together as shown) shows moving in the given direction one (1) pixel at a time:
1
2
3
4
5
6
7
setLocation(getX()+1, getY()); // right
//
setLocation(getX()-1, getY()); // left
//
setLocation(getX(), getY()+1); // down
//
setLocation(getX(), getY()-1); // up
You need to login to post a reply.