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

2019/6/9

Using an int of another class

loldertroll loldertroll

2019/6/9

#
I am making a game rigt now with a character that is able to loose hp. I want to show the hp with a healthbar by replacing the image of my healthbar everytime the actor gets damage, but i dont know how to do it.
danpost danpost

2019/6/10

#
loldertroll wrote...
I am making a game rigt now with a character that is able to loose hp. I want to show the hp with a healthbar by replacing the image of my healthbar everytime the actor gets damage, but i dont know how to do it.
Create the current image in a seperate method:
private void updateHealthBarImage()
{
    GreenfootImage img = ...
    ...
    setImage(img);
}
Then call it from the method where damage is taken:
hp--;
updateHealthBarImage();
You should also be able to use the update method to initialize the image of the health bar.
loldertroll loldertroll

2019/6/10

#
ty
loldertroll loldertroll

2019/6/10

#
danpost wrote...
loldertroll wrote...
I am making a game rigt now with a character that is able to loose hp. I want to show the hp with a healthbar by replacing the image of my healthbar everytime the actor gets damage, but i dont know how to do it.
Create the current image in a seperate method:
private void updateHealthBarImage()
{
    GreenfootImage img = ...
    ...
    setImage(img);
}
Then call it from the method where damage is taken:
hp--;
updateHealthBarImage();
You should also be able to use the update method to initialize the image of the health bar.
ok maybe im stupid but i dont know how to do it
danpost danpost

2019/6/10

#
loldertroll wrote...
ok maybe im stupid but i dont know how to do it
You will have to show what you tried so we can find out where you went wrong.
loldertroll loldertroll

2019/6/10

#
Actor:
public void leben(int amount)
   {
       hp = amount;
     
       if(unverwundbarkeit == false)
       {
           
       if(isTouching(kleinespinne.class))
{
           getWorld().removeObjects(getObjectsInRange(80,kleinespinne.class));
           
            unverwundbarkeit = true;
           hp-=1;
        }
       if(isTouching(Kristall.class))
       {
           getWorld().removeObjects(getObjectsInRange(128,Kristall.class));
                
                
            unverwundbarkeit = true; 
            hp -= 1;
            
            
       
        }
       
       
    

        if(hp<1)
        {
            Greenfoot.setWorld(new verloren());
        }
      }
      if (unverwundbarkeit == true)
      { this.timer3 = this.timer +1;
          if(this.timer3 > 400)
          {
            this.timer3 = 0;
            unverwundbarkeit = false;
            }
          
        }
     }
healthbar:
public void act() 


    {
        if(! getWorld().getObjects(Jim.class).isEmpty())
        {  
         Jim jim = (Jim)getWorld().getObjects(Jim.class).get(0);
       
        int jimX = jim.getX();
         int jimY = jim.getY();
         setLocation(jimX,jimY -50);
    } 
}
   public void jimhpupdate(){
       MyWorld MyMyWorld = (MyWorld) getWorld();
       Jim jim =  MyMyWorld.getjim();
       jim.leben(5);
       
       
        
    if(hp==5)
    {
        setImage("jimhp1.png");
    }
    if( hp==4)
    {
        setImage("jimhp2.png");
        
    }
    if(hp==3)
    {
        setImage("jimhp3.png");
    }
     if(hp==2)
    {
        setImage("jimhp2.png");
    }
     if(hp==1)
    {
        setImage("jimhp1.png");
    }
    

 }
world:
 public Jim getjim()
 { return jimm;
    }
    
Super_Hippo Super_Hippo

2019/6/11

#
Is it on purpose that you have "timer3" on the left, but "timer" on the right?
this.timer3 = this.timer +1;
loldertroll loldertroll

2019/6/11

#
no but i already fixed that
You need to login to post a reply.