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

2015/5/17

Need help with changing color of text

acampbell24 acampbell24

2015/5/17

#
For my game I am trying to create a menu for a character selection of sorts. This part is working fine, but when i click on the text I would like to make it turn red. There are other names on the menu, and when these names are clicked I would like the currently selected one to switch to white. Currently I have a Boolean set to false and when the image is clicked on it changes to red and sets the boolean to true. The code then never runs through again to change the boolean.
public class Colonist2 extends ColonistMain
{
    int a = Greenfoot.getRandomNumber(18);
    int b = Greenfoot.getRandomNumber(4);
    private boolean click2 = false;
    public void act() 
    {
        ColonistName();
    }

    public void ColonistName()
    {
        String Fname[] = {"Anderson", "Jack", "Bryce", "Dale", "Ashton", "Gordan", "Joe", "Luke", "Steven"}; 
        String Lname[] = {"Palumbo", "Miller", "Smith", "Martinez"};
        if (a <= 18)//this is set to be the only one running so I only have to write code for this line for now
        {
            if (b <= 4)//same as if statement a
            {
                if (Greenfoot.mouseClicked(Colonist1) || Greenfoot.mouseClicked(Colonist3))
                {
                    click2=false;
                }
                else if (Greenfoot.mouseClicked(this))
                {
                    setImage(new   GreenfootImage(Fname[0] + " " + Lname[0], 24, Color.RED, new Color (85, 85, 85)));
                    click2 = true; //sets click2 equal to true
                }
                else if (click2==false)//this line checks to see if the boolean is false so this can run
                {
                    setImage(new GreenfootImage(Fname[0] + " " + Lname[0], 24, Color.WHITE, new Color (85, 85, 85)));
                }
            }
Thanks in advance for the help.
danpost danpost

2015/5/17

#
I will presume that once a Colonist object is created that 'a' and 'b' for that actor is set and not ever changed. With that, the 'if' statements on lines 15 and 17 will always be true and can just be removed. Line 19 will not work as coded. The 'mouseClicked' method requires an Actor object, not a class name. It appears you are using three different classes for basically the same type objects, where one would probably be sufficient. Please post the code to one of your other colonist classes so that a better idea of what you are dealing with can be ascertained.
acampbell24 acampbell24

2015/5/18

#
The code within the other two classes is exactly the same as this one. My game starts with the player having control over three colonists, each with a different name. I have it generate a random number to pick the name that the corresponding character will have, and use that as the image of the class. What other methods could be used here instead of mouseClicked? Here are all three Colonist classes. Code for Colonist1
public class Colonist1 extends ColonistMain
{
    int a = Greenfoot.getRandomNumber(18);
    int b = Greenfoot.getRandomNumber(4);
    private boolean click2 = false;
    public void act() 
    {
        ColonistName();
    }
 
    public void ColonistName()
    {
        String Fname[] = {"Anderson", "Jack", "Bryce", "Dale", "Ashton", "Gordan", "Joe", "Luke", "Steven"}; 
        String Lname[] = {"Palumbo", "Miller", "Smith", "Martinez"};
        if (a <= 18)//this is set to be the only one running so I only have to write code for this line for now
        {
            if (b <= 4)//same as if statement a
            {
                if (Greenfoot.mouseClicked(Colonist2) || Greenfoot.mouseClicked(Colonist3))
                {
                    click2=false;
                }
                else if (Greenfoot.mouseClicked(this))
                {
                    setImage(new   GreenfootImage(Fname[0] + " " + Lname[0], 24, Color.RED, new Color (85, 85, 85)));
                    click2 = true; //sets click2 equal to true
                }
                else if (click2==false)//this line checks to see if the boolean is false so this can run
                {
                    setImage(new GreenfootImage(Fname[0] + " " + Lname[0], 24, Color.WHITE, new Color (85, 85, 85)));
                }
            }
Code for Colonist2
public class Colonist2 extends ColonistMain
{
    int a = Greenfoot.getRandomNumber(18);
    int b = Greenfoot.getRandomNumber(4);
    private boolean click2 = false;
    public void act() 
    {
        ColonistName();
    }
 
    public void ColonistName()
    {
        String Fname[] = {"Anderson", "Jack", "Bryce", "Dale", "Ashton", "Gordan", "Joe", "Luke", "Steven"}; 
        String Lname[] = {"Palumbo", "Miller", "Smith", "Martinez"};
        if (a <= 18)//this is set to be the only one running so I only have to write code for this line for now
        {
            if (b <= 4)//same as if statement a
            {
                if (Greenfoot.mouseClicked(Colonist1) || Greenfoot.mouseClicked(Colonist3))
                {
                    click2=false;
                }
                else if (Greenfoot.mouseClicked(this))
                {
                    setImage(new   GreenfootImage(Fname[0] + " " + Lname[0], 24, Color.RED, new Color (85, 85, 85)));
                    click2 = true; //sets click2 equal to true
                }
                else if (click2==false)//this line checks to see if the boolean is false so this can run
                {
                    setImage(new GreenfootImage(Fname[0] + " " + Lname[0], 24, Color.WHITE, new Color (85, 85, 85)));
                }
            }
Code for Colonist3
public class Colonist3 extends ColonistMain
{
    int a = Greenfoot.getRandomNumber(18);
    int b = Greenfoot.getRandomNumber(4);
    private boolean click2 = false;
    public void act() 
    {
        ColonistName();
    }
 
    public void ColonistName()
    {
        String Fname[] = {"Anderson", "Jack", "Bryce", "Dale", "Ashton", "Gordan", "Joe", "Luke", "Steven"}; 
        String Lname[] = {"Palumbo", "Miller", "Smith", "Martinez"};
        if (a <= 18)//this is set to be the only one running so I only have to write code for this line for now
        {
            if (b <= 4)//same as if statement a
            {
                if (Greenfoot.mouseClicked(Colonist1) || Greenfoot.mouseClicked(Colonist2))
                {
                    click2=false;
                }
                else if (Greenfoot.mouseClicked(this))
                {
                    setImage(new   GreenfootImage(Fname[0] + " " + Lname[0], 24, Color.RED, new Color (85, 85, 85)));
                    click2 = true; //sets click2 equal to true
                }
                else if (click2==false)//this line checks to see if the boolean is false so this can run
                {
                    setImage(new GreenfootImage(Fname[0] + " " + Lname[0], 24, Color.WHITE, new Color (85, 85, 85)));
                }
            }
Super_Hippo Super_Hippo

2015/5/18

#
If all these classes have the exactly same code, why are they separate classes? You could just add three instances of one class into the world.
acampbell24 acampbell24

2015/5/18

#
The reason they are three separate classes is because they all run the if statement to pick a name. How i currently have my code setup is to only worry about the first part of the if statement. I have it so it only runs the first part so we only have to fix the one section.
danpost danpost

2015/5/18

#
danpost wrote...
I will presume that once a Colonist object is created that 'a' and 'b' for that actor is set and not ever changed. With that, the 'if' statements on lines 15 and 17 will always be true and can just be removed. Line 19 will not work as coded. The 'mouseClicked' method requires an Actor object, not a class name. It appears you are using three different classes for basically the same type objects, where one would probably be sufficient. Please post the code to one of your other colonist classes so that a better idea of what you are dealing with can be ascertained.
Everything here applies.
acampbell24 acampbell24

2015/5/18

#
Alright thanks for the help. One last questions. What other method could I use that would allow me to click on a class?
danpost danpost

2015/5/19

#
You could get a MouseInfo object if 'mouseClicked(null)' and then see if 'getActor', if not null, returns an actor of said type. However, I would just keep references to all three colonist objects in the world class and check for clicks on each one in its act method:
// world instance fields
Colonist[] colonists = {
    new Colonist(),
    new Colonist(),
    new Colonist()
}; // references to the three colonist
Colonist selectedColonist; // currently selected colonist (initialized to 'null')
// in act
for (int n=0; n<colonists.length; n++)
{
    if (Greenfoot.mouseClicked(colonists[n])) setSelectedColonist(n);
}
// with methods
private void setSelectedColonist(int n)
{
    // actions required when changing the value of the selected colonist
}
You need to login to post a reply.