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

2020/11/14

Removing an object once it has met the criteria

1
2
potsmoka potsmoka

2020/11/14

#
So I need to make an object X disappear once the actor has picked up 3 different objects. Could anyone help me?
Super_Hippo Super_Hippo

2020/11/14

#
You can remove the object from the world by calling the removeObject-method from its world.
potsmoka potsmoka

2020/11/14

#
Yes, but how do I specify that it can only be removed once those 3 items have been picked up?
danpost danpost

2020/11/14

#
Use an int counter to track the number of objects picked up (or a java.util.ArrayList objectsCollected to retain what objects have been collected); then check for removal only upon picking up an object by checking if int value or size of List is 3.
potsmoka potsmoka

2020/11/14

#
danpost wrote...
Use an int counter to track the number of objects picked up (or a java.util.ArrayList objectsCollected to retain what objects have been collected); then check for removal only upon picking up an object by checking if int value or size of List is 3.
Thank you for answering. Is there a tutorial on this? I am a complete beginner and it's just a school project.
danpost danpost

2020/11/14

#
potsmoka wrote...
Thank you for answering. Is there a tutorial on this? I am a complete beginner and it's just a school project.
I am not sure what a tutorial on this would even have to say. To keep track of something, you need a field; and the best time to check a condition is when that condition has the potential to have changed states. So, you would check for removal only when the count changes. What more is there to say on the matter?
potsmoka potsmoka

2020/11/14

#
danpost wrote...
potsmoka wrote...
Thank you for answering. Is there a tutorial on this? I am a complete beginner and it's just a school project.
I am not sure what a tutorial on this would even have to say. To keep track of something, you need a field; and the best time to check a condition is when that condition has the potential to have changed states. So, you would check for removal only when the count changes. What more is there to say on the matter?
Could you show me the code because I just can't find the solution, don't even now where to start.
danpost danpost

2020/11/14

#
In class of object X:
private int countCollected;

public void act()
{
    // other action codes
    if (isTouching(Collectible.class))
    {
        removeTouching(Collectible.class);
        if (++countCollected == 3) getWorld().removeObject(this);
    }
}
potsmoka potsmoka

2020/11/14

#
}
danpost wrote...
In class of object X:
private int countCollected;

public void act()
{
    // other action codes
    if (isTouching(Collectible.class))
    {
        removeTouching(Collectible.class);
        if (++countCollected == 3) getWorld().removeObject(this);
    }
}
Thanks!! But I get this compile error: public void takeToolbox(){ Actor toolbox; toolbox = getOneObjectAtOffset(0, 0, Toolbox.class); if(toolbox !=null) { World myWorld = getWorld(); counter.add(1); (here it says: cannot find symbol - variable counter myWorld.removeObject(toolbox); } } in my world class the counter is: public Counter getCounter() { return counter; }
potsmoka potsmoka

2020/11/14

#
and this is the counter class: public class Counter extends Actor { private int score = 0; public void act() { setImage(new GreenfootImage("Score" + " " + "0", 20, Color.GREEN, Color.BLACK)); } public void addScore() { score++; } }
danpost danpost

2020/11/14

#
Change
counter.add(1);
to
getWorld().getCounter().addScore();
potsmoka potsmoka

2020/11/14

#
public void takeBout(){
        Actor bout;
        bout = getOneObjectAtOffset(0, 0, Bout.class);
        if(bout !=null)
        {
            World world;
            world = getWorld();

            getWorld().getCounter().addScore(1); here it says cannot find symbol - method getCounter()
            world.removeObject(bout);

        }
    }
danpost danpost

2020/11/14

#
Add to Counter class:
public int getScore()
{
    return score;
}
danpost danpost

2020/11/14

#
potsmoka wrote...
here it says cannot find symbol - method getCounter()
Remove the '1' from the line.
danpost danpost

2020/11/14

#
Change 'getWorld()' to '((MyWorld)getWorld())'.
There are more replies on the next page.
1
2