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

2012/10/31

Need help

ryanr4788 ryanr4788

2012/10/31

#
I keep getting an error message because the act method tries to remove the bullet twice....I need to have the bullet removed in both cases. 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 { /** * 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()-5); checkHeight(); checkCollision(); } private void checkCollision() { Actor a = getOneIntersectingObject(Asteroid.class); Actor b = getOneIntersectingObject(Bullet.class); if (a != null) { World world = getWorld(); world.removeObject(a); world.removeObject(this); Greenfoot.playSound("explosion.wav"); }} private void checkHeight() { if(getY()<=37) { World world= getWorld(); world.removeObject(this); } } }
Gevater_Tod4711 Gevater_Tod4711

2012/10/31

#
you could change the method checkHeight() into a boolean:
private boolean checkHeight() {
    if(getY()<=37) {
        World world= getWorld();
        world.removeObject(this);
        return true;
    }
    return false;
}
then you could change the act method like this:
public void act() {
    setLocation(getX(), getY()-5);
    if (!checkHeight()) {
        checkCollision();
    }
} 
You need to login to post a reply.