I'm making a game and i want to make it so that the enemy is on a platform and the enemy moves from one side of the platform to the other and back again. i also want it so the the enemy is continuously going back and forth.


1 2 3 4 5 6 7 8 9 10 11 12 | // field int dir = 1 ; // movement code move(dir); // can be multiplied by a speed value // possible turning about code setLocation(getX(), getY()+ 2 ); Actor platform = getOneIntersectingObject(Platform. class ); setLocation(getX(), getY()- 2 ); int pX = platform.getX(); int pW = platform.getImage().getWidth(); if ((dir < 0 && pX-pW/ 2 >= getX()) || (dir > 0 && pX+pW/ 2 <= getX())) dir = -dir; |
1 2 3 4 5 6 | if ((dir < 0 && pX-pW/ 2 >= getX()) || (dir > 0 && pX+pW/ 2 <= getX())) { dir = -dir; setImage( "enemy" + dir + ".png" ) //Replace .png with whatever format your image is saved as. } |
1 2 3 4 5 | if ((dir < 0 && pX-pW/ 2 >= getX()) || (dir > 0 && pX+pW/ 2 <= getX())) { dir = -dir; getImage().mirrorHorizontally(); } |