2018/12/5

## How can a Enemy shoot randomly?

2018/12/5

I want that Bowser shoots to the left side randomly, but how do I make this? His coordinates are (x = 700, y = 200). PLEASE HELP ME!
danpost

2018/12/5

I want that Bowser shoots to the left side randomly, but how do I make this? His coordinates are (x = 700, y = 200). PLEASE HELP ME!
Add a timer field to the Bowser class. Choose two values that you want to limit the delay between shots -- one for the minimum delay and one for the maximum delay. The difference between these two values is the range. Set the timer to the sum of the minimum value and a random value in the range. Decrement the timer each act. When it becomes zero, shoot and reset the timer as before.

2018/12/6

danpost wrote...
I want that Bowser shoots to the left side randomly, but how do I make this? His coordinates are (x = 700, y = 200). PLEASE HELP ME!
Add a timer field to the Bowser class. Choose two values that you want to limit the delay between shots -- one for the minimum delay and one for the maximum delay. The difference between these two values is the range. Set the timer to the sum of the minimum value and a random value in the range. Decrement the timer each act. When it becomes zero, shoot and reset the timer as before.
i got this:
```import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
* Write a description of class Bowser here.
*
* @version (a version number or a date)
*/
public class Bowser extends Actor
{   private GifImage g = new GifImage("Bowserreadytofightleft.gif");
private GifImage f = new GifImage("Bowserstarving.gif");
private int minShotDelay = 40;
private int maxShotDelay = 160;
private int shotTimer = minShotDelay;
/**
* Act - do whatever the Bowser wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
standard();
hitted();
shootRandomly();
if (shotTimer == 0)
{
shotTimer = minShotDelay+Greenfoot.getRandomNumber(1+maxShotDelay-minShotDelay);
}
else shotTimer--;

}
public void standard()
{if(!isTouching(Fireball.class))
{
this.setImage(g.getCurrentImage());
}
}
public void hitted()
{if(isTouching(Fireball.class))
{
this.setImage(f.getCurrentImage());
HealthBar.health--;
}

}

public void shootRandomly()
{
Fireballs fireballs = new Fireballs();
}

}```

2018/12/6

danpost wrote...
I want that Bowser shoots to the left side randomly, but how do I make this? His coordinates are (x = 700, y = 200). PLEASE HELP ME!
Add a timer field to the Bowser class. Choose two values that you want to limit the delay between shots -- one for the minimum delay and one for the maximum delay. The difference between these two values is the range. Set the timer to the sum of the minimum value and a random value in the range. Decrement the timer each act. When it becomes zero, shoot and reset the timer as before.
i got this:
```import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
* Write a description of class Bowser here.
*
* @version (a version number or a date)
*/
public class Bowser extends Actor
{   private GifImage g = new GifImage("Bowserreadytofightleft.gif");
private GifImage f = new GifImage("Bowserstarving.gif");
private int minShotDelay = 40;
private int maxShotDelay = 160;
private int shotTimer = minShotDelay;
/**
* Act - do whatever the Bowser wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
standard();
hitted();
shootRandomly();
if (shotTimer == 0)
{
shotTimer = minShotDelay+Greenfoot.getRandomNumber(1+maxShotDelay-minShotDelay);
}
else shotTimer--;

}
public void standard()
{if(!isTouching(Fireball.class))
{
this.setImage(g.getCurrentImage());
}
}
public void hitted()
{if(isTouching(Fireball.class))
{
this.setImage(f.getCurrentImage());
HealthBar.health--;
}

}

public void shootRandomly()
{
Fireballs fireballs = new Fireballs();
}

}```
But it does nothing?!
danpost

2018/12/6