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

2021/6/7

Make a button interact with the hp bar

Murdd0k Murdd0k

2021/6/7

#
I want that after I press the button and generate a random number, this number determines the damage I receive, and then change the hp image, I can't do this interaction, could someone help with that?
public class Luta1 extends World
{

    public Luta1()
    {    
        super(672, 336, 1); 
        prepare();
    }

    public void prepare()
    {
     addObject(new HP(), 140,20);
     addObject(new Golpe(),360,210);
     addObject(new Defender(),360,245);
     addObject(new Desistir(),580,280);
    }
    
    public void act()
    {

    }
}
public class Golpe extends BotaoLuta
{
    private boolean dano = false;
    
    
    public Golpe()
    {
    GreenfootImage playButton = new GreenfootImage(1000,60);
    Font adjustedFont = new Font(true,false,20);
    playButton.setFont(adjustedFont);
    playButton.setColor(Color.BLACK);
    playButton.drawString("GOLPEAR",300,50);
    setImage(playButton);
    }

   public void act()
  {
   checarMouse();
   checkDano();
  }
    public void checkDano()
  {
   if(Greenfoot.mouseClicked(this))
    {
     int dado = Greenfoot.getRandomNumber(2);
     if (dado == 1)
     {
       dano = true; 
       setImage("Barra2.png");
     }
   }  
  }
 }
}
public class HP extends Actor
{   
    

    public void act()
    {
     medidor();
    }
    public void medidor()
    {
     int i=0;
     Actor golpe = getOneIntersectingObject(Golpe.class);  
     if (golpe != null)
     {
        i++;
        }
      
     if(i == 0)
     {
      setImage("Barra4.png");
     }
     if(i == 1)
     {
      setImage("Barra3.png");
     }
     if(i == 2)
     {
      setImage("Barra2.png");
     }
     if(i == 3)
     {
      setImage("Barra1.png");
     }
     
    }
danpost danpost

2021/6/7

#
An HP bar is only a displaying of a value and never does anything on its own. Therefore, it should not have any "action" code (that means no act method). It needs only to maintain its value and initialize/update its image. A public method will be used to allow changing of its value (and image). Another public method will allow access to its value from other classes (a "getter" method).
import greenfoot.*;

public class HP extends Actor
{
    private int value;
    
    /* for initializing bar */
    public HP()
    {
        value = 4;
        setImage("Barra0.png");
    }
    
    /* for adjusting value/image */
    public void adjustValue(int changeAmount)
    {
        value += changeAmount;
        if (value > 0) setImage("Barra"+value+".png");
        else ; // ?? set null image or remove hp from worrld
    }
    
    /* for getting value */
    public int getValue()
    {
        return value;
    }
}
Now, buttons are great for triggering events, but not so with actually doing the events they trigger. And like the bar, does not really do anything on its own. Since the world is the best place for codes pertaining to game play, maybe there is where this functionality should be coded. Add fields in Luta1 class to maintain references to the bar and button: In the constructor, set the field values and use the act method for functionality.
public class Luta1 extends World
{
    private HP hp;
    private Golpe golpe
    
    public Luta1()
    {
        super(672, 336, 1);
        hp = new HP();
        addObject(hp, 140, 20);
        golpe = new Golpe()
        addObject(golpe, 360, 210);
        addObject(new Defender(), 360, 2450;
        addObject(new Desistir(), 580, 280);
    }
    
    public void act()
    {
        if (Greenfoot.mouseClicked(golpe)) hp.adjustValue(-1);
    }
}
(remove checkDano from Golpe class and its call from act )
Murdd0k Murdd0k

2021/6/8

#
your code itself didn't solve my problem, but its logic helped me to understand my mistakes, thank you very much
You need to login to post a reply.