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

2015/11/19

How to remove both an Actor and a bullet?

Hpage17 Hpage17

2015/11/19

#
Hi, I've been having some trouble finding a way to remove both a bullet and the actor if the bullet hits an actor.
Royalblue64 Royalblue64

2015/11/19

#
Simple solution! Your bullet class should contain something like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
public void act()
{
    move(100);  //This should just be the bullet movement code
    checkForPlayer();
}
 
public void checkForPlayer()
{
    if (getOneIntersectingObject(Player.class) != null)
    {
        getWorld().removeObject(getOneIntersectingObject(Player.class));
        getWorld().removeObject(this);
    }
}
This will allow your bullet to see if it is currently intersecting the player, and if so, it will remove the player and then remove itself. Alternatively, I realized that you could also use Greenfoot's built-in removal of touching objects:
1
2
3
4
5
6
7
8
public void checkForPlayer()
{
    if (isTouching(Player.class))
    {
        removeTouching(Player.class);
        getWorld().removeObject(this);
    }
}
Hpage17 Hpage17

2015/11/19

#
Whenever I do this, it interferes with my code for removing the bullet if it reaches the end of the world. Any way to make these work together? Also, I have two enemy classes I want this to work for. For some reason it'll give me a java.lang.IllegalStateException whenever I hit one of the enemy classes.
Hpage17 Hpage17

2015/11/19

#
private void hit() { if (getOneIntersectingObject(Alien1.class) != null) { getWorld().removeObject(getOneIntersectingObject(Alien1.class)); getWorld().removeObject(this); counter.add(1); } if (getOneIntersectingObject(Alien2.class) != null) { getWorld().removeObject(getOneIntersectingObject(Alien2.class)); getWorld().removeObject(this); counter.add(5); } } Here is what I put in for the bullet
Royalblue64 Royalblue64

2015/11/19

#
Ah, okay. One of the problems is that you're calling "counter.add(1)" and "counter.add(5)" AFTER the bullet has been removed, meaning it is trying to do something when it doesn't actually exist. So, the bullet should contain this:
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
public void act()
{
    move(100);
    checkForHit();
}
 
private void checkForHit()
{
    if (atWorldEdge())
    {
        getWorld().removeObject(this);
    }
     
    if (isTouching(Alien1.class))
    {
        counter.add(1);
         
        removeTouching(Alien1.class);
        getWorld().removeObject(this);
    }
     
    if (isTouching(Alien2.class))
    {
        counter.add(5);
         
        removeTouching(Alien2.class);
        getWorld().removeObject(this);
    }
}
Hpage17 Hpage17

2015/11/20

#
Gives me another terminal window
davmac davmac

2015/11/20

#
The problem is that you're calling getOneIntersectingObject after removing 'this' object from the world. Detailed explanation here.
Hpage17 Hpage17

2015/11/20

#
Okay, Thank You, That solved my problem.
You need to login to post a reply.