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

2014/12/9

Enemy move certain distance on X axis

mullac51 mullac51

2014/12/9

#
I want to make my enemy move a certain distance on the X axis and when it has reached that distance I want it to turn 180 degrees and go back along the X axis. Basically i want the enemy to move from left to right on the X axis continously. how do i go about doing this?
NikZ NikZ

2014/12/9

#
setLocation() should work for you. To detect your X, use getX().
setLocation(getX() + x, getY());
if (getX() == i || getX() == j) {
    x = -x;
}
Replacing i with your far left value and j with your far right value. Or since you want it to turn, you might want move()
move(x);
if (getX() == i || getX() == j) {
    setRotation(getRotation() + 180);
}
Replacing i with your far left value and j with your far right value.
mullac51 mullac51

2014/12/9

#
Thank you very much! I was thinking it was much more complicated than that!
You need to login to post a reply.