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

2015/12/25

height of hero from enemy class

davemib123 davemib123

2015/12/25

#
Hi, I'm trying to get the height of the hero from the enemy class. I've tried this but it give me a java.lang.NullPointerException:
  private void checkCollision()
    {
        Mario player = (Mario) getOneIntersectingObject(Mario.class);
        int playerBottom = player.getImage().getHeight() / 2;
        System.out.println(playerBottom);
}
Where have I gone wrong with it?
danpost danpost

2015/12/25

#
The variable 'player' will be assigned one of two values -- if intersecting a Mario object, it will then reference that object; if not, then its value will be set to 'null' (which indicates no object is referenced by the variable). If its value does happen to be set to 'null', then you cannot call any methods on it (like trying to call 'getHeight on it as in line 4). In other words, the condition required before calling 'getHeight' on 'player' is that the value of 'player' cannot be 'null'. Insert the following line at line 4:
if (player == null) return;
This will force an immediate return from the 'checkCollision' method to the method that called it (probably your 'act' method) so the rest of the method will not be executed and the error will not occur.
davemib123 davemib123

2015/12/26

#
great, that works as I wanted :)
davemib123 davemib123

2015/12/27

#
Relating to the problem above. How do I get the collision to check that the player's feet have touched the enemy head? This is what I have so far:
private void checkCollision()
    {

        Mario player = (Mario) getOneIntersectingObject(Mario.class);
        if (player != null)
        {
            int playerBottom = player.getImage().getHeight() / 2;
            int creatureTop = getImage().getHeight();

            if (playerBottom  >= creatureTop)
            {
                // player wins
                getWorld().removeObject(this);
            } else {
                player.dead();
            }
        }

    }
If I run straight into the enemy its fine. But when I jump on the head it kills the hero instead of killing the enemy. Any ideas?
danpost danpost

2015/12/27

#
Your fields are not really holding the values you are representing by their names. That is, 'playerBottom' is set to half the height of the player and 'creatureTop' is set to the height of the creature. Comparing these, if the player and the creature are not too different height-wise, line 10 will always evaluate to 'false'. What you need to compare are the y-coordinate location in the world that the bottom of the player and the top of the creature are. These would then be:
int playerBottom = player.getX()+player.getImage().getHeight()/2;
int creatureTop = getX()-getImage().getHeight()/2;
davemib123 davemib123

2015/12/28

#
ok I got that working. Needed to read the message a few times to get my head around it :)
davemib123 davemib123

2016/1/1

#
Edit: I've used an offset to check bottom of a block object. how can I check then entire width depending on where my hero hits it? I have tried this and it seems to work.
 private void collisionCheck()
    {
        Actor MarioM = getOneObjectAtOffset( 0, 30, Mario.class);
        Actor MarioR = getOneObjectAtOffset( 18, 30, Mario.class);
        Actor MarioL = getOneObjectAtOffset( -18, 30, Mario.class);
        if (MarioM != null)  
        { 
            System.out.println("hit-Middle");
        }
        else if (MarioR != null)  
        { 
            System.out.println("hit-Right");
        }
        else if (MarioL != null)  
        { 
            System.out.println("hit-Left");
        }

    }
Is there a better solution?
danpost danpost

2016/1/1

#
danpost wrote...
What you need to compare are the y-coordinate location in the world that the bottom of the player and the top of the creature are. These would then be:
int playerBottom = player.getX()+player.getImage().getHeight()/2;
int creatureTop = getX()-getImage().getHeight()/2;
I noticed that what I coded did not reflect what I specified in text -- I used 'getX' in the quoted code where I needed to use 'getY', even though I did specify that the y-coordinate location in the world was to be used. At any rate, I do not see any problem with your last code issue (for bottom of object and top of player). Of course, saying "it doesn't work" does not say anything about how it executes different from what you wanted.
danpost danpost

2016/1/1

#
davemib123 wrote...
Edit: I've used an offset to check bottom of a block object. how can I check then entire width depending on where my hero hits it?
I just now (2 hours later) noticed the editing of your last post. What I usually do is just check for intersecting of the object from the player class after moving. I use the direction of movement to determine which side of the player makes contact with the intersecting object (btw, that is precisely what I did in my Jump and Run Demo w/Moving Platform scenario).
You need to login to post a reply.