Hi I have a space fighter game where the protector would shoot through asteroids to defeat the enemy ships. My problem is I want to create a floating/vibrating effect for the asteroid movement where they move back and forth just by for example 5px.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 | private int xMiddle; private int direction = Greenfoot.getRandomNumber( 2 )* 2 - 1 ; //+1 or -1 private int speed = 1 ; //only needed if something else than 1 public void addedToWorld(World w) { xMiddle = getX(); } public void act() { setLocation(getX()+direction*speed); if (getX()<xMiddle- 4 || getX()>xMiddle+ 4 ) direction *= - 1 ; //change direction of movement } |
1 2 3 4 5 6 7 8 9 10 11 12 | private static int direction = Greenfoot.getRandomNumber( 2 )* 2 - 1 ; private int speed = 1 ; public void act() { setLocation(getX()+direction*speed, getY()); } public static void changeDirection() { direction *= - 1 ; } |
1 2 3 4 5 6 7 8 9 10 11 | //in world private int directionTimer = 5 ; public void act() { if (--directionTimer== 0 ) { Asteroid.changeDirection(); directionTimer= 10 ; } } |
1 2 3 4 5 6 7 8 9 | private int dirY = 1 ; private int distY = 0 ; public void act() { setLocation(getX(), getY()+dirY); distY = (distY+ 1 )% 5 ; if (distY == 0 ) dirY = -dirY; } |