3.7 The prepare() Method

The prepare() Method and addObject in Greenfoot

Unit 3: Methods, Worlds, and Constructors · Lesson 3.7 · about 30 minutes · no coding experience needed

Since lesson 1.2 you have been dragging actors in by hand and watching them vanish on Reset. This is the lesson that fixes it, and it turns your scenario into something another person can open and play.

What you will be able to do

  • Place actors in code with addObject
  • Write a prepare() method and call it from the constructor
  • Explain why hand-placed actors do not survive Reset but these do

Words you will need

prepare()
In plain words The method where you set up the opening scene.
More precisely A conventional name for the setup method a world constructor calls.
addObject
In plain words Put this actor into the world at these coordinates.
More precisely World.addObject(Actor, int x, int y)

Build it step by step

Every step below leaves a scenario that compiles and runs. If you stop halfway you will have something that works, not something broken. Follow along in Greenfoot rather than reading straight through.

  1. Step 1

    On your screen A prepare method containing three addObject calls.

    public void prepare()
    {
        addObject(new Crab(), 5, 5);
        addObject(new Coin(), 12, 3);
        addObject(new Coin(), 2, 9);
    }

    Three actors, placed in code. Read one line: make a new Crab (that is `new` from 1.3), and put it in this world at x 5, y 5.

    prepare is an ORDINARY method. There is nothing magic about the name; Greenfoot does not know it exists. It is a convention, and a good one, because it puts all the scene setup in one findable place.

  2. Step 2

    On your screen The world constructor calling prepare as its last statement.

    public OceanWorld()
    {
        super(20, 15, 30);
        prepare();
    }

    And this is the line that makes it all work. Because the constructor calls prepare, and the constructor runs on every Reset, the scene rebuilds itself every time.

    Leave this line out and prepare is a method nobody calls, exactly as in 3.2. The world comes up empty and nothing tells you why.

  3. Step 3

    On your screen The world after Reset, showing the same actors back in their starting positions.

    Press Reset now. The crab and both coins come back, in exactly the right places, every time.

    That is the difference between a scenario only you can use and one you can hand to somebody else. It is also the answer owed since 1.2: hand-placed actors were never part of the world's definition, and now yours are.

  4. Step 4

    On your screen An actor being removed from the world with removeObject.

    removeObject(someCoin);
    removeTouching(Coin.class);   // from inside an actor

    The opposite operation. `removeObject` takes an actor out of the world.

    From inside an ACTOR you usually want `removeTouching(Coin.class)`, which finds what you are overlapping and removes it. From inside the WORLD you use removeObject with a specific actor.

    A removed actor is gone. Calling getX() on it afterward is the NullPointerException from E-07.

Mistakes almost everyone makes here

These are the wrong ideas students actually build at this point. Read them even if you think you understand, because a wrong idea you have not noticed is the expensive kind.

Common wrong idea prepare() is a special Greenfoot method that runs automatically.

What is actually true It is an ordinary method. It runs because the constructor calls it.

Why it matters Believing it is automatic means you never notice the missing call, and the world comes up empty with no error.

Common wrong idea addObject creates the actor.

What is actually true `new Crab()` creates it; addObject puts it into the world.

Why it matters Two separate jobs, and separating them is what lets you make an actor, configure it, and then place it.

Common wrong idea Actors dragged in by hand are saved with the scenario.

What is actually true They are not. Only actors placed in code come back.

Why it matters This is the whole reason the lesson exists.

Common wrong idea A removed actor can still be used.

What is actually true It is out of the world. Asking it for its position throws.

Why it matters The most common NullPointerException in a Greenfoot game.

Check your understanding

Answer these before moving on. They are graded instantly and you can retry.

  1. 1. What makes prepare() run?

  2. 2. What does `addObject(new Coin(), 3, 7)` do?

  3. 3. You wrote prepare() but the world comes up empty. What is most likely?

  4. 4. Why do actors placed in prepare survive a Reset?

  5. 5. From inside an ACTOR, which removes the coin it is standing on?

Fill in the code

Set up an opening scene with one player at the center-ish cell (10, 7) and one coin at (3, 3), and make sure it runs when the world is built.

public OceanWorld()
{
    super(20, 15, 30);
    ;
}

public void prepare()
{
    (new Player(), 10, 7);
    addObject( Coin(), 3, 3);
}
Stuck? Open a hint for each blank
  • Blank 1: Call the setup method below. Parentheses required.
  • Blank 2: The world method that puts an actor into the world.
  • Blank 3: The keyword that builds one object from a class.

Lesson quiz

This one counts toward your progress. Take it when the section above makes sense.

  1. 1. prepare() is:

  2. 2. Which line creates AND places an enemy at the top left?

  3. 3. A student drags five actors in, presses Reset, and they vanish. The fix is to:

  4. 4. What happens if you call getX() on an actor you already removed?

The short version

  • addObject(new Thing(), x, y) creates and places an actor.
  • prepare() is an ordinary method; the CONSTRUCTOR calling it is what makes it run.
  • Because the constructor runs on every Reset, the scene rebuilds itself.
  • A removed actor is out of the world, and asking it anything throws.

If you get stuck

These pages cover the problems that come up most often in this lesson. Opening one is not cheating and it is not counted against you.

Get in Touch

Whether you're a student, parent, or teacher — I'd love to hear from you.

Just want free AP CS resources?

Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]