top of page

6. The Counter

1. Import the Counter

The counter class can be imported into your Greenfoot world.

​

Select Edit in the main Greenfoot window then 'Import Class...' and choose Counter.

greenfoot10.png
greenfoot11.png

The Counter class will appear in the Actor classes list.

 

Right-click on the Counter, choose the 'new Counter()' option and drag it into the world.

​

Now right-click on the background and select 'Save the World' once you have dragged the counter into the world.

greenfoot12.png

2. Increase the Counter by 1

Two lines of code are required to increase the counter.

​

Add this code when your main character is removing the collectible object.

greenfoot15.png
greenfoot14.png

This code allows your main character to access the 'add' method from the Counter class.

​

The method 'add' just increases the value of the counter by the number in the brackets.

​

To decrease the counter, type a negative value in the brackets, such as -1.

3. Compile and Run

compile.PNG

Click the Compile button at the top of the code editor.

​

Then you can go back to the main Greenfoot window and click Run to test if your counter increases.

Click on me if you've got an error that you're stuck with.

© CSNewbs 2024

The written, video and visual content of CSNewbs is protected by copyright. © 2024
bottom of page