I try to make a little game where u drive a Humvee and there are randomly spawned RocketLauncher's that aim at the Humvee and shoot rockets at you.
My point is now that i want the Rocketlauncher aim at the Humvee.
this is working but only when the 2 objects are touching eachother when they dont i get the error:
the code from my Humvee:
and the code from my RocketLauncher:
and my background code:
anyone can help me?
java.lang.NullPointerException at RocketLauncher.act(RocketLauncher.java:23) at greenfoot.core.Simulation.actActor(Simulation.java:583) at greenfoot.core.Simulation.runOneLoop(Simulation.java:541) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205)
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Humvee here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Humvee extends Actor
{
/**
* Act - do whatever the Humvee wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
// Add your action code here.
move(2);
if (Greenfoot.isKeyDown("left"))
{
turn(-2);
}
if (Greenfoot.isKeyDown("right"))
{
turn(2);
}
if (Greenfoot.isKeyDown("up"))
{
move(5);
}
checkSide();
}
public void checkSide()
{
if(atWorldEdge()){
World world;
world = getWorld();
world.removeObject(this);
}
}
public boolean atWorldEdge()
{
if(getX() < 10 || getX() > getWorld().getWidth() - 10)
return true;
if(getY() < 10 || getY() > getWorld().getHeight() - 10)
return true;
else
return false;
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class RocketLauncher here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class RocketLauncher extends Actor
{
/**
* Act - do whatever the RocketLauncher wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
// Add your action code here.
//Humvee humvee = (Humvee)Background.getObjects(Humvee.class).get(0);
//turnTowards(humvee.getX(), humvee.getY());
Actor Humvee = getOneIntersectingObject(Humvee.class);
int x = 5;
x = Humvee.getX();
int y = 5;
y = Humvee.getY();
turnTowards(x, y);
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Background here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Background extends World
{
/**
* Constructor for objects of class Background.
*
*/
public Background()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(800, 600, 1);
Humvee Humvee = new Humvee();
addObject(Humvee, 300, 200);
addLauncher();
//RocketLauncher.turnTowards(Humvee.getX(), Humvee.getY());
}
public void addLauncher()
{
int RLx;
int RLy;
RLx = Greenfoot.getRandomNumber(750+25);
RLy = Greenfoot.getRandomNumber(550+25);
RocketLauncher RL = new RocketLauncher();
addObject(RL, RLx, RLy);
}
}

