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

2017/4/12

Adding new asteroids every time asteroids is cleared way

emonakter8 emonakter8

2017/4/12

#
I'm trying to add new asteroids every time one of the two asteroids is eliminated from the world. I tried many different ways but running into some type of error. its due today midnight. please any help is appreciated. thank you.
import greenfoot.*;

/**
 * A rock in space.
 * 
 * @author Poul Henriksen
 * @author Michael Kölling
 */
public class Asteroid extends SmoothMover
{
    /** Size of this asteroid */
    private int size;

    /** When the stability reaches 0 the asteroid will explode */
    private int stability;


    /**
     * Create an asteroid with default size and random direction of movement.
     */
    public Asteroid()
    {
        this(50);
    }
    
    /**
     * Create an asteroid with a given size and random direction of movement.
     */
    public Asteroid(int size)
    {
        super(new Vector(Greenfoot.getRandomNumber(360), 2));
        setSize(size);
    }
    
    /**
     * Create an asteroid with a given size and direction of movement.
     */
    public Asteroid(int size, Vector velocity)
    {
        super(velocity);
        setSize(size);
    }
    
    public void act()
    {         
        move();
        intersecting();
        if (getObjects(Asteroid.class).isEmpty())
        {
            addAsteroids(startAsteroids);
        }
    }

    /**
     * Set the size of this asteroid. Note that stability is directly
     * related to size. Smaller asteroids are less stable.
     */
    public void setSize(int size) 
    {
        stability = size;
        this.size = size;
        GreenfootImage image = getImage();
        image.scale(size, size);
    }

    /**
     * Return the current stability of this asteroid. (If it goes down to 
     * zero, it breaks up.)
     */
    public int getStability() 
    {
        return stability;
    }
    
    /**
     * Hit this asteroid dealing the given amount of damage.
     */
    public void hit(int damage) 
    {
        stability = stability - damage;
        if (stability <= 0) 
        {
            breakUp();
        }
    }
    
    /**
     * Break up this asteroid. If we are still big enough, this will create two
     * smaller asteroids. If we are small already, just disappear.
     */
    private void breakUp() 
    {
        Greenfoot.playSound("Explosion.wav");
        if (size <= 16) {
            ((Space)getWorld()).countScore(50);
            getWorld().removeObject(this);
        }
        else {
            int r = getVelocity().getDirection() + Greenfoot.getRandomNumber(45);
            double l = getVelocity().getLength();
            Vector speed1 = new Vector(r + 60, l * 1.2);
            Vector speed2 = new Vector(r - 60, l * 1.2);        
            Asteroid a1 = new Asteroid(size/2, speed1);
            Asteroid a2 = new Asteroid(size/2, speed2);
            getWorld().addObject(a1, getX(), getY());
            getWorld().addObject(a2, getX(), getY());        
            a1.move();
            a2.move();
            ((Space)getWorld()).countScore(15);
            getWorld().removeObject(this);
        }
    }

    public void intersecting() {//
        Actor rocket = getOneIntersectingObject(Rocket.class);
        if(rocket != null) {
            Actor Explosion = new Explosion();
            getWorld().addObject(Explosion,getX(), getY());
            ((Space)getWorld()).gameOver();
            Greenfoot.playSound("MetalExplosion.wav");
            getWorld().removeObject(rocket);
            getWorld().removeObject(this);
        }
    }
}
Space c;ass
import greenfoot.*;
import java.awt.Color;

/**
 * Space. Something for rockets to fly in.
 * 
 * @author Michael Kölling
 * @version 1.1
 */
public class Space extends World
{
    private Counter scoreCounter;
    private int startAsteroids = 1;
    addAsteroids(startAsteroids);
    
    /**
     * Create the space and all objects within it.
     */
    public Space() 
    {
        super(600, 500, 1);
        GreenfootImage background = getBackground();
        background.setColor(Color.BLACK);
        background.fill();
        createStars(300);
        
        Rocket rocket = new Rocket();
        addObject(rocket, getWidth()/2 + 100, getHeight()/2);
        
        addAsteroids(startAsteroids);
        
        scoreCounter = new Counter("Score: ");
        addObject(scoreCounter, 60, 480);

        Explosion.initializeImages();
        ProtonWave.initializeImages();
    }
    
    /**
     * Add a given number of asteroids to our world. Asteroids are only added into
     * the left half of the world.
     */
    private int addAsteroids(int count) 
    {
        for(int i = 0; i < count; i++) 
        {
            int x = Greenfoot.getRandomNumber(getWidth()/2);
            int y = Greenfoot.getRandomNumber(getHeight()/2);
            addObject(new Asteroid(), x, y); 
        }
        return count;
    }
        
    public int numberOfObjects();
    {
        return asteroids;
    }
    
    private void levelUp() 
   {
       int asteroids = numberOfObjects();
       if (asteroids == 0) 
       {
           Space space = (Space) getWorld();
           space.addAsteroids(2);
       }
   }

    /**
     * Crete a given number of stars in space.
     */
    private void createStars(int number) 
    {
        GreenfootImage background = getBackground();             
        for(int i=0; i < number; i++) 
        {
             int x = Greenfoot.getRandomNumber( getWidth() );
             int y = Greenfoot.getRandomNumber( getHeight() );
             int color = 120 - Greenfoot.getRandomNumber(100);
             background.setColor(new Color(color,color,color));
             background.fillOval(x, y, 2, 2);
        } 
    }
    
    public void countScore(int change)
    {
        scoreCounter.add(change);
    }
    
    /**
     * This method is called when the game is over to display the final score.
     */
    public void gameOver() 
    {
        ScoreBoard gameBoard = new ScoreBoard(); 
        addObject(gameBoard, getWidth()/2, getHeight()/2);
    }

}
Im getting error where i declared addAsteroids(startAsteroids);
Super_Hippo Super_Hippo

2017/4/12

#
What do you want to declare there? Remove the line 14. You can also remove line 51 if you change the first 'int' in line 43 to 'void', the return type doesn't seem to be useful. The 'numberOfObjects' method shouldn't compile as it is either.
danpost danpost

2017/4/12

#
If you want the world to do something while the game is running (like adding/re-adding asteroids into the world when none are present), then you need an 'act' method in the class. This is where line 14 would go; however, it should be enclosed within a conditional block. Use 'isEmpty' on the list returned by 'getObjects' to check if no asteroids are in the world for the condition.
emonakter8 emonakter8

2017/4/12

#
i figured it out last night. thanks for trying to help:)
You need to login to post a reply.