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

2015/5/21

Reducing health

vampire-wannabe vampire-wannabe

2015/5/21

#
Okay so what I'm trying to do is reduce the health when the "Fire" class reaches the bottom but it keeps saying it can't find the "fireWorld" variable. Am I in the wrong class? I really don't know what's wrong..
import greenfoot.*;

/**
 * Write a description of class Fire here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Fire extends Actor
{
    /**
     * Act - do whatever the Fire wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.
        move(1);
        setRotation(90);
        aangGone();
    }
    
    public void aangGone()
    {
        int yCoord = getY();
        if(yCoord == 399)
        {
            getWorld().removeObject(this);
            Volcano volcano = (Volcano)fireWorld;   //cannot find fireWorld variable
            Health health = volcano.getHealth();
            health.lowerHealth();
        }
       Actor aang = getOneIntersectingObject(Aang.class);
       if(aang != null)
          {
             World aangWorld = getWorld();
             aangWorld.removeObject(aang);
       }
    }
}
danpost danpost

2015/5/21

#
In line 29, you are using 'fireWorld' as if it was a previously defined variable -- which is is not (not in the method, the class, or any class this class extends). If you are trying to set the 'volcano' variable to the world object on that line, then it is too late as you just remove the actor from the world. Any way of getting a reference to the world has been eliminated. If you switch lines 28 and 29, you can get the reference before you remove the actor from the world; however, using 'fireWorld' will still fail -- use 'getWorld()' instead. There is another issue with the 'aangGone' method. When the actor reaches a y-coordinate of 399 and is removed from the world, line 33 will throw an IllegalStateException as you cannot get intersectors of an actor that is not in the world. Put the rest of the method (lines 33 through 38) in an 'else' block so it is only executed when the y-coordinate is not 399. As an alternative to that, you could move lines 33 through 38 to the beginning of the method.
You need to login to post a reply.