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

2016/8/11

fade issue

davemib123 davemib123

2016/8/11

#
I've created a fade and get an error:
java.lang.IllegalArgumentException: The transparency value has to be in the range 0 to 255. It was: -1
This is my code:
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;

/**
 * Write a description of class Transition here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Transition extends SharedMembers
{
    private final GreenfootImage transition = new  GreenfootImage(600, 400);
    int fadeUpperLimit = 255;
    int fadeLowerLimit = 25;

    public Transition(String selection)
    {
        
        if (selection == "white")
        {
            this.xSpeed = 1;
            transition.setColor(java.awt.Color.WHITE);
        }
        else if (selection == "black")
        {
            this.xSpeed = 5;
            transition.setColor(java.awt.Color.BLACK);
        }
        transition.fill();
        setImage(transition);
        transition.setTransparency(fadeUpperLimit);
    }

    /**
     * Act - do whatever the Transition wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        fade();
    }    

    private void fade()
    {
        GreenfootImage transition = getImage();
        transition.setTransparency(transition.getTransparency() - this.xSpeed);
        if (transition.getTransparency() <= fadeLowerLimit)
        {
            getWorld().removeObject(this);
        }
        
    }
}
Could someone take a look to see where I have gone wrong? Thanks in advance.
davemib123 davemib123

2016/8/11

#
very strange. If I add as an object into the world it gives the above error. But if its called from an object I have no error.
danpost danpost

2016/8/11

#
davemib123 wrote...
very strange. If I add as an object into the world it gives the above error. But if its called from an object I have no error.
Try removing line 45. The image referenced by 'transition' IS the image of the object already. I am not positive this will solve your issue; but, it is certainly worth the try, considering.
davemib123 davemib123

2016/8/11

#
yea that worked! Thank you.
You need to login to post a reply.