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

2013/6/27

kill an enemy when jumped on

davemib123 davemib123

2013/6/27

#
Hi guys, trying to kill an enemy when the hero jumps on it. I have this so far, but its not working correct:
  public void collisionDetection(){
        Actor Mario = getOneIntersectingObject(Mario.class);
    if ((Mario != null) && (Mario.getY() >= this.getY()-10) )
        {  
           getWorld().removeObject(this);  
        } 

}
Can someone help me out?
davemib123 davemib123

2013/6/27

#
Thanks for the suggestion, but it doesnt work. the single & generates an error.
Gevater_Tod4711 Gevater_Tod4711

2013/6/27

#
I think using one & would not be the best idea (on the contrary that would cause exceptions). I think it would be better to let Mario check whether he is jumping onto an enemy. Try to do it like this:
List<Enemy> enemys = getWorld().getObjectsAt(getX(), getY() + getImage().getHeight()/2 + 5, Enemy.class);
    if (enemys != null && !enemys.isEmpty()) {
        getWorld().removeObject(enemys.get(0));
    }
}
davemib123 davemib123

2013/6/27

#
Thanks, I've managed to get it working with this:
 if (getOneObjectAtOffset(-19, -45, Mario.class) != null || getOneObjectAtOffset(19, -45, Mario.class) != null) 
        {  
            getWorld().removeObject(this);  
        } 
        else if(getOneObjectAtOffset(-19, 10, Mario.class) != null || getOneObjectAtOffset(19, 10, Mario.class) != null )
        {  
             Mario.die();
        } 
You need to login to post a reply.