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

2019/11/30

Everything stop after score is add. how do i fix it?

dROS dROS

2019/11/30

#
whenever bullet hits the tank . Everything stop i don't know why. can you help me please?
	at greenfoot.core.Simulation.run(Simulation.java:183)
java.lang.NullPointerException
	at Bullet.checkTankHit(Bullet.java:43)
	at Bullet.act(Bullet.java:21)
	at greenfoot.core.Simulation.actActor(Simulation.java:567)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:530)
	
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Bullet here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Bullet extends Actor
{
    private static final int damage =15;

    /**
     * Act - do whatever the Bullet wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        setLocation(getX(), getY()+1);
        explosion();
       if(getWorld()!=null)  checkTankHit();
        

    }    
    private void explosion()
    {
        if(isAtEdge()){
            getWorld().removeObject(this);
            
        }
    }

    /**
     * Check whether enemy bomb hit our tank or not
     */ 
    private void checkTankHit(){
        Tank tank =(Tank) getOneIntersectingObject(Tank.class);

        if (tank != null) 
        {
           getWorld().removeObject(this);
           Background background = (Background)getWorld();
           background.addScore(15);
    
        }
    }
}


danpost danpost

2019/12/1

#
Move line 41 down a line or two. The getWorld call needs to be before that line since when the actor is removed from the world, it will have no world to get.
You need to login to post a reply.