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

2012/5/23

Platforms which move up and down?

dandefender2 dandefender2

2012/5/23

#
hi i have already got the code for the platform to move down but am not sure how to make it go up again... the code i already have is as follows: public void act() { setLocation(getX(), getY() + 4); }
trash1000 trash1000

2012/5/23

#
Well, you have to decide when you want the platform to move up again. I'd say the easiest way is to use a boolean variable - if it's true you move down, if it's false you move up.
private boolean down = true;

public void act() {
    if(getY() >= getWorld().getHeight()) {
        down = false;
    }
    else if(getY() <= 0) {
        down = true;
    }
    if(down) {
        setLocation(getX(), getY()+4);
    }
    else {
        setLocation (getX(), getY()-4);  
    }
}
This will cause the actor to constantly move up and down from the top world edge to the bottom world edge (change the first two if statements to change the movement borders.)
You need to login to post a reply.