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

2014/4/30

HealthBar not losing health.

1
2
Than4tos Than4tos

2014/4/30

#
This is the healthBar code.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;

public class healthBar extends Chars
{      
    int health = 4;
    int healthBarWidth = 80;
    int healthBarHeight = 15;
    int pixelsPerHealthPoint = (int)healthBarWidth/health;
    
    public healthBar()
    {
        update();
    }
    
    public void act() 
    {
        update();
    } 
    
    public void update()
    {
        setImage(new GreenfootImage(healthBarWidth +2, healthBarHeight +2));
        GreenfootImage image = getImage();
        image.setColor(Color.WHITE);
        image.drawRect(0 , 0 , healthBarWidth + 1, healthBarHeight + 1);
        image.setColor(Color.RED);
        image.fillRect(1,1,health*pixelsPerHealthPoint, healthBarHeight);
        
    }
    
    public void loseHealth()
    {
        health--;
    }
}
This Part is on the Player Class.
boolean touchingEnemyAi = false;

    public void hitBanana()
    {
        Actor banana = getOneIntersectingObject(EnemyAi.class);
        if(banana != null)
        {
            World myWorld = getWorld();
            Cave cave = (Cave)myWorld;
            healthBar HP = cave.gethealthBar();
            if(touchingEnemyAi == false)
            {
                HP.loseHealth();
                touchingEnemyAi = true;
                if(HP.health <= 0)
                {
                    myWorld.removeObject(this);
                }
            }
        }
        else
        {
            touchingEnemyAi = false;
        }
    }
And i almost forgot but on my world i got
healthBar HP = new healthBar();
    public healthBar gethealthBar()
    {
        return HP;
    }
The problem here is when the enemy touches the player nothing happens health does not go down and i can't really figure it out why. i thing i got all the relevent pieces of code pasted here.
Than4tos Than4tos

2014/4/30

#
Sorry for the big wall of text
Than4tos Than4tos

2014/4/30

#
anybody? :/
danpost danpost

2014/4/30

#
In your Cave (world) class, what are you using to add the healthbar into the world?
Than4tos Than4tos

2014/4/30

#
i used the Save World option to test it out.
danpost danpost

2014/4/30

#
danpost wrote...
In your Cave (world) class, what are you using to add the healthbar into the world?
bump.
Than4tos Than4tos

2014/4/30

#
Yep managed to find the problem was declaring 2 bars one on the start another abit lower how the hell did i miss it before :(
Than4tos Than4tos

2014/5/1

#
quick question if i got 2 Worlds how can i check in wich world i currently am in?
danpost danpost

2014/5/1

#
Than4tos wrote...
quick question if i got 2 Worlds how can i check in wich world i currently am in?
if (getWorld() instanceof Level1) { /** some code */ }
if (getWorld() instanceof Level2) { /** some code */ }
Than4tos Than4tos

2014/5/1

#
        if(getWorld().getObjects(Player.class).size() != 0  )
        {   
            xPlayer = getX();
            yPlayer = getY();
        }

that's a way to check if one especific actor is still in the world ? or am i missing something?
danpost danpost

2014/5/1

#
Than4tos wrote...
< Code Omitted > that's a way to check if one especific actor is still in the world ? or am i missing something?
I will have to presume that you mean 'a specific class of actor'. Nowhere are you getting a reference to the player -- to get the x and y location coordinate from.
Than4tos Than4tos

2014/5/1

#
 public static int xPlayer;
public static int yPlayer;
then that bit that i posted so i can call it here from another actor
 public void followPlayer()
    {  turnTowards(Player.xPlayer , Player.yPlayer);
       move(1);
       setRotation(0);
}
Than4tos Than4tos

2014/5/1

#
put im getting null pointers
danpost danpost

2014/5/1

#
What code is causing the nullpoiners? (or copy/paste the entire error message).
Than4tos Than4tos

2014/5/1

#
java.lang.NullPointerException
	at Player.playerCord(Player.java:173)
	at Player.act(Player.java:166)
	at greenfoot.core.Simulation.actActor(Simulation.java:568)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:526)
	at greenfoot.core.Simulation.runContent(Simulation.java:215)
	at greenfoot.core.Simulation.run(Simulation.java:205)
i thing it's because im trying to still call the coords of the player when i remove him .
There are more replies on the next page.
1
2