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

2014/4/29

Issue with switch

theiam79 theiam79

2014/4/29

#
I'm having an issue with this switch. The collision method is exactly the same as canSee from animal class in all the examples. The spawn method just adds an object with some randomness, and when I call just spawn from the act method there are no issues, but from the switch it adds multiple (anywhere from 5 to 30) asteroids and then throws an error, neither of which it should do. the error is java.lang.NullPointerException at Asteroid.spawn(Asteroid.java:111) at Asteroid.collideAsteroid(Asteroid.java:79) at Asteroid.act(Asteroid.java:25) and always points to one of the spawn calls in the switch. Maybe I'm missing something obvious but any help would be appreciated. If you need any other code or have questions let me know. Thanks!
public void collideAsteroid()
    {

        if(collision(Asteroid.class) && blownUp==false)
        {

            getWorld().removeObjects(getObjectsAtOffset(0,0,null));
            //setImage("Explosion1.png");

            blownUp=true;

            asteroidSpeed=0;

            
        }
        
        if (blownUp=true)
        {
            whereToSpawn=(Greenfoot.getRandomNumber(10));

            switch(whereToSpawn)
            {
                case 0:
                spawn(0,0);
                break;
                case 1:
                spawn(0,80);
                break;
                case 2:
                spawn(0,160);
                break;
                case 3:
                spawn(0,240);
                break;
                case 4:
                spawn(0,320);
                break;
                case 5:
                spawn(0,400);
                break;
                case 6:
                spawn(0,480);
                break;
                case 7:
                spawn(0,560);
                break;
                case 8:
                spawn(0,640);
                break;
                case 9:
                spawn(0,720);
                break;

                default:
                spawn(0,0);
                break;

            }
            blownUp=false;
        }
danpost danpost

2014/4/29

#
It would certainly help to see the 'spawn' method; also, indicate which line is 111 in the Asteroid class. The following snippet will do the same thing as the code you posted:
public void collideAsteroid()
{
    if (collision(Asteroid.class) && !blownUp)
    {
        getWorld().removeObjects(getObjectsAtOffset(0, 0, null));
        //setImage("Explosion1.png");
        blownUp = true;
        spawn(0, 80*Greenfoot.getRandomNumber(10));
        asteroidSpeed = 0;
    }
}
The only difference is this code does not reset 'blownUp' back to 'false' like your code does. I am not entirely sure that you wanted to do that anyway; and, that may be why you were getting multiple additional asteroids.
theiam79 theiam79

2014/5/1

#
here is the spawn method
public void spawn(int maxX, int maxY)
    {
        getWorld().addObject(new Asteroid(), (maxX+10), (Greenfoot.getRandomNumber(80)+maxY));
    }
I'll try taking out the blownUp and see what happens
danpost danpost

2014/5/1

#
When two asteroids collide what exactly do you want to happen. Are both asteroids (or is just one of the two) to be removed from the world and a new one spawned at some random location? and if just one, is the other supposed to stop moving? Are the new asteroids to be spawned along the left edge of the world between 10 and 20 pixels from that edge?
theiam79 theiam79

2014/5/1

#
Figured it out. Simple ordering, respawn before removing the old asteroids (duh). What happens when two collide is the picture changes, for effect, a new asteroid respawns 10 pixels in from the left at a random height, and then the two old asteroids disappear.
You need to login to post a reply.