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

2018/11/15

Keep on getting error

Will5230 Will5230

2018/11/15

#
Im trying to make a basic space invaders game but the bullet from the spaceship keeps getting an error when it hits the alien. It doesn't show me the error and the world just pauses. Here is the spaceship class
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Spaceship here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Spaceship extends Actor
{
    int shootTimer = 0;

    public void act()
    {
        Move();
        Shoot();
        hit();
        shootTimer++;
    }

    public Spaceship() 
    {
        GreenfootImage image = getImage();  
        image.scale(75, 75);
        setImage(image);

    } 

    public void Move()
    {
        int x = getX();
        int y = getY();

        if(Greenfoot.isKeyDown("left")){
            setLocation(x-4, y);
        }else if(Greenfoot.isKeyDown("right")){
            setLocation(x+4, y);
        }    
    }

    public void Shoot()
    {

        if (Greenfoot.isKeyDown("up") & shootTimer > 30)   
        {
            Bullet bullet = new Bullet();
            getWorld().addObject(bullet, getX(), getY());
            bullet.getImage().scale(15, 15);
            bullet.setRotation(getRotation()-90);
            bullet.move(45);
            shootTimer = 0;
        }
    }

    public void hit()
    {  
        Actor Alienbullet;
        Alienbullet = getOneObjectAtOffset(0, 0, Alienbullet.class);
        if(Alienbullet !=null)
        {
            World world;
            world = getWorld();
            getWorld().removeObject(Alienbullet);
            ((SpaceWorld)world).HealthBar.add(-10);
        }
    }
}
This is the bullet class
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
{
    Alien1 Alien1;
    public void act() 
    {
        move (8);
        eat1();
        if(isAtEdge())
        {
            getWorld().removeObject(this);
        }
     
    }

    public Bullet()
    {
        GreenfootImage image = getImage();  
        image.scale(15, 15);
        setImage(image);
    }

    public void eat1()
    {
        Actor Alien1 = getOneObjectAtOffset(0, 0,Alien1.class);
        if(Alien1 != null)
        {
          World world = getWorld();
          world.removeObject(Alien1);
          world.removeObject(this);
          ((SpaceWorld)world).counter.add(10);
        }
    } 

}
and here is the world code
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class MyWorld here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class SpaceWorld extends greenfoot.World
{
    Counter counter;
    HealthBar HealthBar;
    Alienbullet AlienBullet;

    /**
     * Constructor for objects of class MyWorld.
     * 
     */
    public SpaceWorld()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 900, 1); 
        prepare();
    }

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        Alien1 alien1 = new Alien1();
        addObject(alien1,400,245);
        alien1.setLocation(401,235);
        removeObject(alien1);
        Spaceship spaceship = new Spaceship();
        addObject(spaceship,372,734);
        spaceship.setLocation(405,807);
        spaceship.setLocation(398,804);
        spaceship.setLocation(402,820);
        spaceship.setLocation(398,828);
        counter = new Counter();
        addObject(counter,720,843);

        HealthBar = new HealthBar();
        addObject(HealthBar,75,860);
        Alien1 alien12 = new Alien1();
        addObject(alien12,357,178);

    }


}
Thanks
Super_Hippo Super_Hippo

2018/11/15

#
Check for intersection in one class, not in both. The reason for the error is that you remove the bullet and then check if it is at the edge of the world. But it isn't in any world at this point.
danpost danpost

2018/11/15

#
Will5230 wrote...
Im trying to make a basic space invaders game but the bullet from the spaceship keeps getting an error when it hits the alien. It doesn't show me the error and the world just pauses. << Codes Omitted >>
I am sure your terminal is showing a IllegalStateExcption error. Line 16 calls the isAtEdge method, which fails when eat1 (line 15) causes the bullet to be removed from the world. You just need to make sure the bullet is still in the world first. Change line 16 to:
if (getWorld() != null && isAtEdge())
Will5230 Will5230

2018/11/16

#
That worked Thanks alot guys!!!
You need to login to post a reply.