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

2013/12/6

Player get hit and subtract health problem

Unreliable Unreliable

2013/12/6

#
Well the title says it. here is the terminal window when the enemy touches the player: java.lang.NullPointerException at Enemy.hit(Enemy.java:37) at Enemy.act(Enemy.java:21) 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 Enemy.hit(Enemy.java:37) at Enemy.act(Enemy.java:21) 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 Enemy.hit(Enemy.java:37) at Enemy.act(Enemy.java:21) 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 Enemy.hit(Enemy.java:37) at Enemy.act(Enemy.java:21) 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 Enemy.hit(Enemy.java:37) at Enemy.act(Enemy.java:21) 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) and here is the project code of the enemy:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Enemy here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Enemy extends Actor
{
    private Health aHealth;
    
    /**
     * Act - do whatever the Enemy 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.
        followPlayer();
        hit();
    }    

    public void followPlayer()
    {
        Player pl = (Player) getWorld().getObjects(Player.class).get(0);  
        turnTowards(pl.getX(), pl.getY());
        move(4);
    }
    
    public void hit() 
    {  
        Actor Enemy;
        Enemy = getOneIntersectingObject(Player.class);  
        if (Enemy != null)  
        {  
            aHealth.sub(150);
            getWorld().removeObjects(getWorld().getObjects(Enemy.class)); 
        }  
    }
}
Gevater_Tod4711 Gevater_Tod4711

2013/12/6

#
The problem is that you want to execute the methud sub on the object aHealth in line 37. But this object is null. Therefore you get the NullPointerException. You can fix this problem by initialising the variable aHealth. E.g. you could change line 11 of your code to: 'private Health aHealth = new Health();'
Unreliable Unreliable

2013/12/6

#
Thanks so much! One last thing. (You've probably spotted it)
getWorld().removeObjects(getWorld().getObjects(Enemy.class)); 
with that when a player hits an enemy or a enemy hits a player it will remove the enemy. but...... if i have more than one enemy in the world it removed all of them... any tutorial or guide i can follow for that? thanks again!
Gevater_Tod4711 Gevater_Tod4711

2013/12/6

#
getWorld().removeObjects() removes not only one object but a list of objects. To remove only the enemy your player is touching you need to change line 38 of your code to: 'getWorld().removeObject(Enemy);' This will only remove the enemy your player is touching because Enemy is the variable that represents the touching enemy (because you initialised the variable with this enemy in line 34).
Unreliable Unreliable

2013/12/6

#
i hate to keep this going but I am kinda new to Greenfoot and java. But when i compile I get OutOfMemoryError. I know what this means but it makes no sense. it was using to memory before but now its using about 4gb... Here is the terminal java.lang.OutOfMemoryError: Java heap space at java.awt.image.DataBufferInt.<init>(DataBufferInt.java:75) at java.awt.image.Raster.createPackedRaster(Raster.java:467) at java.awt.image.DirectColorModel.createCompatibleWritableRaster(DirectColorModel.java:1032) at java.awt.GraphicsConfiguration.createCompatibleImage(GraphicsConfiguration.java:186) at greenfoot.util.GraphicsUtilities.createCompatibleTranslucentImage(GraphicsUtilities.java:179) at greenfoot.GreenfootImage.<init>(GreenfootImage.java:131) at greenfoot.World.getBackground(World.java:208) at Board.<init>(Board.java:44) at Player.<init>(Player.java:11) at Board.<init>(Board.java:51) at Player.<init>(Player.java:11) at Board.<init>(Board.java:51) at Player.<init>(Player.java:11) at Board.<init>(Board.java:51) at Player.<init>(Player.java:11) at Board.<init>(Board.java:51) at Player.<init>(Player.java:11) at Board.<init>(Board.java:51) at Player.<init>(Player.java:11) at Board.<init>(Board.java:51) at Player.<init>(Player.java:11) at Board.<init>(Board.java:51) at Player.<init>(Player.java:11) at Board.<init>(Board.java:51) at Player.<init>(Player.java:11) at Board.<init>(Board.java:51) at Player.<init>(Player.java:11) at Board.<init>(Board.java:51) at Player.<init>(Player.java:11) at Board.<init>(Board.java:51) at Player.<init>(Player.java:11) at Board.<init>(Board.java:51) java.lang.OutOfMemoryError: Java heap space at java.awt.image.DataBufferInt.<init>(DataBufferInt.java:75) at java.awt.image.Raster.createPackedRaster(Raster.java:467) at java.awt.image.DirectColorModel.createCompatibleWritableRaster(DirectColorModel.java:1032) at java.awt.GraphicsConfiguration.createCompatibleImage(GraphicsConfiguration.java:186) at greenfoot.util.GraphicsUtilities.createCompatibleTranslucentImage(GraphicsUtilities.java:179) at greenfoot.GreenfootImage.<init>(GreenfootImage.java:131) at greenfoot.World.getBackground(World.java:208) at Board.<init>(Board.java:44) at Player.<init>(Player.java:11) at Board.<init>(Board.java:51) at Player.<init>(Player.java:11) at Board.<init>(Board.java:51) at Player.<init>(Player.java:11) at Board.<init>(Board.java:51) at Player.<init>(Player.java:11) at Board.<init>(Board.java:51) at Player.<init>(Player.java:11) at Board.<init>(Board.java:51) at Player.<init>(Player.java:11) at Board.<init>(Board.java:51) at Player.<init>(Player.java:11) at Board.<init>(Board.java:51) at Player.<init>(Player.java:11) at Board.<init>(Board.java:51) at Player.<init>(Player.java:11) at Board.<init>(Board.java:51) at Player.<init>(Player.java:11) at Board.<init>(Board.java:51) at Player.<init>(Player.java:11) at Board.<init>(Board.java:51) at Player.<init>(Player.java:11) at Board.<init>(Board.java:51) Board:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
import java.awt.GraphicsEnvironment;
import java.awt.Font;



/**
 * Write a description of class Board here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Board extends World
{
    private Counter actCounter;
    public Bar bar = new Bar("Health:", "" , 1000, 1000);
    
    public void act()
    {
        actCounter.setValue(actCounter.getValue() + 1);
        
        if (bar.getValue() == bar.getMinimumValue())
        {
            if (getObjects(GameOver.class).isEmpty()) showGameOver();
            return;
        }
    }
    
    public void BarSubtract(int value)
    {
        value = 0;
        bar.subtract(value); 
    }
    
    /**
     * Constructor for objects of class Board.
     * 
     */
    public Board()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 800, 1); 
        GreenfootImage background = getBackground();  
        background.setColor(Color.BLACK);  
       // background.fill();  
        createStars(500);  
        
        addObject(bar, getWidth()/3, 30);
        
        addObject(new  Player(), getWidth()/2, getHeight()/2);
        actCounter = new Counter("Score: ");

        addObject(actCounter, getWidth() / 2, 30);
        Greenfoot.setSpeed(50);
    }
    
    /** 
      * Create a given number of stars in space. 
      */  
     private void createStars(int number)  
     {  
          GreenfootImage background = getBackground();  
          for(int i=0; i < number; i++)  
          {  
               int x = Greenfoot.getRandomNumber( getWidth() );  
               int y = Greenfoot.getRandomNumber( getHeight() );  
               int color = 120 - Greenfoot.getRandomNumber(100);  
               background.setColor(new Color(color,color,color));  
               background.fillOval(x, y, 2, 2);  
          }  
     }
     
     private void showGameOver()
    {
        addObject(new GameOver(), getWidth() / 2, getHeight() / 2);
    }
}
player:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Player here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Player extends Actor
{
    Board br = new Board();
    /**
     * Act - do whatever the Player wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        hit();
        MouseInfo info = Greenfoot.getMouseInfo();
        if(info!=null){
            int thatX=info.getX();
            int thatY=info.getY() ;
            turnTowards(thatX, thatY); 
        }
        moveOnKeyPress();
        
    }    
    
    public void hit() 
    {   
        Actor Enemy;
        Enemy = getOneIntersectingObject(Enemy.class);
        if (Enemy != null)  
        {  
            br.BarSubtract(150);
            getWorld().removeObject(Enemy);
        }  
    }
    
    public void moveRight()  
    {  
        int x = (getX() + 4);  
        int y = (getY());  
              
        setLocation(x, y);  
    }  
    public void moveLeft()  
    {  
        int x = (getX() - 4);  
        int y = (getY());  
              
        setLocation(x, y);  
    }  
    public void moveDown()  
    {  
        int x = (getX());  
        int y = (getY() + 4);  
            
        setLocation(x, y);  
    }  
    public void moveUp()  
    {  
        int x = (getX());  
        int y = (getY() - 4);  
              
        setLocation(x, y);  
    }
    
    public void moveOnKeyPress()
    {
        if(Greenfoot.isKeyDown("left")) {
            moveLeft();
        }
        if(Greenfoot.isKeyDown("right")) {
            moveRight();
        }
        if(Greenfoot.isKeyDown("up")) {
            moveUp();
        }
        if(Greenfoot.isKeyDown("down")) {
            moveDown();
        }
    }
}
Gevater_Tod4711 Gevater_Tod4711

2013/12/6

#
The problem is in line 51 of your Board class and in line 11 of your player class. Your player creates a new Board object in line 11 when it's initialised. The new board object creates a new player object in line 51 (also when it's initialised). This new player object creates a new board... So you create so many objects that your heap space is not enough. You need to change the code so that you either create a new player object when the board is initialised or a new board when the player is initialised. But both will always create objects on and on so your heap space is not enought.
Unreliable Unreliable

2013/12/7

#
Ok, I understand that but for some reason I cant seem to figure out how to fix it. If i move certain classes to other actors and/or board it gives me errors because a certain statement is not recognized by that class. If I have to post the rest of my code I will.
Gevater_Tod4711 Gevater_Tod4711

2013/12/7

#
I think the easiest way would be to remove line 51 of your Board class. If every Player has it's own board you don't need to create a new player in the board. The reference to the player that is created is also not saved in any variables. There is just a new Player added to the world. So I think you can delete this. If this doesn't work it would be gread if you could post the errors or exceptions you get. That would make it much easier to find the problem.
Unreliable Unreliable

2013/12/7

#
Ooh ok I got that to work but the reason I had it add the player is so that the player didn't have to do it themselves. But I need the board reference in the player because I need to subtract hp if the player gets hit by an enemy. I am using danposts progress bar.
You need to login to post a reply.