I am making a shooter kind of game, and I have monsters shooting bullets towards the players... However, greenfoot never seems to run my constructor and the world for mBullet always ends up as null I have no idea why it isn't working.
This is the code for my mBullet class
and here's the code for my monster class at the moment
If any of you guys can help out it'd be a major life saver.
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.*;
/**
* The bullet of monsters
*
* @mreed7231
*
*/
public class mBullet extends SmoothMover
{
private World bulletWorld;
private int timer;
private int done = 0;
int playerX;
int playerY;
public mBullet(World shooterWorld)
{
System.out.println("KITTEN");
bulletWorld = shooterWorld;
System.out.println("**"+bulletWorld);
}
{
System.out.println("////"+bulletWorld);
List Players = bulletWorld.getObjects(Player.class);
if(Players.size() != 0)
{
Actor player = (Actor)bulletWorld.getObjects(Player.class).get(0);
playerX = player.getX();
playerY = player.getY();
}
if (done == 0)
{
turnTowards(playerX, playerY);
done++;
}
move(0.5);
if (timer == 0)
timer = 600;
if (timer > 0)
{
timer--;
if ((this != null) && isAtEdge())
{
getWorld().removeObject(this);
}
else if (timer == 0 && (this != null))
{
getWorld().removeObject(this);
}
}
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
*
*
* @mreed7231
*/
public class Monster extends SmoothMover
{
private static int hp;
private static int lvl;
private static int xp;
private static int dex;
public Monster()
{
hp = 300;
lvl = 5;
dex = 100;
xp = 0;
}
private double time;
private double time2;
private double time3;
//For Motion
private GreenfootImage left = new GreenfootImage("MonsterOtherSide.gif");
private GreenfootImage right= new GreenfootImage("MonsterSide.gif");
private GreenfootImage up= new GreenfootImage("MonsterBack.gif");
private GreenfootImage down= new GreenfootImage("MonsterForward.gif");
//For Dexterity
long lastAddedDex = System.currentTimeMillis();
boolean inRange = false;
Class Player;
public void act()
{
if (this.getObjectsInRange(15, Player) != null)
inRange = true;
if (inRange)
{
long curTime = System.currentTimeMillis();
double waitTime = (60/(double)dex) * 1000;
if (curTime >= (lastAddedDex + waitTime))
{
shoot();
lastAddedDex = curTime;
}
}
}
public static void changehp(int value)
{
hp = hp + value;
}
public void shoot()
{
System.out.println(":-)"+getWorld()+" " +getX()+" " +getY());
getWorld().addObject(new mBullet(getWorld()), getX(), getY());
}
public void getsHit()
{
changehp(-25);
}
public static int gethp()
{
return hp;
}
public static int getLvl()
{
return lvl;
}
}

