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

2017/4/3

Object reference

1
2
LeonV LeonV

2017/4/3

#
Hello people, I'm very new to programming, we've been getting lesson on programming in GreenFoot for about a month now at school. I have this test coming up tomorrow and suddenly my test game that I was making doesn't want to work. I was trying to make a game in which you move your player and enemies move towards you (I know it's very basic). I tried putting in an object reference, but it keeps giving me some error. Here is the code I'm using in the enemy:
public void act() 
    {
        int x = player.getX();
        if (x < getX())
        {
            setLocation(this.getX()-2, this.getY());
        }
        if (x > getX())
        {
                setLocation(this.getX()+2, this.getY());
        }
    }  
    
    public void setPlayer (Player p)
    {
        player = p;
    }
And this I used in the player:
BasicEnemy enemy = new BasicEnemy();
        enemy.setPlayer(this);
I hope someone will be able to help me.
danpost danpost

2017/4/3

#
Nothing stands out as being wrong with the code given except for the fact that you are spawning the enemies from the player class. That should probably be done in your world class.
LeonV LeonV

2017/4/3

#
Uhm okay, but how do I change that. In the world I've said to place an enemy at the start of the game. Has it got something to do with the "new" before BasicEnemy()? If so, how do I change it. Oh and maybe I should mention what kind of error it gives. It's a java.lang.NullPointerException and it's in the enemy code line 3, so where it gets the player's location. I just assumed it's because the reference is wrong, because when I use inspect on the enemy it says "null" instead of the usual error, but I could also be someting else. Thanks for you quick response btw :D
LeonV LeonV

2017/4/3

#
public MyWorld()
    {    
        super(600, 400, 1, false); 
        addObject (new Player(), 300, 200);
        addObject (new BasicEnemy(), 550, 50);
    }
I think it's just this.
danpost danpost

2017/4/3

#
Try this:
public MyWorld()
{
    super(600, 400, 1, false);
    Player player = new Player();
    addObject(player, 300, 200);
    BasicEnemy enemy = new BasicEnemy();
    enemy.setPlayer(player);
    addObject(enemy, 550, 50);
}
Super_Hippo Super_Hippo

2017/4/3

#
Well, your last lines of code (the MyWorld constructor) can not produce a NullPointer exeption and danpost's code (which is basically the same, it just uses the setPlayer method on the enemy) can not produce this error, because 'new BasicEnemy()' does not pass any parameter/argument while your error is saying that it would. Edit: Well okay, seems like you removed your post.
LeonV LeonV

2017/4/3

#
Hahaha yeah I made a mistake when typing it :) Now it works so thank you guys very much, but I'm not really sure what really changed. Tomorrow on my test I'll need to explain what I'm doing and know I don't have a clue what we just changed. So could you maybe explain it a little? I see you made a lot of object refferences but why?
Super_Hippo Super_Hippo

2017/4/3

#
The only important one is the enemy variable which makes it possible to call the setPlayer method on it. The player variable is not needed.
danpost danpost

2017/4/3

#
Super_Hippo wrote...
The only important one is the enemy variable which makes it possible to call the setPlayer method on it. The player variable is not needed.
Actually, the player variable was needed also; otherwise we cannot with the same object both add it into the world and set the player of the enemy.
LeonV LeonV

2017/4/3

#
Oh okay, I think I get it now. And now for a follow-up question, the next thing I want to do is to make different enemy classes which will all do specific things, but always towards the player. Is it possible to put the refference in the subclass "Enemies", which all enemies belong too?
danpost danpost

2017/4/3

#
LeonV wrote...
Oh okay, I think I get it now. And now for a follow-up question, the next thing I want to do is to make different enemy classes which will all do specific things, but always towards the player. Is it possible to put the refference in the subclass "Enemies", which all enemies belong too?
Yes. All subclasses of Enemies will acquire all the fields and have use of all the non-private methods in the Enemies class.
Super_Hippo Super_Hippo

2017/4/3

#
danpost wrote...
Super_Hippo wrote...
The only important one is the enemy variable which makes it possible to call the setPlayer method on it. The player variable is not needed.
Actually, the player variable was needed also; otherwise we cannot with the same object both add it into the world and set the player of the enemy.
Right, I missed that. @LeonV: You could make a static variable in the Enemy class if the player is always the target anyways.
//in Enemy class
private static Actor player;

public static void setPlayer(Actor a)
{
    player = a;
}
Then, in your world constructor after adding the player to the world:
Enemy.setPlayer(player);
LeonV LeonV

2017/4/3

#
I've tried doing what you said @Super_Hippo, but I'm getting the same error about the referenc again. Here's my current code for Enemies:
private static Actor player;

public void act() 
    {

    }  

    public static void setPlayer (Actor a)
    {
        player = a;
    }
And what I now have in BasicEnemy, a subclass of Enemies:
Player player;

public void act() 
    {
        moveTowardsPlayer();
    }  

    private void moveTowardsPlayer()
    {
        //Move towards the player
        int x = player.getX();
        int y = player.getY();

        if (x < getX() && y < getY())
        {
            setLocation(this.getX()-1, this.getY()-1);
        }
        if (x < getX() && y > getY())
        {
            setLocation(this.getX()-1, this.getY()+1);
        }
        if (x > getX() && y < getY())
        {
            setLocation(this.getX()+1, this.getY()-1);
        }
        if (x > getX() && y > getY())
        {
            setLocation(this.getX()+1, this.getY()+1);
        }
        if (x == getX() && y < getY())
        {
            setLocation(this.getX(), this.getY()-1);
        }
        if (x == getX() && y > getY())
        {
            setLocation(this.getX(), this.getY()+1);
        }
        if (x < getX() && y == getY())
        {
            setLocation(this.getX()-1, this.getY());
        }
        if (x > getX() && y == getY())
        {
            setLocation(this.getX()+1, this.getY());
        }
    }
The error is in lines 5 and 11 of the BasicEnemy. And also my world code for if there is something wrong there:
public MyWorld()
    {    
        super(600, 400, 1, false);
        
        Player player = new Player();
        addObject(player, 300, 200);
        
        Enemies.setPlayer(player);
        addObject(new BasicEnemy(), 550, 50);
}
Super_Hippo Super_Hippo

2017/4/3

#
Remove line 1 in BasicEnemy.
LeonV LeonV

2017/4/3

#
Then it says, when trying to get the player's location: player has private access in Enemies. Maybe "private static Actor player" should be public then? Edit: Oh yeah that worked :D
There are more replies on the next page.
1
2