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

2015/11/20

Help with Greenfoot-generated images

I am trying to create a graphical representation of the reload state of the Proton Wave weapon from the Asteroids senario. I don't know if I should have it as a Counter, generated from the World, or it's own class. Here is my current code:
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)

import java.awt.Color;
import java.awt.Graphics;
import java.util.List;
import java.awt.Font;

/**
 * Space. Something for rockets to fly in.
 * 
 * @author Michael Kolling
 * @version 1.0
 */
public class Space extends World
{
    private Counter scoreCounter;
    private Counter protonCounter;
    private Counter levelCounter;
    private GreenfootSound endSound = new GreenfootSound("EndLevel.wav");
    private protonBar;
    
    private int startAsteroids;
    private int level = 1;
    private int asteroidsDestroyed = 0;
    private int asteroidsObjective;
    private int reloadState;
public Space() 
    {
        super(1280, 720, 1);
        GreenfootImage background = getBackground();
        background.setColor(Color.BLACK);
        background.fill();
        
        loadLevel();
        
        Rocket rocket = new Rocket();
        addObject(rocket, getWidth()/2 + 100, getHeight()/2);
        
        //addAsteroids(startAsteroids);
        addAsteroidsUsingWhile(startAsteroids);
        
        scoreCounter = new Counter("Score: ");
        addObject(scoreCounter, 60, getHeight()-40);
        
        protonCounter = new Counter("Proton Waves: ");
        addObject(protonCounter, 95, getHeight()-20);
        
        levelCounter = new Counter("Level ");
        addObject(levelCounter, 1250, getHeight()-20);
        
        Explosion.initializeImages();
        ProtonWave.initializeImages();
        
        //createStars(300);
        createRandomStars(300);
        
        countProton(3);
        countLevel(1);
        protonOutline();
        
        
    }
    public void act()
    {         
        loadLevel();
        protonBar();
    }

private void protonOutline()
    {
        GreenfootImage rect = getBackground();
        Color color = new Color(0, 250, 255);
        rect.setColor(color);
        rect.drawRect(19, 19, 101, 6);
        
        Font font = new Font("Comic Sans MS", Font.PLAIN, 16);
        Color textColor = new Color(0, 180, 255);
        rect.setFont(font);
        rect.setColor(textColor);
        rect.drawString("Proton Wave", 23, 47);
    }
    
    private void protonBar()
    {
        GreenfootImage rect = getBackground();
        
        Color color = new Color(0, 128, 255);
        rect.setColor(color);
        protonBar = rect.fillRect(20, 20, reloadState, 5);
    }
    
    public void setProtonRecharge (int percent)
    {
        reloadState = percent;
    }
The percent variable is set from the rocket class via that last method. It goes up to 100, makes a rectangle/bar and can be sent as soon as the bar is at 100. The problem is when I set the percent to 0 after the wave is launched, it stays full. I think this because I am creating an image every act cycle.
danpost danpost

2015/11/20

#
I believe the problem is that when you draw the bar for the new value, you are not doing anything about the bar that was previously drawn. It is not replaced when you draw a new bar, but the new bar, which may have less length, is drawn over part of the old drawn bar. In other words you need to "remove" the old bar before drawing the new one. To avoid drawing every act cycle, call the 'protonBar' method from the constructor and from the 'setProtonRecharge' method (at the end of both).
You need to login to post a reply.