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

2013/12/16

Unknown error message

GreenAnthony GreenAnthony

2013/12/16

#
Hi, everybody well I programed a game like space invaders but after I finished it I got the idea of adding a few new things and thats what I did. However now it shows a few different error messages, I got nobody to help me so I decided to post the first error message that appears on the screen when an enemy reaches the end of the world. Here are the lines: public class EnemyRocket extends Animal { int changeSize = 1; int LifeCounter = 3; int counter = 0; /** * Act - do whatever the EnemyRocket 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); if (counter >= 200) { setLocation(getX(), getY()+7); } if (counter >= 1000) { setLocation(getX(), getY()+12); } if (counter >= 20) { setLocation(getX(), getY()+4); } if (changeSize == 1) { GreenfootImage image = getImage(); image.scale(image.getWidth() - 50, image.getHeight() - 50); setImage(image); changeSize = 0; } if (canSee(MainRocket.class)) { getWorld().removeObject(this); } if (atWorldEdge()) { getWorld().removeObject(this); getWorld().addObject(new EnemyRocket(), Greenfoot.getRandomNumber(800),Greenfoot.getRandomNumber(170) ); } if (getWorld() == null) { return; } if (canSee(EnemyRocket.class)) { getWorld().addObject(new EnemyRocket(), Greenfoot.getRandomNumber(800),Greenfoot.getRandomNumber(170) ); getWorld().removeObject(this); } counter++; } } It always says that getWorld().addObject(new EnemyRocket(), Greenfoot.getRandomNumber(800),Greenfoot.getRandomNumber(170) ); is an unknown error. Can you guys see a mistake here?
Game/maniac Game/maniac

2013/12/16

#
The code you posted is making my head spin, please put it into a code box by using the code button below the text field
Game/maniac Game/maniac

2013/12/16

#
Never mind, it looks like my phone was just messing with me
GreenAnthony GreenAnthony

2013/12/16

#
}
public class EnemyRocket extends Animal
{
    int changeSize = 1;
    int LifeCounter = 3;
    int counter = 0;
    /**
     * Act - do whatever the EnemyRocket 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);
        if (counter >= 200)
        {
            setLocation(getX(), getY()+7);
        }
        if (counter >= 1000)
        {
            setLocation(getX(), getY()+12);
        }
        if (counter >= 20)
        {
            setLocation(getX(), getY()+4);
        }
        if (changeSize == 1)
        {
            GreenfootImage image = getImage();
            image.scale(image.getWidth() - 50, image.getHeight() - 50);
            setImage(image);
            changeSize = 0;
        }    
        if (canSee(MainRocket.class))
        {
            getWorld().removeObject(this);
        }
        if (atWorldEdge())
        {
            getWorld().removeObject(this);
            getWorld().addObject(new EnemyRocket(), Greenfoot.getRandomNumber(800),Greenfoot.getRandomNumber(170) );
        }
        if (getWorld() == null)
        {
            return;
        }
        if (canSee(EnemyRocket.class))
        {
            getWorld().addObject(new EnemyRocket(), Greenfoot.getRandomNumber(800),Greenfoot.getRandomNumber(170) );
            getWorld().removeObject(this);
        } 
        counter++;
    }
}
danpost danpost

2013/12/16

#
The problem is that there are several places in your code where the actor (referred to by 'this') is removed from the world. If any one of them is executed, any later statements that require the actor to be in the world will cause an error ('getWorld' will return 'null' and any world class method called on it will cause a 'nullPointerException'; and, 'getX' and 'getY' are meaningless and will cause an 'illegalStateException'). Lines 38 and 39 above is an example of the first case; where the actor is removed from the world in one line and then 'getWorld' is used on the next. The same type error will occur if line 34 ever gets executed as well. The easiest way to rectify this is as follows (starting at line 32 of your code above):
;       if (canSee(MainRocket.class))
        {
            getWorld().removeObject(this);
            return; // added this line 
        }
        if (atWorldEdge())
        {
            getWorld().addObject(new EnemyRocket(), Greenfoot.getRandomNumber(800),Greenfoot.getRandomNumber(170) );
            getWorld().removeObject(this); // switch this line with the previous line
            return; // added this line
        }
        // commented out the following 4 lines (they can be removed)
        //        if (getWorld() == null)
        //        {
        //            return;
        //        }
        if (canSee(EnemyRocket.class))
        {
            getWorld().addObject(new EnemyRocket(), Greenfoot.getRandomNumber(800),Greenfoot.getRandomNumber(170) );
            getWorld().removeObject(this);
            return; // added this line
        } 
        counter++;
    }
}
You need to login to post a reply.