This site requires JavaScript, please enable it in your browser!
Greenfoot back
UNDEAD_DC
UNDEAD_DC wrote ...

2014/11/25

Let an object face another object not working

UNDEAD_DC UNDEAD_DC

2014/11/25

#
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:
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)
the code from my Humvee:
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;    
    }    
}
and the code from my RocketLauncher:
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); 
        
    }    
}
and my background code:
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);
    }
}
anyone can help me?
danpost danpost

2014/11/25

#
You can only get the location, using 'getX' in line 23 of RocketLauncher, of a Humvee if the rocketlauncher intersects a Humvee. If it does not, then the value of 'Humvee will be null and you cannot execute any method of a null value. To avoid the error, make sure that the variable Humvee is not null before trying to run a method on it. From what it looks like, however, you are trying to make the rocket launcher turn toward an intersecting object. That, in itself, does not make much sense. Maybe you should be using a different method for getting a reference to the Humvee instead of 'getOneIntersectingObject'.
UNDEAD_DC UNDEAD_DC

2014/11/25

#
I tried doing that but cant find out with what i should replace it. I now do understand why my code did not work thanks for that.
danpost danpost

2014/11/25

#
UNDEAD_DC wrote...
I tried doing that but cant find out with what i should replace it. I now do understand why my code did not work thanks for that.
If you only have one Humvee object in the world, you can do this:
Humvee Humvee = (Humvee)getWorld().getObjects(Humvee.class).get(0);
If it is possible that no Humvee object is in the world, you must make sure that 'getObjects' does not return an empty list before using the 'get' method on it.
UNDEAD_DC UNDEAD_DC

2014/11/25

#
I first go and try another idea i just got: make the turrets have a range and when they dont have the Humvee inside that range they go into "scan mode" wich makes em spin around useless. And yes there will only be one Humvee in the field at the time and the idea was that the Humvee will disapear when hitting the walls wich makes you have to keep playing but il try that bit of code Thanks
UNDEAD_DC UNDEAD_DC

2014/11/25

#
Yes your code worked. Made it so that when the Humvee hits the side now it gets "teleported" to the other side of the screen. Thank you so much for helping
You need to login to post a reply.