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

2014/4/1

Chapter 6, Exercise 6.34

hdeforest hdeforest

2014/4/1

#
We are working on the Greenfoot book scenarios, and we can't figure out exercise 6.34. We are able to get our "planets" to change color, but after the first time it hits the edge, they turn into squares. Does anyone know how to make it without change the shape of the "planets?" Here is our code:
    private void bounceAtEdge()
    {
        if (getX() == 0 || getX() == getWorld().getWidth()-1) {
            setLocation((double)getX(), (double)getY());
            getMovement().revertHorizontal();
            accelerate(0.9);
            changeColor();
        }
        else if (getY() == 0 || getY() == getWorld().getHeight()-1) {
            setLocation((double)getX(), (double)getY());
            getMovement().revertVertical();
            accelerate(0.9);
            changeColor();
        }
        
    }
    private void changeColor()
 
    {
        int r = Greenfoot.getRandomNumber(255);
        int g = Greenfoot.getRandomNumber(255);
        int b = Greenfoot.getRandomNumber(255);
        GreenfootImage image = new GreenfootImage ("Body.png");
        image.setColor (new Color(r,g,b));
        image.fill();
        setImage (image);
    }
You need to login to post a reply.