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

2015/8/27

addObject

1
2
Vellyxenya Vellyxenya

2015/8/28

#
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:
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);
        }
    }
And the "mouseOn() method is in a super-class :
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;
    }
Can you help me pls? So that i can see the lifebar of each monster my mouse is hovering, not only one^^
danpost danpost

2015/8/28

#
Vellyxenya wrote...
Can you help me pls?
It might help to see the Lifebar class code.
Vellyxenya Vellyxenya

2015/8/28

#
Here is it: it's only an image^^
public void act() 
    {
        // Add your action code here.
    }    
Vellyxenya Vellyxenya

2015/8/28

#
i just want to see the lifebar on the screen when my mouse is hovering any of the monsters, not only one^^
danpost danpost

2015/8/28

#
Vellyxenya wrote...
i just want to see the lifebar on the screen when my mouse is hovering any of the monsters, not only one^^
Then, what is the code for your 'act' method in the Monsters class? and what fields might you be using in that class (both static and non-static)?
Vellyxenya Vellyxenya

2015/8/28

#
This is my act method in my Monsters class:
public void act() 
    {
        move(-(1+Greenfoot.getRandomNumber(1)));
        goUpOrDown();
        showLife();
        hideLife();
        vanish();
    }
and all my methods are public, should I name them static?
Super_Hippo Super_Hippo

2015/8/28

#
The problem is that every 'Monsters' object is removing ALL 'lifebars' if the mouse is not above it. So let's say you add monster A, B and C to the world. If you have your mouse over B, then B will create the lifebar, but C will remove it. To avoid that, you could add a reference to the lifebar object which is created by a monster and remove its lifebar (instead of all) if the mouse isn't above the object. (Change 'Monsters' to 'Monster' and 'lifebars' to 'Lifebar'. It should be singular and start with an uppercase letter.) Since the 'showLife' and 'hideLife' methods are in the act-method of the Monster class, you should not use this for-each loop. You can also put both methods together:
private Lifebar myLifebar = null;

private void adjustLifebar()
{
    if (mouseOn())
    {
        if (myLifebar == null) getWorld().addObject(new Lifebar(), getWorld().getWidth()/2, 50);
    }
    else if (myLifebar != null)
    {
        getWorld().removeObject(myLifebar);
        myLifebar = null;
    }
}
By the way, I wonder how the life bar is showing the health if you don't pass it to the life bar.
Vellyxenya Vellyxenya

2015/8/28

#
Hi, I replaced the two methods showLife and hideLife by your method adjustLifebar but the problem now is that when i remove my mouse from the monster, the lifebar doesn't disappear... I've no idea how to fix that :( Until now, i'm not even trying to show the health in the lifebar, the lifebar itself isn't working correctly^^''
danpost danpost

2015/8/28

#
Vellyxenya wrote...
and all my methods are public, should I name them static?
Absolutely not -- at least not as is. You have several issues with the codes given (some of which could not be verified until the code for the act method was given). Instead of dealing with all the issues that currently exist, I think it best to try to explain some basics. For one thing, each Monsters object in the world will execute the act method given each act cycle -- meaning that only the last Monsters object acted on will determine whether a Lifebar object remains in the world or not (with your code as given). Therefore, it is better to place the main code for controlling the Lifebar object(s) in the world class where you can check all monsters at one time instead of each one individually. Another thing is that no checking needs to be done unless the mouse actually move on that act cycle (if no change in mouse position, there would be no change in the existence of the Lifebar obect). Next, there would also be no change if the mouse moved but also remained over the same Monsters object. So, knowing which Monsters object the mouse was last on would be helpful on subsequent act cycles. This means having a field to hold the Monsters object the mouse was last on. The world can also maintain a field for a single Lifebar object whose image can be adjusted as needed. The state of the Lifebar object being in the world or not can be used, in part, to determine if it may need removed or added.
Vellyxenya Vellyxenya

2015/8/28

#
Here was my act method so far:
public void act() 
    {
        move(-(1+Greenfoot.getRandomNumber(1)));
        goUpOrDown();
        /*showLife();
        hideLife();*/
        adjustLifebar();
        vanish();
    }
Thank you very much for your explanation, i see much more clearly now^^ I'm trying to put the code controlling the lifebar in the world but i'm facing a problem: how can i call the mouseOn() (it's in the actors class) from the world? Should I remove it from the Actor class and write it on the world class?
danpost danpost

2015/8/28

#
Vellyxenya wrote...
how can i call the mouseOn() (it's in the actors class) from the world? Should I remove it from the Actor class and write it on the world class?
for (Object obj : getObjects(Monsters.class))
{
    Monsters monster = (Monsters)obj;
    if (monster.mouseOn())
    { // etc.
fejfo fejfo

2015/8/29

#
an other option might be to draw the lifebar on the image
public void drawHealthBar() {
        GreenfootImage i = new GreenfootImage(getImage());

        GreenfootImage healthBar = new GreenfootImage(i.getWidth(),5);
        healthBar.setColor(Color.WHITE);
        healthBar.fill();

        int healthPixels = (int) (<health>/<maxHealth>*healthBar.getWidth());
        if(healthPixels <= 0) { healthPixels=1; }
        GreenfootImage filledHealth = new GreenfootImage(healthPixels,5);
        filledHealth.setColor(Color.RED);
        filledHealth.fill();

        healthBar.drawImage(filledHealth,0,0);        
        i.drawImage(healthBar,0,i.getHeight()-healthBar.getHeight());
        setImage(i);
    }
you should
import java.awt.Color
and replace the <health> and <maxHealth>
Vellyxenya Vellyxenya

2015/9/1

#
Thank you very much :D
You need to login to post a reply.
1
2