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

2015/11/1

Help!

LJE516 LJE516

2015/11/1

#
Actually I am a new learner of greenfoot and I have a trouble for my assignment I have to make a Jungle board game Here is the given codes
public class Chess extends Actor
{
    private int color;
    private int rank;
    private int real_rank;

    public static final int COLOR_RED = 0;
    public static final int COLOR_GREEN = 1;
    
    public static final int RANK_RAT = 1;
    public static final int RANK_CAT = 2;
    public static final int RANK_DOG = 3;
    public static final int RANK_WOLF = 4;
    public static final int RANK_LEOPARD = 5;
    public static final int RANK_TIGER = 6;
    public static final int RANK_LION = 7;
    public static final int REAL_RANK_LION = 6; //Lion and Tiger are equally strong.
    public static final int RANK_ELEPHANT = 8;

    public Chess(int c, int r)
    {
        color = c ;
        rank = r;
        updateImage();
    }
/**
     * It simply loads the image based on the given rank and color, No need to modify.
     */    
    private void updateImage()
    {
        String prefix = "";
        String postfix = "" ;        
        if ( color == COLOR_RED )
        {
            prefix = "red_";    
        }
        else
        {
            prefix = "green_";
        }
        switch (rank)
        {            
            case RANK_RAT: postfix = "rat"; break;
            case RANK_CAT: postfix = "cat"; break; 
            case RANK_DOG: postfix = "dog"; break;
            case RANK_WOLF: postfix = "wolf"; break;
            case RANK_LEOPARD: postfix = "leopard"; break;
            case RANK_TIGER: postfix = "tiger"; break;
            case RANK_LION: postfix = "lion"; break;
            default: 
            case RANK_ELEPHANT: postfix = "elephant"; break;

        }        
        setImage( prefix + postfix + ".png" );        
    }
I have to create some chess and place them on the right place of the board for initialization. So I tried to create the subclass, and create an animal lets say is a Rat and I type the following codes in the GWorld
    private void initializeChess()
    {
        Rat greenrat = new greenRat;
        GWorld.addOneObject(greenrat,0,0);
}
however, I cannot compile my program since this problem How should I fix it? Many thanks! Frankly speaking, I am a very new learner of greenfoot and I am not familar with the environment yet ........
danpost danpost

2015/11/1

#
Do not assume that the issue is a greenfoot issue. This appears to be a issue with lack of knowledge of constructors in subclasses. First thing is that you are missing a set of round brackets after the class name in creating a 'greenRat' object. Change line 3 of your 'initializeChess' method to:
Rat greenrat = new greenRat();
Class names, by convention, should start with uppercase letters -- so, 'GreenRat' would be conventional; not 'greenRat'. Now, you did not supply any code from the greenRat class -- but you need a constructor in it that will inform the Chess class what rank and color it is; or rather, you need to inform the Rat class of the color and, in turn, the Rat class can inform the Chess class of both rank and color. As an example of passing the values, the Rat class constructor could be:
public Rat(int c)
{
    super(Chess.RANK_RAT, c);
}
This shows the color being accepted and both the color and rank being passed to the Chess class (its superclass).
You need to login to post a reply.