1.6 The act() Method

The act() Method: Greenfoot's Game Loop

Unit 1: Meet Greenfoot · Lesson 1.6 · about 35 minutes · no coding experience needed

Everything so far you did by hand: right-click, choose a method, watch it happen. Games do not have somebody right-clicking sixty times a second. act() is where you write down what should happen automatically, over and over, forever.

What you will be able to do

  • Explain when Greenfoot calls act() and how often
  • Write behavior inside act() by filling in the gaps
  • Explain why a forever loop inside act() freezes the scenario

Words you will need

act()
In plain words The method Greenfoot calls for you, once every frame.
More precisely A method every Actor and World may override. Greenfoot invokes it once per actor per frame while the scenario is running.
Game loop
In plain words The endless repeat that makes a game move.
More precisely The cycle of updating every object once per frame. In Greenfoot this loop is provided for you; act() is your hook into it.

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 The Crab class code open, showing an empty act method with a comment inside it.

    public class Crab extends Actor
    {
        public void act()
        {
            // your code goes here
        }
    }

    Every actor class starts with this. It is empty, which is why a new actor sits there doing nothing. Greenfoot is already calling this method for you, sixty-odd times a second while the scenario runs. It just has nothing to do.

    Read the signature with what you learned in 1.5: `void` so nothing comes back, `act` is the name, `()` so it needs nothing from you. YOU never call this method. Greenfoot calls it.

  2. Step 2

    On your screen The crab moving steadily across the world after a move call was added to act.

    public void act()
    {
        move(2);
    }

    One line, and the crab walks by itself. Compile, then press Run.

    Look at what that line actually says: "move 2". Not "move 2 over and over". The repetition is not in your code at all. It comes from Greenfoot calling act() again next frame, and the frame after that.

  3. Step 3

    On your screen The crab part way across the world, paused mid-journey after a single press of the Act button.

    Press Reset, then press ACT once instead of Run. The crab moves 2 cells and stops. Press it again: 2 more.

    This is the clearest demonstration of what a frame is, and it is worth doing rather than reading. Run is just this button held down.

  4. Step 4

    On your screen The crab turning back on itself at the edge of the world.

    public void act()
    {
        move(2);
        if (isAtEdge())
        {
            turn(180);
        }
    }

    Now it patrols. Each frame: move 2, then ask "am I at the edge?", and if so turn around.

    You have not formally met `if` yet, that is Unit 2. Read it as plain English for now: IF the thing in the parentheses is true, do what is in the braces. `isAtEdge()` is a method that hands back true or false, which is exactly the shape of thing an if needs.

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 act() runs once when the scenario starts.

What is actually true act() runs once per frame, over and over, for as long as the scenario runs.

Why it matters Believing it runs once makes all animation seem impossible.

Common wrong idea I need a loop inside act() to keep the crab moving.

What is actually true act() IS the loop. One move(2) per frame is continuous movement.

Why it matters This is the big one. A `while (true)` inside act() never returns, so Greenfoot never gets to draw the next frame and the whole window freezes. It looks like a crash and it is the most common way a beginner loses their work.

Common wrong idea I should call act() myself to make things happen.

What is actually true Greenfoot calls it. You only write what goes inside it.

Why it matters Calling act() by hand produces confusing double-speed behavior and hides real bugs.

Common wrong idea A bigger number in move() makes the animation smoother.

What is actually true A bigger number makes bigger jumps per frame, which is LESS smooth.

Why it matters Smoothness comes from small steps happening often.

Check your understanding

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

  1. 1. How many times does Greenfoot call act() on an actor while the scenario is running?

  2. 2. An act() method contains only `move(3);`. You press Act four times. How far has the actor traveled in total?

  3. 3. Why does putting `while (true) { move(1); }` inside act() freeze the scenario?

  4. 4. Who calls act()?

  5. 5. What does `isAtEdge()` hand back?

Fill in the code

Complete this act() method so the actor walks forward 3 cells every frame and turns right around whenever it reaches the edge of the world.

public void ()
{
    (3);
    if ()
    {
        turn();
    }
}
Stuck? Open a hint for each blank
  • Blank 1: The method Greenfoot calls for you once per frame.
  • Blank 2: Which method travels forward in the direction the actor faces?
  • Blank 3: A method that reports true when the actor has reached the edge. Do not forget the parentheses.
  • Blank 4: Turning right around is half a full circle, in degrees.

Lesson quiz

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

  1. 1. act() is best described as:

  2. 2. Which act() body makes an actor move continuously and smoothly?

  3. 3. A scenario locks up and stops responding as soon as you press Run. What is the most likely cause?

  4. 4. You press Act exactly once on an actor whose act() body is `move(5); turn(90);`. What happened?

  5. 5. Why should you NOT call act() yourself from your own code?

The short version

  • Greenfoot calls act() once per frame, for every actor, forever.
  • You write the body. You never call act() yourself.
  • act() IS the repetition. One small move per frame is continuous motion.
  • A forever loop inside act() freezes the whole scenario, because act() must return.
  • Act runs one frame. Run repeats it.

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]