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
1
2
3
4
5
6
   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)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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:
1
if (getWorld() == null) return;
OR: change line 21 to:
1
if (getWorld() != null) checkTankHit();
dROS dROS

2019/11/30

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