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

2016/11/15

UP AND DOWN COMMAND

lolitsme lolitsme

2016/11/15

#
Hi guys! What codes can I use for my actor to move up and down continuously? thank you!
danpost danpost

2016/11/15

#
Use an int field to track the current direction ( '1' for moving down and '-1' for moving up). The field can be multiplied by the speed and added to the current y-coordinate to get the new y-coordinate of the actor. If an edge is encountered, multiply the field by '-1' to change its direction of motion.
lolitsme lolitsme

2016/11/16

#
do you have codes for that? if yes, thank you so much!
Super_Hippo Super_Hippo

2016/11/16

#
private int direction = 1;
public void act()
{
    setLocation(getX(), getY()+direction);
    if (isAtEdge()) direction *= -1;
}
You need to login to post a reply.