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

2016/5/23

Problem with Space Invaders!

Yvonne Yvonne

2016/5/23

#
It said it can't find symbol- List class, idk how to fix it. Thank you very much. New to Greenfoot.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Space here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Space extends World
{
private static final int Width= 640;
private static final int Height= 480;
private static final int Row_length= 500;
private static final int Firing_interval=30;
private int actCounter=0;
    /**
     * Constructor for objects of class Space.
     * 
     */
    public Space()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(Width, Height, 1); 
        
        prepare();
    }
    
    private void prepare()
    {
        Shooter shooter= new Shooter();
        addObject(shooter, Width/2, Height-40);
        
        for(int i=0; i<11; i++){
        addObject(new Alien(),70+i*Row_length/10,20);
    }
}

   private void addAliens()
{
 actCounter++;
     if (actCounter > Firing_interval){
        List aliens= getObjects(Alien.class);
        if(!aliens.isEmpty()){
            int number_of_aliens=aliens.size();
            int i= Greenfoot.getRandomNumber(number_of_aliens);
            Alien randomAlien= (Alien)aliens.get(i);
            randomAlien.shoot();
        }
        actCounter=0;
    }
 }
} 
danpost danpost

2016/5/23

#
The List class is a utility class that is not automatically included into your project. It must either be imported, using the following line at or near the top of the class (and outside the block of the class):
import java.util.List;
or you can refer to it directly in the code by changing line 42 to this:
java.util.List aliens = getObjects(Alien.class);
Yvonne Yvonne

2016/5/25

#
Omg! thank you so much! it works now!!:)
Yvonne Yvonne

2016/5/26

#
I am sorry, but I am not sure why the Aliens are not shooting. Is the code wrong?
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
{
    private static final int Step_size=10;
    private static final int Default_direction=270;
  
    public Bullet()
    {
        this(Default_direction);
    }
    
    public Bullet(int Direction)
    {
        setRotation(Direction);
    }
    
    /**
     * Act - do whatever the ShooterBullet wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
       move(Step_size);
       if (getY()< Step_size){
           getWorld().removeObject(this);
        }
    }
}
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class AlienBullet here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class AlienBullet extends Bullet
{
    private static final int Default_Direction=90;
    
    public AlienBullet()
    {
        this(Default_Direction);
    }
    
    public AlienBullet(int direction)
    {
        setRotation(270);
    }
}
 
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Alien here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Alien extends Actor
{
    /**
     * Act - do whatever the Alien wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        Actor bullet=getOneIntersectingObject(ShooterBullet.class);
        if(bullet != null){
            getWorld().removeObject(this);
    }    
}
    public void shoot()
    {
        getWorld().addObject(new AlienBullet(),getX(),getY());
    }
}
danpost danpost

2016/5/26

#
Yvonne wrote...
I am not sure why the Aliens are not shooting. Is the code wrong?
The 'shoot' method in the Alien class is not being called at all. You should probably have it called from the act method under some random condition.
You need to login to post a reply.