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.
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
}
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;
}//in world
private int directionTimer = 5;
public void act()
{
if (--directionTimer==0)
{
Asteroid.changeDirection();
directionTimer=10;
}
}private int dirY = 1;
private int distY = 0;
public void act()
{
setLocation(getX(), getY()+dirY);
distY = (distY+1)%5;
if (distY == 0) dirY = -dirY;
}