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

2015/4/8

Need help with my healthbar :D

Yurey Yurey

2015/4/8

#
hey guys i have a lil problem with my code i try to make a healthbar here is the code from the Player(engel):
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Engel here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Engel extends Mover

{
    /**
     * Act - do whatever the Engel wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
       checkKeys();
       spawn();
    }
    int hp=((Healthbar)getWorld().getObjects(Healthbar.class).get(0)).hp--;
    private boolean move = true;
    private boolean spawned = true;
    int teufel=2;
       private void checkKeys()
    {
       String key = Greenfoot.getKey();
        if ("9".equals(key) && move){ moveUpperRight(); move = false; spawned = true;}
        if ("8".equals(key) && move){ moveUp(); move =false; spawned = true;}
        if ("7".equals(key) && move){ moveUpperLeft(); move =false; spawned = true;}       
        if ("6".equals(key) && move){ moveRight();move =false; spawned = true;}
        if ("4".equals(key) && move){ moveLeft();move =false; spawned = true;}
        if ("3".equals(key) && move){ moveUnderRight();move =false; spawned = true;}
        if ("2".equals(key) && move){ moveDown();move =false; spawned = true;}
        if ("1".equals(key) && move){ moveUnderLeft();move =false; spawned = true;}  
    }
    private void spawn()
    {
        if (teufel==30)
        {
            spawned= false;
        }
        if (!move)
        {
            if(Greenfoot.getRandomNumber(3)==0 && spawned)
            {
                getWorld().addObject(new Teufel(),Greenfoot.getRandomNumber(91),Greenfoot.getRandomNumber(71));
                teufel++;
            }    
            move = true;
            spawned = false;
        }
    }
}
here is the code from the healthbar:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Healthbar here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Healthbar extends Actor
{
    /**
     * Act - do whatever the Healthbar wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        Leben();// Add your action code here.
    } 
    public int hp=100;
    public int getHP()
    {
        return hp;
    }
    public void Leben()
    {
        if(hp>=150)
        {
            setImage("Lebensanzeige15.png");
        }
         if(hp>=140 && hp<150)
        {
            setImage("Lebensanzeige14.png");
        }
         if(hp>=130 && hp<140)
        {
            setImage("Lebensanzeige13.png");
        }
         if(hp>=120 && hp<130)
        {
            setImage("Lebensanzeige12.png");
        }
         if(hp>=110 && hp<120)
        {
            setImage("Lebensanzeige11.png");
        }
        if(hp>=100 && hp<110)
        {
            setImage("Lebensanzeige10.png");
        }
        if(hp>=90 && hp<100)
        {
            setImage("Lebensanzeige9.png");
        }
        if(hp>=80 && hp<90)
        {
            setImage("Lebensanzeige8.png");
        }
        if(hp>=70 && hp<80)
        {
            setImage("Lebensanzeige7.png");
        }
        if(hp>=60 && hp<70)
        {
            setImage("Lebensanzeige6.png");
        }
        if(hp>=50 && hp<60)
        {
            setImage("Lebensanzeige5.png");
        }
        if(hp>=40 && hp<50)
        {
            setImage("Lebensanzeige4.png");
        }
        if(hp>=30 && hp<40)
        {
            setImage("Lebensanzeige3.png");
        }
        if(hp>=20 && hp<30)
        {
            setImage("Lebensanzeige2.png");
        }
        if(hp>0 && hp<20)
        {
            setImage("Lebensanzeige1.png");
        }
        if(hp==0)
        {
            setImage("Lebensanzeige.png");
            Greenfoot.stop();
           // Greenfoot.playSound("    "); 
        }
    }
}
at last here is the code from my World (Heaven):
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Heaven here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Heaven extends World
{

    /**
     * Constructor for objects of class Heaven.
     * 
     */
    public Heaven()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(80, 60, 10); 
        populateWorld();
    }
    private void populateWorld()
    {
        addObject(new Teufel(),Greenfoot.getRandomNumber(80), Greenfoot.getRandomNumber(60));
        addObject(new Teufel(),Greenfoot.getRandomNumber(80), Greenfoot.getRandomNumber(60));
        addObject(new Engel(),40,30);
        addObject(new Healthbar(),8,1);
        addObject(new Telepilz(),Greenfoot.getRandomNumber(80), Greenfoot.getRandomNumber(60));
    }
}
and know here is the error by compilling the world wich is shown in the Terminal Window java.lang.NullPointerException at Engel.<init>(Engel.java:21) at Heaven.populateWorld(Heaven.java:26) at Heaven.<init>(Heaven.java:20) pls help my thank you all guys :D
danpost danpost

2015/4/8

#
Your fields (declared on lines 21 through 24 of the Engel class), are given to your Engel object while it is being created. If assigning values to those fields on the line they are being declared (as you do in those lines), the values are set immediately. You are calling 'getWorld' on the Engel object while it is still being created; which is before the object is placed into any world. This means that when 'getWorld' is executed here, it will return a 'null' value; and you cannot call a method on a null reference. You can assign the value to 'hp' when the Engel object is added into the world by overriding the 'addedToWorld(World)' method of the Actor class:
protected void addedToWorld(World world)
{
    hp = ((HealthBar)world.getObjects(HealthBar.class).get(0)).hp;
}
I removed the '--' after 'hp' at the end of the line because I did not think you wanted to change its value of the HealthBar object 'hp' field after assigning its original value to the Engel 'hp' field. Just like the 'act' method of the Actor class, the greenfoot framework calls the 'addedToWorld' method automatically; but only once, each time the object is added to a world.
Yurey Yurey

2015/4/8

#
thank you for ur help danpost but at first i dontknow is the code right know ? here is the code of the Actor(engel): import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Engel here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Engel extends Mover

{
    /**
     * Act - do whatever the Engel wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
       checkKeys();
       spawn();
    }
     protected void addedToWorld(World world)
    {
        int hp = ((Healthbar)world.getObjects(Healthbar.class).get(0)).hp;
    }

    //int hp=((Healthbar)getWorld().getObjects(Healthbar.class).get(0)).hp;
    private boolean move = true;
    private boolean spawned = true;
    int teufel=2;
       private void checkKeys()
    {
       String key = Greenfoot.getKey();
        if ("9".equals(key) && move){ moveUpperRight(); move = false; spawned = true;}
        if ("8".equals(key) && move){ moveUp(); move =false; spawned = true;}
        if ("7".equals(key) && move){ moveUpperLeft(); move =false; spawned = true;}       
        if ("6".equals(key) && move){ moveRight();move =false; spawned = true;}
        if ("4".equals(key) && move){ moveLeft();move =false; spawned = true;}
        if ("3".equals(key) && move){ moveUnderRight();move =false; spawned = true;}
        if ("2".equals(key) && move){ moveDown();move =false; spawned = true;}
        if ("1".equals(key) && move){ moveUnderLeft();move =false; spawned = true;}  
    }
    private void spawn()
    {
        if (teufel==30)
        {
            spawned= false;
        }
        if (!move)
        {
            if(Greenfoot.getRandomNumber(3)==0 && spawned)
            {
                getWorld().addObject(new Teufel(),Greenfoot.getRandomNumber(91),Greenfoot.getRandomNumber(71));
                teufel++;
            }    
            move = true;
            spawned = false;
        }
    }
}
at least it doesnt work ^^
danpost danpost

2015/4/8

#
You still need to declare the 'hp' field in the class. Change line 26 to this:
private int hp;
Then remove the 'int' at the beginning of line 23.
Yurey Yurey

2015/4/8

#
now i changed the line and deleted the int but it dont work anyways
danpost danpost

2015/4/8

#
Yurey wrote...
now i changed the line and deleted the int but it dont work anyways
'it don't work' does not tell me anything as far as what is occurring and what you want to occur.
Yurey Yurey

2015/4/8

#
i got the same error report java.lang.NullPointerException at Engel.<init>(Engel.java:21) at Heaven.populateWorld(Heaven.java:26) at Heaven.<init>(Heaven.java:20) and a new one java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at java.util.ArrayList.rangeCheck(ArrayList.java:635) at java.util.ArrayList.get(ArrayList.java:411) at Engel.addedToWorld(Engel.java:56) at greenfoot.World.addObject(World.java:410) at Heaven.populateWorld(Heaven.java:26) at Heaven.<init>(Heaven.java:20)
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Engel here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Engel extends Mover

{
    /**
     * Act - do whatever the Engel wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
       checkKeys();
       spawn();
    }
     protected void addedToWorld(World world)
    {
        hp = ((Healthbar)world.getObjects(Healthbar.class).get(0)).hp;
    }

    private int hp;
    private boolean move = true;
    private boolean spawned = true;
    int teufel=2;
       private void checkKeys()
    {
       String key = Greenfoot.getKey();
        if ("9".equals(key) && move){ moveUpperRight(); move = false; spawned = true;}
        if ("8".equals(key) && move){ moveUp(); move =false; spawned = true;}
        if ("7".equals(key) && move){ moveUpperLeft(); move =false; spawned = true;}       
        if ("6".equals(key) && move){ moveRight();move =false; spawned = true;}
        if ("4".equals(key) && move){ moveLeft();move =false; spawned = true;}
        if ("3".equals(key) && move){ moveUnderRight();move =false; spawned = true;}
        if ("2".equals(key) && move){ moveDown();move =false; spawned = true;}
        if ("1".equals(key) && move){ moveUnderLeft();move =false; spawned = true;}  
    }
    private void spawn()
    {
        if (teufel==30)
        {
            spawned= false;
        }
        if (!move)
        {
            if(Greenfoot.getRandomNumber(3)==0 && spawned)
            {
                getWorld().addObject(new Teufel(),Greenfoot.getRandomNumber(91),Greenfoot.getRandomNumber(71));
                teufel++;
            }    
            move = true;
            spawned = false;
        }
    }
}
thats the code and only the new error report changed
danpost danpost

2015/4/8

#
Ok. Make sure that you create and add the HealthBar object into the world before you add the Engel object into the world (switch lines 26 and 27 in the Heaven class).
Yurey Yurey

2015/4/8

#
thank you for the help and excuse my low programming and english skill ^^ it works know u are great :D
You need to login to post a reply.