My enemy is supposed to wait before each shot but for some reason it doesn't, here is the code for my enemy, shooting is in the shoot method.
import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)
import java.util.List;
public class Key extends Actor
{
private int wait = 0;
private int shootWait = 0;
private final int waitTime = 35;
private final int shootWaitTime = 40;
private boolean touchingPlayer = false;
private boolean canShoot = false;
/**
* Create a new key.
*/
public void act()
{
checkShootingStatus();
checkTouchingStatus();
hitPlayer();
die();
if (getWorld() == null)return;
movementAi();
shoot();
}
public void die()
{
menu menuWorld = (menu) getWorld();
Bar scoreBar = menuWorld.getScoreBarValue();
if (getHealth() <= 0)
{
scoreBar.add(50);
getWorld().removeObject(this);
}
}
private void checkTouchingStatus()
{
if (wait > 0) wait--;
if (wait == 0) touchingPlayer = false;
}
private void checkShootingStatus()
{
if (wait > 0) shootWait--;
if (wait == 0) canShoot = false;
}
//movement
public void movementAi()
{
move(2);
if(Greenfoot.getRandomNumber(100) < 10)
{
turn(Greenfoot.getRandomNumber(90));
}
if (getX() <= 5 || getX() >= getWorld().getWidth() - 5)
{
turn(180);
}
if (getY() <= 5 || getY() >= getWorld().getHeight() - 5)
{
turn(180);
}
}
//move to player
//Enemy health
private int health = 3;//or any other starting value;
public void setHealth(int points) {
health += points;
}
public int getHealth() {
return health;
}
public void hitPlayer() {
key2 key2 = (key2) getOneObjectAtOffset(0, 0, key2.class);
if (key2 != null && !touchingPlayer) {
touchingPlayer = true;
wait += waitTime;
menu menuWorld = (menu) getWorld(); //Do this every time to call method!!!
Bar healthBar = menuWorld.getHealthBarValue();
healthBar.subtract(1);//decrements the health of your player
}
}
//shoot
public void shoot()
{
List key2 = getObjectsInRange(200, key2.class);
if (! key2.isEmpty() && !canShoot)
{
canShoot = false;
shootWait += shootWaitTime;
menu menuWorld = (menu) getWorld();
key2 player = menuWorld.getPlayerPos();
enemyBullet enemyBullet = new enemyBullet();
getWorld().addObject(enemyBullet, getX(), getY());
enemyBullet.turnTowards(player.getX(), player.getY());
}
}
}
