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

2013/2/7

Changing Colours when Bouncing

Memises Memises

2013/2/7

#
Hello, I have been trying to make a body bounce when bouncing off an edge in Newton's Lab 3. This is to make multiple enities when they touch the edge, change colour. Here is the current 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()
        }
        
    }
    
    public void changeColor()
    {
        int g = Greenfoot.getRandomNumber(255);
        int r = Greenfoot.getRandomNumber(255);
        int b = Greenfoot.getRandomNumber(255);    
        image.setColor (color);
              
    }
    /**
davmac davmac

2013/2/7

#
setColor (line 26) just changes the drawing color for an image (i.e. what color will be used if you draw on the image). There's no way to change the color of the whole image except by redrawing it or loading a different image.
Memises Memises

2013/2/7

#
There is a way to change the colour as I am already setting the colour for the body and the size of the body, however I may make some different .png files of different colours and set it to randomly pick one. Thanks for your help anyways.
danpost danpost

2013/2/7

#
True, you are setting the drawing color. However, there is no code shown where the images are altered by any of the GreenfootImage drawing commands ('fill', 'drawLine', 'drawRect', 'fillOval' and the like).
Memises Memises

2013/2/14

#
Thanks, I got the code to work with the help from a friend and using the "fillOval" command.
You need to login to post a reply.