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

2018/3/16

Trying to remove two actors at the same time

Aqurila Aqurila

2018/3/16

#
Hello, When I click on an actor which writes start, the actor is getting removed. But when I try to make it that another actor, which is the title, has to disappear the same time as the start button, only the start button disappears... Here's the code that I tried for the title:
public void act() 
    {
        if (Greenfoot.mouseClicked(Run.class)){
            getWorld().removeObject(this);
        }
    }    
and here the code from the Start button:
public void act() 
    {
        if (Greenfoot.mouseClicked(this)){
            getWorld().removeObject(this);           
        }
    }    
Thanks for the help!
Super_Hippo Super_Hippo

2018/3/16

#
The method requires an object, not a class. You can remove the title object (or in my example all title objects) from the Start class.
public void act() 
{
    if (Greenfoot.mouseClicked(this))
    {
        if (!getWorld().getObjects(Title.class).isEmpty())
        {
            getWorld().removeObjects(getWorld().getObjects(Title.class));
        }
        getWorld().removeObject(this);           
    }
}
(Not sure if you have to check that there is one in the world, probably not.)
Aqurila Aqurila

2018/3/16

#
Hello, thanks for your answer. It works perfectly :) Can you just explain what the isEmpty() is doing?
Aqurila Aqurila

2018/3/16

#
Also I have another place where I should be able to use the exact same code: it is when the player intersects with a ball (one of several that spawn after some time). So is this right if I use this code? I tried it out but it doesn't seem to be working...
public void act() 
    {    
        jump();
        //disappear();

        if(Greenfoot.isKeyDown("right")){

            setImage(gif1.getCurrentImage());

        }

        if(Greenfoot.isKeyDown("left")){

            setImage(gif2.getCurrentImage());

        }       
        if (this.getOneIntersectingObject(Ball.class) != null)
        {
            if (!getWorld().getObjects(Ball.class).isEmpty())
            {
                getWorld().removeObjects(getWorld().getObjects(Ball.class));
            }
            getWorld().removeObject(this);           
        }
    }
danpost danpost

2018/3/16

#
Aqurila wrote...
Can you just explain what the isEmpty() is doing?
Your line 19 could be written as (but no need to change it):
if (getWorld().getObjects(Ball.class).size() != 0)
The lines do precisely the same thing. "list is empty" is the same as "list contains no elements" or "list size is zero". With the boolean negator ,'!', in front, it transforms it to "list is not empty" or "list contains elements" or "list size is greater than zero".
Super_Hippo Super_Hippo

2018/3/16

#
What is happening? If all are removed and you only want to remove the one touching, then use this:
if (isTouching(Ball.class))
{
    removeTouching(Ball.class);
    getWorld().removeObject(this);
}
Yehuda Yehuda

2018/3/19

#
If trying to retrieve a Ball object (line 17) returns non-null then you don't need to check if there are any in the world (line 19), and if you are not actually doing anything with the retrieved ball then you can just use a boolean method (the one Hippo used).
You need to login to post a reply.