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

2013/1/2

Remove a specified object of a list

Rodeurhalt Rodeurhalt

2013/1/2

#
Hi! I have two button and when my mouse is on one of them a text will appear under this button and disappear if I'm not on this button. Here is the code :
    public void info()
    {
        greenfoot.MouseInfo info = Greenfoot.getMouseInfo();  

        if (info != null) 
        { 
            mouseX = info.getX();
            mouseY = info.getY();
        } 

        if(getX()+ largeur >= mouseX && mouseX > getX()-largeur && getY()+hauteur > mouseY && mouseY>getY()-hauteur) // variable largeur and hauteur are the height and the width of the image divided by 2
        {
            Info information = new Info();
            String l =System.getProperty("line.separator");
            information.setImage( new GreenfootImage( "...", 18, Color.BLACK, Color.WHITE));
            getWorld().addObject(information,1500,320);
        }
        else
        {
            java.util.List infos = getWorld().getObjects(Info.class);
            getWorld().removeObjects(infos);
        }
    }
The problem is that the other button does not work because I put in the else : remove all objects of the class Info ,which is the class of the text, so the text will appear but directly be removed by this else. So I tried to remove only the object that is create when I put my mouse on this button ( the object information in the if). here is what I change in the else :
        else
        {
            java.util.List infos = getWorld().getObjects(Info.class);
            if(infos.contains((Object) information))
            {
                getWorld().removeObject(information);
            }
        }
But an error occur : cannot find symbol - variable information So here is my question : How do I get my object information if it is in the world and then remove it without removing other object of this class ?
Gevater_Tod4711 Gevater_Tod4711

2013/1/2

#
the object information can't be find because it isn't existing in the else part. You use the variable information only in the if part and then if the else part is executed this variable is no longer known because it's private and is removed after the method ends. You should try to make the information object global.
Rodeurhalt Rodeurhalt

2013/1/2

#
It works, but I can't remove it. In fact as long as my mouse is on the button it will create other Info object (because this method info() is in the act() method.)
Gevater_Tod4711 Gevater_Tod4711

2013/1/3

#
The easyest way would be to let the info object remove themselves or give the info objects an index which makes clear to which button they belong to. If you know the index you can remove only the objects that you want to remove.
danpost danpost

2013/1/3

#
If only one Info object is to be on the screen at a time, then you only need to add another condition to the 'if' statement at line 11: getWorld().getObjects(Info.class).isEmpty()
Rodeurhalt Rodeurhalt

2013/1/3

#
Ok thanks! But I found another solution, I used the X and Y of my mouse to determinate if yes or no it must remove all the objects of my class Info. :) It works well :P
danpost danpost

2013/1/3

#
Great! Glad to hear it.
You need to login to post a reply.