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

2018/4/12

Having Trouble with Space, Counter, and Explosion

razornale razornale

2018/4/12

#
On the Scenario asteroid I am having trouble with why it won't accept the color on my code for Space, the textColor for my counter and a new GreenfootImage(); on Explosion here are my codes
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)
import java.awt.Color;

/**
 * Space. Something for rockets to fly in.
 * 
 * 
 * @version 1.0
 */
public class Space extends World
{
    private Counter scoreCounter;
    private int startAsteroids = 3;

    public Space() 
    {
        super(800, 700, 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, 380);

        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 void 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);
        }
    } 
     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);
        }
    }
    /**
     * This method is called when the game is over to display the final score.
     */
    public void gameOver() 
    {
        addObject(new ScoreBoard(getCounter().getValue()), getWidth()/2, getHeight()/2);
    }
    public Counter getCounter()
    {
        
       return scoreCounter;
     }

}
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics;
/**
 * Counter that displays a text and number.
 * 
 * 
 * @version 1.0.1
 */
public class Counter extends Actor
{
    private static final Color textColor = new Color(255, 180, 150);
    
    private int value = 0;
    private static int target = 0;
    private String text;
    private int stringLength;

    public Counter()
    {
        this("0");
    }

    public Counter(String prefix)
    {
        text = prefix;
        stringLength = (text.length() + 2) * 10;

        setImage(new GreenfootImage(stringLength, 16));
        GreenfootImage image = getImage();
        image.setColor(textColor);

        updateImage();
    }
    
    public void act() {
       
        if(value < target) {
            value++;
            updateImage();
        }
        else if(value > target) {
            value--;
            updateImage();
        }
    }

    public static void add(int score)
    {
        target += score;
    }

    public int getValue()
    {
        return value;
    }
   
    /**
     * Make the image
     */
    private void updateImage()
    {
        GreenfootImage image = getImage();
        image.clear();
        image.drawString(text + value, 1, 12);
    }
}
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)

/**
 * An explosion. It starts by expanding and then collapsing. 
 * The explosion will explode other obejcts that the explosion intersects.
 * 
 * 
 * @version 1.0.1
 */
public class Explosion extends Actor
{
    /** How many images should be used in the animation of the explostion */
    private final static int IMAGE_COUNT= 12;
    
    /** 
     * The images in the explosion. This is static so the images are not
     * recreated for every object (improves performance significantly).
     */
    private static GreenfootImage images;
    
    /** Current size of the explosion */
    private int imageNo = 0;
    
    /** How much do we increment the index in the explosion animation. */
    private int increment=1;
    
    /**
     * Create a new explosion.
     */
    public Explosion() 
    {
        initializeImages();
        setImage(images);
        Greenfoot.playSound("MetalExplosion.wav");
    }    
    
    /** 
     * Create the images for explosion.
     */
    public synchronized static void initializeImages() 
    {
        if(images == null) {
            GreenfootImage baseImage = new GreenfootImage("explosion-big.png");
            images = new GreenfootImage();
            for (int i = 0; i < IMAGE_COUNT; i++)
            {
                int size = (i+1) * ( baseImage.getWidth() / IMAGE_COUNT );
                images = new GreenfootImage(baseImage);
                images.scale(size, size);
            }
        }
    }
    
    /**
     * Explode!
     */
    public void act()
    { 
        setImage(images);

        imageNo += increment;
        if(imageNo >= IMAGE_COUNT) {
            increment = -increment;
            imageNo += increment;
        }
        
        if(imageNo < 0) {
            getWorld().removeObject(this);
        }
    }
}
Thanks in advance for any help that you may be able to give!
danpost danpost

2018/4/12

#
Remove all import lines beginning with "java.awt."
razornale razornale

2018/4/12

#
Thank you My codes for space and counter compile. I'm still having a error message on line 44 GreenfootImage(); is coming back with an error message of GreenfootImage() has private access in greenfoot.GreenfootImage what is it wanting? I'm confused.
razornale razornale

2018/4/12

#
That's from my Explosion code BTW
danpost danpost

2018/4/12

#
razornale wrote...
I'm still having a error message on line 44 GreenfootImage(); is coming back with an error message of GreenfootImage() has private access in greenfoot.GreenfootImage
Firstly, there is no GreenfootImage constructor that has zero parameters. Secondly, you are not yet wanting to create an image -- that is done within the following for loop. You may, however, want to declare the variable to hold a GreenfootImage at that time (although that is not mandatory as you can declare it inside the loop where it would be declared each iteration of the loop). You can just change line 44 to the following:
GreenfootImage images = null;
or, as the alternative, you can remove line 44 and change line 48 to:
GreenfootImage images = new GreenfootImage(baseImage);
You need to login to post a reply.