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

2013/2/6

HELP! COLOUR CHANGE WHEN BOUNCEATEDGE

USER67 USER67

2013/2/6

#
How do you code a random color change for an object that occurs when it hits the edge of the world???????????????
paul10y paul10y

2013/2/6

#
well, one questionmark would have been enough
Gevater_Tod4711 Gevater_Tod4711

2013/2/6

#
To get a random color you just have to create a new color with random values:
import java.awt.Color;

//...

Color randomColor = new Color(Greenfoot.getRandomNumber(255), Greenfoot.getRandomNumber(255), Greenfoot.getRandomNumber(255), 255);
To check whether the object is at the edge of the world you can use this method:
    public boolean atWorldEdge() {
        if(getX() < 20 || getX() > getWorld().getWidth() - 20) {
            return true;
        }
        if(getY() < 20 || getY() > getWorld().getHeight() - 20) {
            return true;
        }
        return false;
    }
Then you just need to change the image of your actor to the given Color.
JTSCADET JTSCADET

2013/10/18

#
import java.awt.Color;
//...
Color randomColor = new Color(rc, rc, rc);  
        if (getX() == 0 || getX() == getWorld().getWidth()-1) {
            setLocation((double)getX(), (double)getY());
            getMovement().revertHorizontal();
            accelerate(0.9);
            image.setColor(randomColor);
                    
         }
        else if (getY() == 0 || getY() == getWorld().getHeight()-1) {
            setLocation((double)getX(), (double)getY());
            getMovement().revertVertical();
            accelerate(.9);
            image.setColor(randomColor);
            
         }
I am trying to do the same thing but when i compile it says "image has private access in greenfoot.actor"
Gevater_Tod4711 Gevater_Tod4711

2013/10/18

#
Try replacing 'image' trough 'getImage()'. This method returns the image of your actor.
You need to login to post a reply.