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

2019/1/9

Health In Game Problem

ErnieBoi ErnieBoi

2019/1/9

#

Ernie is back hello

when health run out, error of the null pointer exception. this means i need help as i am not knowing what is wrong java.lang.NullPointerException at Movers.removeHealth2(Movers.java:88) at Champion.act(Champion.java:29) at greenfoot.core.Simulation.actActor(Simulation.java:604) at greenfoot.core.Simulation.runOneLoop(Simulation.java:562) at greenfoot.core.Simulation.runContent(Simulation.java:221) at greenfoot.core.Simulation.run(Simulation.java:211)
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Movers here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Movers extends Actor
{
    /**
     * Test if we are close to one of the edges of the world. Return true is we are.
     */
    public boolean atWorldEdge()
    {
        if(getX() < 10 || getX() > getWorld().getWidth() - 10)
            return true;
        if(getY() < 10 || getY() > getWorld().getHeight() - 10)
            return true;
        else
            return false;
    }

    /**
     * Return true if we can see an object of class 'clss' right where we are. 
     * False if there is no such object here.
     */
    public boolean canSee(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0, 0, clss);
        return actor != null;        
    }

    /**
     * Try to eat an object of class 'clss'. This is only successful if there
     * is such an object where we currently are. Otherwise this method does
     * nothing.
     */
    public void eat(Class clss)
    {
        Actor actor = getOneObjectAtOffset(0, 0, clss);
        if(actor != null) {
            getWorld().removeObject(actor);
        }
    }
    
     public void deadChampion()
    {
        if (this.getWorld().getClass() == PlayArea.class)
        {
            PlayArea world = getWorldOfType(PlayArea.class);
            if(world.getHealth()<=0)
            {
                getWorld().removeObject(this);
                Greenfoot.setWorld(new GameOver());
            }
        }
    }

    public void deadChampion2()
    {
        if (this.getWorld().getClass() == BossBattle.class)
        {
            BossBattle world = getWorldOfType(BossBattle.class);
            if(world.getHealth()<=0)
            {
                getWorld().removeObject(this);
                Greenfoot.setWorld(new GameOver());
            }
        }
    }

    public void removeHealth()
    {
        if (this.getWorld().getClass() == PlayArea.class)
        {
            if(this.isTouching(Monsters.class))
            {
                PlayArea world = getWorldOfType(PlayArea.class);
                world.removeHealth();//removes players health

            }
        }
    }

    public void removeHealth2()
    {
        if (this.getWorld().getClass() == BossBattle.class)
        {
            if(this.canSee(Monsters.class))
            {
                BossBattle world = getWorldOfType(BossBattle.class);
                world.removeHealth2();//removes players health

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

/**
 * Write a description of class Champion here.
 * 
 * 
 * @author Eli
 * @version v.1.0
 */
public class Champion extends People
{
    public int health = 100;
    int x;
    int y;
    int firecount;

    public void act()
    {
        checkKeys();
        firecount++;
        if(firecount > 5)
            if(Greenfoot.isKeyDown("space"))
            {
                getWorld().addObject(new Bullet(getRotation()), getX(), getY());
                firecount = 4;
            }
        removeHealth();
        deadChampion();
        removeHealth2();
        deadChampion2();

    }

    /**
     * Check whether the control keys are being pressed, and turn
     * if they are.
     */
    public void checkKeys()
    {
        //these events make this program an event driven application.
        if ( Greenfoot.isKeyDown("up") )//trigger function
        {
            move(8);//event handler
        }
        if ( Greenfoot.isKeyDown("down") )//trigger function
        {
            move(-5);//event handler
        }
        if ( Greenfoot.isKeyDown("left") )//trigger function
        {
            turn(-5);//event handler
        }
        if ( Greenfoot.isKeyDown("right") )//trigger function
        {
            turn(5);//event handler
        }
    }


    

}
danpostsuperfan01 danpostsuperfan01

2019/1/9

#
HELLO ERNIE I AM DAN POST NO. 1 FAN!
ErnieBoi ErnieBoi

2019/1/9

#

DANPOST I SUMMON THEEEEEEEE

Zamoht Zamoht

2019/1/9

#
When you call removeHealth() in the act method after the champion is dead it has been removed from the world which leads to a null pointer when calling getWorld(). Try to change the code of your act method to:
    public void act()
    {
        checkKeys();
        firecount++;
        if(firecount > 5)
            if(Greenfoot.isKeyDown("space"))
            {
                getWorld().addObject(new Bullet(getRotation()), getX(), getY());
                firecount = 4;
            }
        removeHealth();
        removeHealth2();
        deadChampion();
        deadChampion2();
 
    }
and the deadChampion code to
    public void deadChampion()
    {
        if (this.getWorld() != null && this.getWorld().getClass() == PlayArea.class)
        {
            PlayArea world = getWorldOfType(PlayArea.class);
            if(world.getHealth()<=0)
            {
                getWorld().removeObject(this);
                Greenfoot.setWorld(new GameOver());
            }
        }
    }
    public void deadChampion2()
    {
        if (this.getWorld() != null && this.getWorld().getClass() == BossBattle.class)
        {
            BossBattle world = getWorldOfType(BossBattle.class);
            if(world.getHealth()<=0)
            {
                getWorld().removeObject(this);
                Greenfoot.setWorld(new GameOver());
            }
        }
    }
ErnieBoi ErnieBoi

2019/1/10

#
Zamoht wrote...
When you call removeHealth() in the act method after the champion is dead it has been removed from the world which leads to a null pointer when calling getWorld(). Try to change the code of your act method to:
    public void act()
    {
        checkKeys();
        firecount++;
        if(firecount > 5)
            if(Greenfoot.isKeyDown("space"))
            {
                getWorld().addObject(new Bullet(getRotation()), getX(), getY());
                firecount = 4;
            }
        removeHealth();
        removeHealth2();
        deadChampion();
        deadChampion2();
 
    }
and the deadChampion code to
    public void deadChampion()
    {
        if (this.getWorld() != null && this.getWorld().getClass() == PlayArea.class)
        {
            PlayArea world = getWorldOfType(PlayArea.class);
            if(world.getHealth()<=0)
            {
                getWorld().removeObject(this);
                Greenfoot.setWorld(new GameOver());
            }
        }
    }
    public void deadChampion2()
    {
        if (this.getWorld() != null && this.getWorld().getClass() == BossBattle.class)
        {
            BossBattle world = getWorldOfType(BossBattle.class);
            if(world.getHealth()<=0)
            {
                getWorld().removeObject(this);
                Greenfoot.setWorld(new GameOver());
            }
        }
    }
THnaks you it worke
You need to login to post a reply.