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

2014/7/11

Pheromones blink instead of shrinking and fading

PGKCoder PGKCoder

2014/7/11

#
There are two issues with this code. First the getImage() code that was supplied for the course had to be remarked out before the pheromone image would display. The second issue is in the updateImage() we calculate the size of the Pheromone and the Transparency level. When the code executes the pheromone simply blinks and disappears instead of shrinking and fading. Any suggestions will be appreciated.
import greenfoot.*;  // (World, Actor, GreenfootImage, and Greenfoot)
import java.awt.Color;

/**
 * Pheromones are dropped by ants when they want to communicate something to
 * other ants.
 * 
 * @author Michael Kolling
 * @version 1.1
 */
public class Pheromone extends Actor
{
    public GreenfootImage image;
    public static final int IMAGE_SIZE = 30;
    private final static int MAX_INTENSITY = 180;
    private static final int MAX_TRANSPARENCY = 255; 
    private int intensity;
    private int currentSize;
    private int currentTransparency;
    /**
     * Create a new drop of pheromone with full intensity.
     */
    public Pheromone()
    {
        intensity = MAX_INTENSITY;
        updateImage();
    }

    /**
     * The pheromone decreases the intensity. When the intensity reaches zero, it disappears.
     */
    public void act()
    {
        fade();
        updateImage();
    }

        /**
     * Decrease the intensity of the pheromone and
     * remove it if the intensity is zero
     */
    public void fade()
    {        
        // Decrease the intensity of the Pheromone by 1
        intensity -= 1;
        // If its intensity is less than or equal to 0, remove it from AntWorld
        if (intensity <= 0) {
            getWorld().removeObject(this);
        }
    }
    /**
     * Update the size and transparency of the pheromone
     */

    public void updateImage() 
    {
        GreenfootImage image = new GreenfootImage(IMAGE_SIZE,IMAGE_SIZE);
        currentSize = IMAGE_SIZE * (intensity/MAX_INTENSITY);
        currentTransparency = MAX_TRANSPARENCY * (intensity/MAX_INTENSITY); 
        // Make the image white. Use the constant java.awt.Color.WHITE
        image.setColor(new Color(255,255,255, currentTransparency));
        // Create a circle of right size to represent the Pheromone
        image.fillOval(0, 0, currentSize, currentSize);
        setImage(image);
    }
    /**
     * Return the image object
     */
    //public GreenfootImage getImage() 
    //{
        //return image;
    //}

}
danpost danpost

2014/7/11

#
Because all your fields are 'int' fields, '(intensity/MAX_INTENSITY)' on line 59 will be one when called from the constructor. But, once the 'fade' method subtracts one from 'intensity', it will be zero. Believe it or not, but this is not difficult to deal with -- just remove the enclosing parenthesis. Change line 59 to:
currentTransparency = MAX_TRANSPARENCY * intensity / MAX_INTENSITY;
The multiplication will be done first and the result will gradually decrease instead of jumping right to zero.
PGKCoder PGKCoder

2014/7/11

#
Great, I have the whole program completed thanks to your help. Best,
You need to login to post a reply.