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

2012/12/17

Health bars, using Progress bar/Health bar class by Danpost

Johnathon Johnathon

2012/12/17

#
So, I set the bars up to my liking, including a player 1 and player 2 bars both having 10,10 health. When a player presses the Space bar button a missile is created, or the num0 key a player two missile is created. These missiles fly forward until colliding with the edge of the map or with the other player. On collision with another player or edge the missile is removed. how do I make it so when the missile collides with player one health is taken off the health bar?
//This is in my world
 public health1 bar = new health1("Player 1", "Health Points", 10, 10);

//Along with this in the constructor of the world
        addObject (bar, 115, 25);
and this is the collision part of the rocket
  {
        move(-5);
        Actor collided = getOneObjectAtOffset(-40, 0, Player1.class);
        if(collided != null)
        {
            //The line where I expect the health subration to go?
            getWorld().removeObject(this);
        }
    }  
Johnathon Johnathon

2012/12/17

#
never mind! already fixed it. Was a really bad question ha. If anyone has trouble with this, simply make the bar static in the world, then when you get to the colision part, call it, to subtract one. For example, my world is Space2, so the line where I put wheere I expect the subtraction to go put Space2.bar.subtract(1);
danpost danpost

2012/12/17

#
You have the correct position for the statement. The statement should be:
bar.setValue(bar.getValue()-1);
// or
bar.add(-1);
// or
bar.subtract(1);
after line 7, you probably want something like the following
if(bar.getValue()<=0) 
{
    // game over code
}
Johnathon Johnathon

2012/12/17

#
Yeah, I got it! Thanks though. Another quick question, my missiles now cause the game to crash if colliding with a player. It crashes because the class calls for another if test to happen after its removal, because of the multiple ways of removing it. How could I resolve this error? Heres the code.
public class missile2 extends Player2
{
    missile2()
    {
    getImage().mirrorHorizontally();
    }
    
    public void act() 
    {
        move(-5);
        Actor collided = getOneObjectAtOffset(-40, 0, Player1.class);
        if(collided != null)
        {
            Space2.bar.subtract(1);
            getWorld().removeObject(this);
            
        }
        if(getX() < (getImage().getWidth()-90)) 
        {  
            getWorld().removeObject(this);  
        }
    }    
}
Johnathon Johnathon

2012/12/17

#
Never mind once again... Just simply made it an else if. I'm having a really derpy morning haha. Sorry to bother you!
You need to login to post a reply.