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

2012/12/15

So near and yet so far!

Terrosion Terrosion

2012/12/15

#
After spending ages trying to sort out a healthbar, which I thought was fixed and then this error came up: java.lang.NullPointerException at Autodread.enemyCollision(Autodread.java:77) at Autodread.act(Autodread.java:16) The source of this error is:
public void enemyCollision()
    {
        Actor enemy = getOneIntersectingObject(Enemies.class);
        if (enemy != null)
        {
            getWorld().removeObject(getOneIntersectingObject(Enemies.class));
            ScrollWorld world = (ScrollWorld) getWorld();
            HealthBar Health = world.getHealth();
            Health.setImage(-1);
        }
    }
Any tips?
danpost danpost

2012/12/15

#
It would help to know which line erred. It is (was) line 77 in the Autodread class, but we cannot tell from what you posted which line that was. I am inclined to think that the variable whose value is being returned by 'getHealth' in your 'ScrollWorld' class either has not been set or has been reset back to 'null'.
Terrosion Terrosion

2012/12/15

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Autodread here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Autodread extends Actor
{
    public int can_shoot = 0;
    public void act() 
    {
        Move();
        Shoot();
        enemyCollision();
    }
    private void Shoot()
    {
        if (Greenfoot.isKeyDown("space"))
        {
            if (can_shoot ==0) 
            {
                getWorld().addObject(new Autoround(), getX()+20, getY()+24);
                getWorld().addObject(new Autoround(), getX()+20, getY()-24);
                can_shoot = 25;
            }
        }
        if (Greenfoot.isKeyDown("esc"))
        {
            Greenfoot.stop();
        }
        if (can_shoot > 0)
        {
            can_shoot=can_shoot-1;
        }
    }
    public void Move()
    {
        if (Greenfoot.isKeyDown("right"))
        {
            if (getX()<670)
            {
                move(4);
            }
        }
        if (Greenfoot.isKeyDown("left"))
        {
            if (getX()>30)
            {
                move(-4);
            }
        }
        if (Greenfoot.isKeyDown("up"))
        {
            if (getY()>110)
            {
                setLocation(getX(), getY()-4);
            }
        }
        if (Greenfoot.isKeyDown("down"))
        {
            if (getY()<470)
            {
                setLocation(getX(), getY()+4);
            }
        }
    }
    public void enemyCollision()
    {
        Actor enemy = getOneIntersectingObject(Enemies.class);
        if (enemy != null)
        {
            getWorld().removeObject(getOneIntersectingObject(Enemies.class));
            ScrollWorld world = (ScrollWorld) getWorld();
            HealthBar Health = world.getHealth();
            Health.setImage(-1);
        }
    }
}
Here the code is in its entirety.
danpost danpost

2012/12/15

#
Well, the error did occur on the line I though it would; but, that means your problem is in the 'ScrollWorld' class (not this one).
Terrosion Terrosion

2012/12/15

#
Another revalation (Damn lack of editing) When I right click on the World and choose 'HealthBar getHealth' (The actor and variable I'm referencing) It returns 'null'. How would I fix this?
danpost danpost

2012/12/15

#
Unless you have a reference to the HealthBar object in your world class, your getHealth() method should look something like the following:
public HealthBar getHealth()
{
    if(getObjects(HealthBar.class).isEmpty()) return null;
    return (HealthBar) getObjects(HealthBar.class).get(0);
}
Duta Duta

2012/12/16

#
An alternative to danpost's code is:
public class GameWorld extends World {
	private HealthBar health;

	public GameWorld() {
		super(600, 400, 1);
		health = new HealthBar();
		addObject(health, 0, 0);
	}

	public HealthBar getHealth() {
		return health;
	}
}
You need to login to post a reply.