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

2013/12/4

Creating an instance variable of an Object with parameter.

JasonZhu JasonZhu

2013/12/4

#
I have a Player class that has 2 constructors. One that has no parameters and one that has one with an int. I need to create an instance variable of that Player with an int constructor in a separate class. How do I do that?
danpost danpost

2013/12/4

#
A bit more background as to what you are trying to do would help. All I can do, for now, is this:
// instance field declared
private Player player;
// creating a player using constructor with 'int' parameter and assigning to instance field
player = new Player(/* int value */);
Line 4 would be somewhere in a constructor or method.
JasonZhu JasonZhu

2013/12/4

#
I'm trying to get a object of another class to follow the player with the int parameter. To do this, I would need to pass an instance variable of the player with the int parameter. This player would be already existant in the world, so it wouldn't be new player right?
danpost danpost

2013/12/4

#
I think you need to post the class with the two constructors. This is just to make sure what you are trying to do is even possible yet (the way it is coded).
JasonZhu JasonZhu

2013/12/4

#
    public Player(int type)
    {
        //initialize imageList
        if(type==1){
            rightImageList.add(new GreenfootImage("stand1a.png"));
            rightImageList.add(new GreenfootImage("stand1b.png"));
            rightImageList.add(new GreenfootImage("walk1a.png"));
            rightImageList.add(new GreenfootImage("walk1b.png"));
            rightImageList.add(new GreenfootImage("walk1c.png"));

            //GreenfootImage has no clone method
            leftImageList.add(new GreenfootImage("stand1a.png"));
            leftImageList.add(new GreenfootImage("stand1b.png"));
            leftImageList.add(new GreenfootImage("walk1a.png"));
            leftImageList.add(new GreenfootImage("walk1b.png"));
            leftImageList.add(new GreenfootImage("walk1c.png"));
            for(int a =0;a<leftImageList.size();a++){
                leftImageList.get(a).mirrorHorizontally();
            }
        }
        setImage(rightImageList.get(0));
        this.player=player;
    }

    public Player()
    {

    }
This is it here.
danpost danpost

2013/12/4

#
Does this even compile? It looks like line 22 would be a problem, as 'player' is not introduced into the constructor anywhere. I guess it would still compile, but 'player' is not changed. Once a player is constructed, its type is no longer accessible (the way it is coded).
JasonZhu JasonZhu

2013/12/4

#
It does compile although I find that I have lots of unnecessary code to only accomplish one task; that task being to flip the image of the Player only once when I press the left/right button.
danpost danpost

2013/12/4

#
I just realized that it would still compile; but the value of player remains unchanged because 'player' and 'this.player' would refer to the same field.
JasonZhu JasonZhu

2013/12/4

#
I did this in my Gun class:
    private Player player;


    public Gun(Player player, int type)
    {
        this.player = player;

    }
And I could use the player okay. However, when I do the same in my Enemy class, it gives me a null pointer exception. The only difference is my Enemy constructor is parameter-less.
danpost danpost

2013/12/4

#
Yes. With the Gun class, your constructor has an argument call 'player' which becomes local to the method (it is not the same as 'this.player' which is your instance field declared outside the method). So the Player object brought into the constructor then becomes the value of the instance field when line 6 is executed. The following two methods behave in the same manner (the first is similar to the constructor above):
public void setPlayer(Player player)
{
    this.player = player; // instance field set to value of local field
}

public void initPlayer()
{
    Player player = new Player(); // local field
    this.player = player; // instance field set to value of local field
}
JasonZhu JasonZhu

2013/12/5

#
Thanks dan, makes sense.
You need to login to post a reply.