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

2014/12/5

How to display a level screen in the world and change when actor dies

cloud41910 cloud41910

2014/12/5

#
Here is what i tried and i'm getting an error
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Heartless here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
    /**
     * Act - do whatever the Heartless wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
public class Heartless extends Actor
{
    HeartlessBlast heartlessblast = new HeartlessBlast();
    LastHeartless lastheartless = new LastHeartless();
    private int shotDelay = 0;
    private int JumpChance = 0;
    private int vspeed = 0;
    private int acceleration = 1;
    boolean CanJump;
    private Bar healthBar;
  
    public void act() 
    {
        fall();
        healthBar.setLocation(getX(), getY());  
        randomjump();
        fap();
        shoot();
        hit();
        World world = getWorld();
        Level level = new Level();
        world.addObject(level, world.getWidth()/2,world.getHeight()/2);
        
    }

    public Heartless()
     {  
             
     
        healthBar = new Bar("", "", 100, 100);  
        healthBar.setShowTextualUnits(false);  
        healthBar.setBarWidth(30);  
        healthBar.setBarHeight(3);  
        healthBar.setMaximumValue(70);
        healthBar.setValue(70);
        healthBar.setBreakValue(10); 
    }  
      
      public void fap()
    {
         getWorld().addObject(heartlessblast, getX(), getY());  
    }
    
    public void shoot()
    {
        if(shotDelay >= 100)
        {
            getWorld().addObject(new HeartlessBlast(), getX(), getY());
            shotDelay = 0;
        }
        shotDelay++;
    }
   public boolean isOnGround()
    {
        Clone Window = (Clone) getWorld();
        if(getY() + (getImage().getHeight() / 2) >= (Window.getHeight() -1)) {
             vspeed = 0; return true;
        }
        return false;
    }
   public void randomjump()
   {
       if( isOnGround() && Greenfoot.getRandomNumber(100) ==3)
       {
           jump();
        }
    }
    public void jump()  
   {  
     vspeed = -10;  
     fall();  
   }  
   public void fall()  
   {  
     setLocation ( getX(), getY() + vspeed);  
     vspeed = vspeed + acceleration;  
   }  
      protected void addedToWorld(World world)  
    {  
        world.addObject(healthBar, getX()-0, getY()-0); 
        Level level = new Level();
        world.addObject(level, 500, 40);
    }  
   public void hit()
   {
         Actor kiblast = getOneIntersectingObject(KiBlast.class);  
        if(kiblast != null)  
        {  
            healthBar.subtract(10);  
            getWorld().removeObject(kiblast);
        }  
           if(healthBar.getValue() == 0)  
        {    
        World world = getWorld();
        Clone clone = (Clone)world;
        ScoringPoints scoringpoints = clone.getScoringPoints();
        scoringpoints.addScore();
        getWorld().addObject(lastheartless, getX(), getY());
        getWorld().removeObject(healthBar);  
        getWorld().removeObject(this);
        }  
    }
}
cloud41910 cloud41910

2014/12/5

#
here's the error
java.lang.NullPointerException
	at Heartless.act(Heartless.java:34)
	at greenfoot.core.Simulation.actActor(Simulation.java:583)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:541)
	at greenfoot.core.Simulation.runContent(Simulation.java:215)
	at greenfoot.core.Simulation.run(Simulation.java:205)
wabuilderman wabuilderman

2014/12/5

#
could you try naming level, something other than level? something like firstLevel? As it stands, the object has the same name as the class. Maybe thats it. I know in some places, name actually can matter quite a bit.
danpost danpost

2014/12/5

#
There is no problem with the naming. The main problem ... well, there are two here. (1) every act cycle you will be creating and adding a new Level object into the world which will most certainly either (a) create lag (b) cause an OutOfMemory error and/or (c) cause a StackOverflow error; and, (2) the moment 'hit' removes the actor from the world, 'world' will be assigned a 'null' value on line 32 and a NullPointerException will occur on line 34 when you try to call methods on 'world'. If you only want to add a new Level object into the world when your player dies, then put the code to add a new Level object into the world at the place in your code where you know the player has died at (somewhere between lines 105 and 112).
You need to login to post a reply.