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

2013/12/6

Help! with switching Actor directions?

SpecialK705 SpecialK705

2013/12/6

#
As it is stated in several of the tutorials you can call random numbers to make an actor turn randomly or when it is at the world's edge. If I have an actor that moves horizontally only at the bottom of the word, is there someway to have the actor randomly switch from right to left without rotating? Any help would be very much appreciated!
Gevater_Tod4711 Gevater_Tod4711

2013/12/6

#
I think the easyest way would be to declare a variable for the movement and change it from positive to negative:
private int movement = 5;

public void act() {
    if (Greenfoot.getRandomNumber(100) == 0) {
        movement = -movement;
    }
    //I think changing the movement at the world edge would be good too:
    if (atWorldEdge()) {
        movement = -movement;
    }
    setLocation(getX() + movement, getY());
}
SpecialK705 SpecialK705

2013/12/7

#
Thank you so much! going to test it out and will post back shortly!
SpecialK705 SpecialK705

2013/12/7

#
Works perfectly, I can't thank you enough, I was going partially insane trying to figure this out!!
You need to login to post a reply.