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

2015/9/15

Change image of a class based on the value of a variable from another class?

cactusButt cactusButt

2015/9/15

#
So basically, I'm trying to make a simple "Create a character" screen like this The code for an arrow, in this case for now the left arrow for the hair is this:
import greenfoot.*;

/**
 * Write a description of class LeftArrowH here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class LeftArrowH extends HairArrow
{
    private boolean onThis = false; 
    public static int hairstyle = 1;
    /**
     * Act - do whatever the LeftArrowH wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.

        if(Greenfoot.mouseMoved(null)){
            onThis = Greenfoot.mouseMoved(this);  
        }
        if(onThis)  
        {
            setImage("arrow_left_hover.png");  
            GreenfootImage image = getImage();  
            image.scale(100, 75);
            setImage(image);
        }
        else  {
            setImage("arrow_left_null.png");   
            GreenfootImage image = getImage();  
            image.scale(100, 75);
            setImage(image);
        }
        if (Greenfoot.mouseClicked(this)) {
            setImage("arrow_left_click.png");
            GreenfootImage image = getImage();  
            image.scale(100, 75);
            setImage(image);
            lefthair();
        }

    }    

    public LeftArrowH()
    {
        GreenfootImage image = getImage();  
        image.scale(100, 75);
        setImage(image);
    }

    public void lefthair()
    {
        if (hairstyle == 1)
        {hairstyle = 3;}
        if (hairstyle == 2)
        {hairstyle = 1;}
        if (hairstyle == 3)
        {hairstyle = 2;}
    }
}
and the code for the character who's supposed to be changing image as hairstyle value changes is this:
import greenfoot.*;

/**
 * Write a description of class CreateCharacter here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class CreateCharacter extends CreateClass
{
    public static int hairstyle = 1;
    public static int batch = 1;
    public static int skin = 1;

    
    /**
     * Act - do whatever the Character wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        // Add your action code here.
        
            if (hairstyle == 1 && batch == 1 && skin == 1)
            {
                setImage("h1b1s1.png");  

            }    
            if (hairstyle == 2 && batch == 1 && skin == 1)
            {
                setImage("h2b1s1.png");  

            }   
            if (hairstyle == 3 && batch == 1 && skin == 1)
            {
                setImage("h3b1s1.png");  

            }   
        }
    }
So my problem is that, I can't seem to get the character to change image when an arrow is pressed, so far I've only coded for the two hair arrows, the left and the right-- they have nearly identical codes, with the only difference in the method that they execute (lefthair and righthair) who's values are pretty much just opposite. I have put the images needed in the greenfoot folder also. I think my mistake is in how I declared the variables equating them to 1? To be honest, I have no idea what I'm doing and I'm new to this so, all help is appreciated. Thank you!
Super_Hippo Super_Hippo

2015/9/15

#
Since the variables in your CreateCharacter class are 'public static', you could just use this variable in the arrow classes instead of using their own. So replace all 'hairstyle' in the arrow class with CreateCharacter.hairstyle. A few things: -- you don't need an own arrow class for each arrow -- you don't need to hard-code every possibility in the act method of the CreateCharacter class
setImage("h" + hairstyle + "b" + batch + "s" + skin ".png");
-- you only need to set the image when it is changed
You need to login to post a reply.