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

2019/11/30

How to remove object after it reach edge and remove object if it hit another object?

dROS dROS

2019/11/30

#
I want to remove object which is bullet after it reach edge but it didn't work. can you please help me? Thank you
       at Bullet.checkTankHit(Bullet.java:35)
	at Bullet.act(Bullet.java:21)
	at greenfoot.core.Simulation.actActor(Simulation.java:567)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:530)
	at greenfoot.core.Simulation.runContent(Simulation.java:193)
	at greenfoot.core.Simulation.run(Simulation.java:183)
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();
       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);
       tank.hit(damage);
       
 
    
}
}
}


danpost danpost

2019/11/30

#
Insert the following at line 21:
if (getWorld() == null) return;
OR: change line 21 to:
if (getWorld() != null) checkTankHit();
dROS dROS

2019/11/30

#
Thank you so much for your help
You need to login to post a reply.