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

2021/2/25

Changing color of text

BlueHand BlueHand

2021/2/25

#
Hello! I have a string of text in my instructions class that says "One Ball!" Its color depends on an integer called "imgNum". For example, when imgNum is equal to 1, the text is blue. So far, my instructions class looks likes this:
public class Instructions extends Actor
{
    /**
     * Act - do whatever the Instructions wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public Instructions(String message)
    {
        setImage(new GreenfootImage (message, 56, Color.BLACK, Color.WHITE));
    }    
}
and my ball class looks like this:
if (myCounter.getScore() == MINSCORE )
        {
            if(imgNum == 0)
            {
                //blue text
            }
            Instructions instruct = new Instructions("One ball!");
            Greenfoot.stop();
            getWorld().addObject(instruct, getWorld().getWidth()/2, getWorld().getHeight()/2);
        }
How can I change the color of the text? There are also other colors than blue like orange and pink
danpost danpost

2021/2/25

#
You will need to have the Instructions object retain the message for later use (add a String field and assign message to it) and then add a setColor method to the class that acquires a color (via parameter argument) and creates and sets an appropriately colored new image.
You need to login to post a reply.