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

2014/11/3

My health code seem to not work

1
2
coder04 coder04

2014/11/3

#
my ship has the health
import greenfoot.*;
import java.util.List;
/**
 */
public class Ship extends Actor
{
    int speed = 5;
    int shotCounter = 0;
    private int delay = 0;
    private final int MAX_DELAY = 13;
    private int health = 3;

    /**
     * Method act: move spaceship 
     */
    public void act() 
    {
        move();   
        
        if(delay <= 0) {
            if( Greenfoot.isKeyDown("space") ) 
            {

                fire();
                Greenfoot.playSound("laser.wav");
                delay = MAX_DELAY;
            }
        }
        else {
            delay--;
        }

    }
   
    
    /**
     * Fire the laser
     */
    private void fire()
    {
        Shot shot = new Shot();
        getWorld().addObject(shot, getX(), getY());
        getWorld().addObject(shot, getX(), getY());
        shot.setRotation(getRotation());
    }    

    /**
     * Method move: checks for keystrokes and applies the changes, then moves the ship.
     * I applied a bit of slowing to the ship's speed (so it would be drifting to a stop)
     */
    private void move()
    {
        int dz = 0;
        if (Greenfoot.isKeyDown("d")) dz++;
        if (Greenfoot.isKeyDown("a")) dz--;
        setRotation(getRotation() + dz * 5);
        int ds = -1;
        if (Greenfoot.isKeyDown("w")) ds += 2;
        speed += ds;
        if (speed < 0) speed = 0;
        if (speed > 90) speed = 90;
        if (speed >= 200) move(speed / 100);
        if (Greenfoot.isKeyDown("s")) ds += 5;
        speed += ds;
        if (speed < 5) speed = 5;
        if (speed > 200) speed = 200;
        if (speed >= 20) move(speed / 10);

    }

   // private int health = 5; 
      
    public void setHealth(int points) {  
        health += points;  
    }  
    public int getHealth() {  
        return health;  
    }  
}  
    
The redlaser need to remove the ship after three hits
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Enemylaser here.
 * 
 */
public class Redlaser extends Mover

{
   // private int life; 
    
    /**
     * Act - do whatever the Enemylaser wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
     public void act()
     {
        move(20);
        eat();
        //life--;
       //if (life == 0)
      // {  
        //   getWorld().removeObject(this);  
       //}
       ifAtWorldEdge();
     } 
     
    public void ifAtWorldEdge()  
    {  
        if (atWorldEdge())  
        {  
             getWorld().removeObject(this);  
        }  
    }
    
     
    public void eat()
    {
        //Actor Ship;
        //Ship = getOneObjectAtOffset(0, 0, Ship.class);
        
       // if (Ship != null)
       // {
          //  World World;
          //  World = getWorld();
          //  GameOver gameover = new GameOver();
          //  World.addObject(gameover, World.getWidth()/2, World.getHeight()/2);
          //  World.removeObject(Ship);
            
        //} 
        
       //  if (Ship != null)
       // {
        //    World world;
        //    world = getWorld();
        //    world.removeObject(Ship);
        //    GameOver gameover = new GameOver();
       // }
        
              
        Actor BigRock;
        BigRock = getOneObjectAtOffset(0, 0, BigRock.class);
        if (BigRock != null)
        {
            World world;
            world = getWorld();
            world.removeObject(BigRock);
        } 
    }
    
    //in your bullet class you have to add this method:  
public void hitEnemy() {  
    Ship ship = (Ship) getOneObjectAtOffset(0, 0, Ship.class);  
    if (ship != null) {  
        ship.setHealth(-1);//decrements the health of this enemy;  
        //if you want your bullet to be removed now you have to add the next line; otherwhise delete it.  
        getWorld().removeObject(this);  
    }  
}  
}   
    
    
    
erdelf erdelf

2014/11/3

#
write
hitEnemy();
in your act method, then it should work
coder04 coder04

2014/11/3

#
I get this error after adding it java.lang.NullPointerException at GameOver.<init>(GameOver.java:13) at Rock.eat(Rock.java:131) at Rock.act(Rock.java:26) at BigRock.act(BigRock.java:11) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.NullPointerException at GameOver.<init>(GameOver.java:13) at Enemylaser.eat(Enemylaser.java:46) at Enemylaser.act(Enemylaser.java:19) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205) java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed. at greenfoot.Actor.failIfNotInWorld(Actor.java:663) at greenfoot.Actor.getOneObjectAtOffset(Actor.java:867) at Redlaser.hitEnemy(Redlaser.java:74) at Redlaser.act(Redlaser.java:26) at greenfoot.core.Simulation.actActor(Simulation.java:568) at greenfoot.core.Simulation.runOneLoop(Simulation.java:526) at greenfoot.core.Simulation.runContent(Simulation.java:215) at greenfoot.core.Simulation.run(Simulation.java:205)
coder04 coder04

2014/11/3

#
Also my ship is not getting removed when the redlaser hits me 3 times
Alwin_Gerrits Alwin_Gerrits

2014/11/3

#
java.lang.IllegalStateException: Actor not in world. An attempt was made to use the actor's location while it is not in the world. Either it has not yet been inserted, or it has been removed.
Seems like you're not adding the object to the world? Or you removed it before this part of the code came by. By the way, why using atWorldEdge() if there's a standart command like isAtEdge? isAtEdge() returns true if the object is at any edge of the world. Ow i think i spotted the problem. You need to make your statement at row 77 into an statement that has an if-condition. So if(ship.getHealth()==0) {getWorld().removeObject(this);}. They way you posted the code now it's going to remove the ship as soon as it loses even one health. Besides after it loses one health it could be called on again while it's allready removed. Allso no something you would want seen your last error.
coder04 coder04

2014/11/3

#
These are the codes now but they dont work can you check what ive done wrong ship code
import java.util.List;
/**
 */
public class Ship extends Actor
{
    int speed = 5;
    int shotCounter = 0;
    private int delay = 0;
    private final int MAX_DELAY = 13;
    private int health = 3;

    /**
     * Method act: move spaceship 
     */
    public void act() 
    {
        move();   
        
        if(delay <= 0) {
            if( Greenfoot.isKeyDown("space") ) 
            {

                fire();
                Greenfoot.playSound("laser.wav");
                delay = MAX_DELAY;
            }
        }
        else {
            delay--;
        }

    }
   
    
    /**
     * Fire the laser
     */
    private void fire()
    {
        Shot shot = new Shot();
        getWorld().addObject(shot, getX(), getY());
        getWorld().addObject(shot, getX(), getY());
        shot.setRotation(getRotation());
    }    

    /**
     * Method move: checks for keystrokes and applies the changes, then moves the ship.
     * I applied a bit of slowing to the ship's speed (so it would be drifting to a stop)
     */
    private void move()
    {
        int dz = 0;
        if (Greenfoot.isKeyDown("d")) dz++;
        if (Greenfoot.isKeyDown("a")) dz--;
        setRotation(getRotation() + dz * 5);
        int ds = -1;
        if (Greenfoot.isKeyDown("w")) ds += 2;
        speed += ds;
        if (speed < 0) speed = 0;
        if (speed > 90) speed = 90;
        if (speed >= 200) move(speed / 100);
        if (Greenfoot.isKeyDown("s")) ds += 5;
        speed += ds;
        if (speed < 5) speed = 5;
        if (speed > 200) speed = 200;
        if (speed >= 20) move(speed / 10);

    }

   // private int health = 5; 
      
    public void setHealth(int points) {  
        health += points;  
    }  
    public int getHealth() {  
        return health;
    } 

}  
    
Redlaser code
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Enemylaser here.
 * 
 */
public class Redlaser extends Mover

{
   // private int life; 
    
    /**
     * Act - do whatever the Enemylaser wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
     public void act()
     {
        move(20);
        eat();
        //life--;
       //if (life == 0)
      // {  
        //   getWorld().removeObject(this);  
       //}
       ifAtWorldEdge();
       hitEnemy();
     } 
     
    public void ifAtWorldEdge()  
    {  
        if (atWorldEdge())  
        {  
             getWorld().removeObject(this);  
        }  
    }
    
     
    public void eat()
    {
        //Actor Ship;
        //Ship = getOneObjectAtOffset(0, 0, Ship.class);
        
       // if (Ship != null)
       // {
          //  World World;
          //  World = getWorld();
          //  GameOver gameover = new GameOver();
          //  World.addObject(gameover, World.getWidth()/2, World.getHeight()/2);
          //  World.removeObject(Ship);
            
        //} 
        
       //  if (Ship != null)
       // {
        //    World world;
        //    world = getWorld();
        //    world.removeObject(Ship);
        //    GameOver gameover = new GameOver();
       // }
        
              
        Actor BigRock;
        BigRock = getOneObjectAtOffset(0, 0, BigRock.class);
        if (BigRock != null)
        {
            World world;
            world = getWorld();
            world.removeObject(BigRock);
        } 
    }
    
    //in your bullet class you have to add this method:  
public void hitEnemy() {  
    Ship ship = (Ship) getOneObjectAtOffset(0, 0, Ship.class);  
    if (ship != null) {  
        ship.setHealth(-1);//decrements the health of this enemy;  
        //if you want your bullet to be removed now you have to add the next line; otherwhise delete it.  
        //getWorld().removeObject(this);
         if(ship.getHealth()==0) {getWorld().removeObject(this);}
    }  
}  
}   
    
    
Alwin_Gerrits Alwin_Gerrits

2014/11/3

#
Ah i see the problem (probably). Take the statement
if(ship.getHealth()==0) {getWorld().removeObject(this);}
and place it inside you ship class instead of your redlaser class. You can put it on the end of your act method, but you have to change it a bit because you're not calling a different class anymore. So change it to:
if(getHealth()==0) {getWorld().removeObject(this);} 
coder04 coder04

2014/11/3

#
oh
Alwin_Gerrits Alwin_Gerrits

2014/11/3

#
Lol we reacted allmost at the exact same moment. Anyway please respond if that solves it
coder04 coder04

2014/11/3

#
The object is still not getting removed
coder04 coder04

2014/11/3

#
is there a way to fix? cant seem to fix it?
Alwin_Gerrits Alwin_Gerrits

2014/11/3

#
Maybe change setHealth(int points) {health+=points;}. Not sure if it helps, but if you just call setHealth and make the method like this: setHealth() {health--;} It might solve your problem. So just to be clear:
ship.setHealth(); //your new call

setHealth()
{health--;} //your new method
-- simply means decrease variable with 1
danpost danpost

2014/11/3

#
The easiest way to avoid throwing the IllegalStateException is to combine everything that could have the actor removed from the world into one 'if' condition:
if (life == 0 || atWorldEdge() || hitEnemy()) getWorld().removeObject(this);
Of course, the 'hitEnemy' method must be adjusted to return a true/false value (like the 'atWorldEdge' method does). Remove line 79 in the Redlaser class above. You should do that check in the 'setHealth' method of the Ship class after the value adjustment.
coder04 coder04

2014/11/3

#
where shall i add it because if i add it in the act or the hit enemy in the red laser class it says void type not allowed here
coder04 coder04

2014/11/3

#
it keeps saying void type not allowed?
There are more replies on the next page.
1
2