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

2016/5/9

Int Health Display Help

StoveAteMe StoveAteMe

2016/5/9

#
Hello, I have a "Savior" class that loses health if it comes into contact with a "Rocket" object. I want to display the amount of health in the bottom left corner of my world. Which is (600,400,1). Here is the code for my actor Savior
public void act() 
    {
        
        if(Greenfoot.isKeyDown("a")){
  
            move(-3);
    }    
    if(Greenfoot.isKeyDown("d")) {
            move(3);

       }
    if(time > 0){
            time--;
    }
    if(Greenfoot.isKeyDown("w")) {
           if(time == 0){
           getWorld().addObject(new Missile(), this.getX(), this.getY());
           time = 30;
        }    
    }   
    Hit();
    
     
  }
  
  public void Hit()
  {  
   
      if (isTouching(Rocket.class)) {
        removeTouching(Rocket.class);
        health = health - 25;
        if (health == 0) {
            getWorld().removeObject(this);
        }
    }
  }
  
  public int getHealth() {
      return health;
    }
}
Would I need to have this number display be its own subclass of actor? I initially thought of having the display in my savior class, so I can reference the health variable.
danpost danpost

2016/5/9

#
You could, for now, just use this in the class where the health variable is:
getWorld().setText("Health:  "+health, 550, 380);
Place this line in the constructor of the class and immediately after any line that changes the health value. If can view scenarios on this site, I recommend you check out my Value Display Tutorial scenario.
StoveAteMe StoveAteMe

2016/5/9

#
Okay I just tried your suggestion, placing that piece of code in my constructor, and right after line 31 in the code I previously posted. But now it's giving the error: "cannot find symbol - method setText(java.lang.string, int, int)"
danpost danpost

2016/5/9

#
StoveAteMe wrote...
Okay I just tried your suggestion, placing that piece of code in my constructor, and right after line 31 in the code I previously posted. But now it's giving the error: "cannot find symbol - method setText(java.lang.string, int, int)"
Oh. Sorry. One cannot use 'getWorld' in the constructor. Add the following method then:
StoveAteMe StoveAteMe

2016/5/9

#
So I just remove getWorld from the statement and just leave the rest of the method? If that's what your asking, I did that and I'm still getting the same error both after line 31, and in my constructor.
danpost danpost

2016/5/10

#
danpost wrote...
StoveAteMe wrote...
Okay I just tried your suggestion, placing that piece of code in my constructor, and right after line 31 in the code I previously posted. But now it's giving the error: "cannot find symbol - method setText(java.lang.string, int, int)"
Oh. Sorry. One cannot use 'getWorld' in the constructor. Add the following method then:
protected void addedToWorld(World world)
{
    world.setText("Health:  "+health, 550, 380);
}
This is my post above -- with correct code tags (the incorrect code tags prevented the code from showing in the post). Remove that line from the constructor.
StoveAteMe StoveAteMe

2016/5/10

#
I'm still receiving the same error as before, with the setText method. Btw the only thing different about the this segment of code, and the previous one I sent earlier, is that I added your method near the bottom.
public Savior()
    {
        GreenfootImage image = getImage();
        image.scale(image.getWidth() - 20,  image.getHeight() - 20);
        setImage(image);
        
    } 
    public void act() 
    {
            
         if(Greenfoot.isKeyDown("a")){
  
            move(-3);
    }    
    if(Greenfoot.isKeyDown("d")) {
            move(3);

       }
    if(time > 0){
            time--;
    }
    if(Greenfoot.isKeyDown("w")) {
           if(time == 0){
           getWorld().addObject(new Missile(), this.getX(), this.getY());
           time = 00;
        }    
    }   
    Hit();
    
     
  }
  
  public void Hit()
  {  
   
      if (isTouching(Rocket.class)) {
        removeTouching(Rocket.class);
        health = health - 25;
        
        if (health == 0) {
            getWorld().removeObject(this);
        }
    }
  }
  
  public int getHealth() {
      return health;
    }
  protected void addedToWorld(World world)
   {
    world.setText("Health:  "+health, 550, 380);
   }
danpost danpost

2016/5/10

#
StoveAteMe wrote...
I'm still receiving the same error as before, with the setText method.
Oh, gosh! The method is 'showText' -- not 'setText'.
StoveAteMe StoveAteMe

2016/5/11

#
danpost wrote...
danpost wrote...
StoveAteMe wrote...
Okay I just tried your suggestion, placing that piece of code in my constructor, and right after line 31 in the code I previously posted. But now it's giving the error: "cannot find symbol - method setText(java.lang.string, int, int)"
Oh. Sorry. One cannot use 'getWorld' in the constructor. Add the following method then:
protected void addedToWorld(World world)
{
    world.showText("Health:  "+health, 550, 380);
}
This is my post above -- with correct code tags (the incorrect code tags prevented the code from showing in the post). Remove that line from the constructor.
Okay so, I have a couple of questions, what does the protected statement do, when declaring the method, and why do we need the world variable of type world. And what parameter would I input for the addedToWorld method for it to function correctly? Also just to give you an idea of what is happening, where my code is at now, it only displays the total amount of health, but it does not decrease it accordingly, I assume that is because the addedToWorld method is not placed after the actual "health" reducing statement?
danpost danpost

2016/5/11

#
StoveAteMe wrote...
Okay so, I have a couple of questions, what does the protected statement do, when declaring the method, and why do we need the world variable of type world. And what parameter would I input for the addedToWorld method for it to function correctly?
Adding this method overrides the method with the same name, parameter, and access modifier as the one in the Actor class. The method is called automatically by greenfoot anytime an actor created from the class is added into any world. The method in the Actor class has no implementation (the body of the method between the squiggly brackets is empty). It is provided so that you can perform whatever you may want at the time an actor of the class is added into a world. You do not need to place a call to this method in your code (similar to how the act method works -- but different as far as when it is called).
Also just to give you an idea of what is happening, where my code is at now, it only displays the total amount of health, but it does not decrease it accordingly, I assume that is because the addedToWorld method is not placed after the actual "health" reducing statement?
You assume incorrectly. As I said before, you need to insert a copy of the one line located within the body of the 'addedToWorld' method after any place in the class where you change the value of the health field (like line 39 above).
You need to login to post a reply.