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

2012/5/26

How to make bullet disappear

SayMyName SayMyName

2012/5/26

#
Hello guys, right now I have a class for bullet and a class for my enemies. Inside my bullet class I`m able to make it that if the bullet hit the enemy, the enemy is removed from the world. My problem is that after it hits the enemy and removes the enemy, the bullet continue flying until it hit the edge and removes itself. How do i make it so when the bullet hit the enemy, the enemy AND the bullet removes itself. Thank you
BadatCoding BadatCoding

2012/5/26

#
I did something similar to this in my current work, have you tried writing the following when the enemy is removed in the bullet class:
getWorld().removeObject(this);
? It should be within the same statement as you have written to remove the enemy.
SayMyName SayMyName

2012/5/26

#
that doesn`t work because inside my bullet class I already have that line when the bullet reaches the end of the world
BadatCoding BadatCoding

2012/5/26

#
Please post the code of the bullet Actor
SayMyName SayMyName

2012/5/26

#
public class BlueBullet extends Mover { //declaring image variable private GreenfootImage bullet; public BlueBullet () { bullet = new GreenfootImage ("bullet.png"); this.setImage(bullet); } /** * 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() { // makes the bullet move 10 pixel move(10.0); // detects if the bullet hits a red circle, if so deduct points/kill it Actor a = getOneIntersectingObject (RedCircle.class); // detects if the bullet hits a green circle, if so deduct points/kill it Actor b = getOneIntersectingObject (GreenCircle.class); // detects if the bullet hits a yellow circle, if so deduct points/kill it Actor c = getOneIntersectingObject (YellowCircle.class); // if statements checking if the bullet hit the enemy or not if (a != null) { SpaceWorld m = (SpaceWorld)getWorld(); m.removeObject(a); } if (b != null) { SpaceWorld m = (SpaceWorld)getWorld(); m.removeObject(b); } if (c != null) { SpaceWorld m = (SpaceWorld)getWorld(); m.removeObject(c); } //calls atWorldEdge method to see if it reaches the end, if it does remove bullet if (atWorldEdge()) { getWorld().removeObject(this); } } /** * atWorldEdge() method that checks if the bullet is at the end of the world, if it is, return true, otherwise false */ public boolean atWorldEdge () { int maxX = getWorld().getBackground().getWidth(); int maxY = getWorld().getBackground().getHeight(); if (getX() <= 0 || getX() >= maxX - 1) { return true; } if (getY() <= 0 || getY() >= maxY - 1) { return true; } return false; } }
BadatCoding BadatCoding

2012/5/26

#
Ok, doing as I said should be fine, try this:
 if (a != null) {
    { 
        SpaceWorld m = (SpaceWorld)getWorld(); 
        m.removeObject(a); 
        m.removeObject(this);
        return;
    } 
    if (b != null) { 
        SpaceWorld m = (SpaceWorld)getWorld(); 
        m.removeObject(b); 
        m.removeObject(this);
        return;
    } 
    if (c != null) { 
        SpaceWorld m = (SpaceWorld)getWorld(); 
        m.removeObject(c); 
        m.removeObject(this);
        return;
    }
SayMyName SayMyName

2012/5/26

#
i still get a null pointer exception :S
danpost danpost

2012/5/26

#
Just change the statement 'if (atWorldEdge())' to 'if (atWorldEdge() || a != null || b != null || c != null)'
SayMyName SayMyName

2012/5/26

#
danpost wrote...
Just change the statement 'if (atWorldEdge())' to 'if (atWorldEdge() || a != null || b != null || c != null)'
thank you very much, it works now!
You need to login to post a reply.