Hi again and excuse me for the multiple posts^^''
I've been trying to create a method that show the lifebar of the current "monster" when my mouse is hovering it. It does work but the problem is that it works only on one of the three monsters in the world (it works only on the last monster that has been created)...
Here's the code of this method in my Actor class:
And the "mouseOn() method is in a super-class :
Can you help me pls? So that i can see the lifebar of each monster my mouse is hovering, not only one^^
public void showLife() { List<Monsters> monsters = getWorld().getObjects(Monsters.class); for (Monsters monster : monsters) { if(mouseOn() == true) { getWorld().addObject(new Lifebar(), getWorld().getWidth()/2, 50); } } } public void hideLife() { if(mouseOn() == false) { List<Lifebar> lifebars = getWorld().getObjects(Lifebar.class); getWorld().removeObjects(lifebars); } }
public boolean mouseOn() { MouseInfo mouse = Greenfoot.getMouseInfo(); if(mouse != null) { if( (mouse.getX() < (getX()+ this.getImage().getWidth()/2)) && (mouse.getX() > (getX()- this.getImage().getWidth()/2)) && (mouse.getY() < (getY()+ this.getImage().getHeight()/2)) && (mouse.getY() > (getY()- this.getImage().getHeight()/2))) { return true; } } return false; }