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

2013/11/2

help when is use code remove object my world stops

arnchi arnchi

2013/11/2

#
help when is use code remove object my world stops. the error say the actor where i am asking location isn't in world. i use this code import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot) /** * A bullet that can hit bal. * * */ public class Bullet extends Mover { /** A bullet looses one life each act, and will disappear when life = 0 */ private int life = 75; private int setnumber; /** The damage this bullet will deal */ private int damage = 16; /** public Bullet() { } */ public Bullet(Vector speed, int rotation) { super(speed); setRotation(rotation); increaseSpeed(new Vector(rotation, 35)); /** Greenfoot.playSound("EnergyGun.wav"); */ } /** * The bullet will damage Body if it hits them. */ public void act() { if(life <= 0) { } else { move(); Ball Ball = (Ball) getOneIntersectingObject(Ball.class); if(Ball != null){ getWorld().addObject(new Ball2(), getX(), getY()); getWorld().addObject(new Ball2(), getX(), getY()); getWorld().removeObject(Ball); getWorld().removeObject(this); } else { life--; } Ball2 Ball2 = (Ball2) getOneIntersectingObject(Ball2.class); if(Ball2 != null){ getWorld().addObject(new Ball3(), getX(), getY()); getWorld().addObject(new Ball3(), getX(), getY()); getWorld().removeObject(Ball2); getWorld().removeObject(this); } else { life--; } Ball3 Ball3 = (Ball3) getOneIntersectingObject(Ball3.class); if(Ball3 != null){ getWorld().addObject(new Ball4(), getX(), getY()); getWorld().addObject(new Ball4(), getX(), getY()); getWorld().removeObject(Ball3); getWorld().removeObject(this); } else { life--; } Ball4 Ball4 = (Ball4) getOneIntersectingObject(Ball4.class); if(Ball4 != null){ getWorld().addObject(new Ball5(), getX(), getY()); getWorld().addObject(new Ball5(), getX(), getY()); getWorld().removeObject(Ball4); getWorld().removeObject(this); } else { life--; } Ball5 Ball5 = (Ball5) getOneIntersectingObject(Ball5.class); if(Ball5 != null){ getWorld().removeObject(Ball5); getWorld().removeObject(this); } else { life--; } } checkCollision(); } /** * Check whether we are colliding with Obstacle. */ private void checkCollision() { Obstacle a = (Obstacle) getOneIntersectingObject(Obstacle.class); if(a != null) { getWorld().removeObject(this); } } /** * Check whether we are colliding with Obstacle2. private void checkCollision() { Obstacle2 a = (Obstacle2) getOneIntersectingObject(Obstacle2.class); if(a != null) { getWorld().removeObject(this); } } */ }
Zamoht Zamoht

2013/11/2

#
Change your act method to the following should work:
    public void act()
    {
        if(life <= 0) {

        } 
        else {

            move();
            Ball Ball = (Ball) getOneIntersectingObject(Ball.class);
            if(Ball != null){

                getWorld().addObject(new Ball2(), getX(), getY());
                getWorld().addObject(new Ball2(), getX(), getY());
                getWorld().removeObject(Ball);
                getWorld().removeObject(this);
                return;
                
            }
            else {
                life--;
            }
            Ball2 Ball2 = (Ball2) getOneIntersectingObject(Ball2.class);
            if(Ball2 != null){
                getWorld().addObject(new Ball3(), getX(), getY());
                getWorld().addObject(new Ball3(), getX(), getY());
                getWorld().removeObject(Ball2);
                getWorld().removeObject(this);
                return;

            }
            else {
                life--;
            }
            Ball3 Ball3 = (Ball3) getOneIntersectingObject(Ball3.class);
            if(Ball3 != null){

                getWorld().addObject(new Ball4(), getX(), getY());
                getWorld().addObject(new Ball4(), getX(), getY());
                getWorld().removeObject(Ball3);
                getWorld().removeObject(this);
                return;

            }
            else {
                life--;
            }
            Ball4 Ball4 = (Ball4) getOneIntersectingObject(Ball4.class);
            if(Ball4 != null){

                getWorld().addObject(new Ball5(), getX(), getY());
                getWorld().addObject(new Ball5(), getX(), getY());
                getWorld().removeObject(Ball4);
                getWorld().removeObject(this);
                return;

            }
            else {
                life--;
            }
            Ball5 Ball5 = (Ball5) getOneIntersectingObject(Ball5.class);
            if(Ball5 != null){

                getWorld().removeObject(Ball5);
                getWorld().removeObject(this);
                return;

            }
            else {
                life--;
            }
        }
        checkCollision(); 
    }
arnchi arnchi

2013/11/2

#
omg thnks you helped me so much
You need to login to post a reply.