how would i get a counter to keep track of how many items are being collected and display it as they go through the level. the max items they can get is 2
import greenfoot.*;
/**
* This class Level1 displays the first level for the user to play.
*
* Jacqueline Reilly
* Wednesday, January 18, 2017
*/
public class Level1 extends World
{
Counter counter = new Counter ();
/**
* Constructor for objects of class Level1.
*
*/
public Level1()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(950, 700, 1);
//Making background fit the screen
GreenfootImage Bg = new GreenfootImage ("background.jpg");
Bg.scale (getWidth (), getHeight ());
setBackground (Bg);
//Adding the platforms
Platform platform = new Platform ();
addObject (platform, 268, 653);
Platform platform1 = new Platform ();
addObject (platform1, 704, 652);
//Adding characters
addObject (new MrKrabs(), 93, 522);
addObject (new Plankton(), 208, 490);
//Adding object for Mr. Krabs to collect
KrabbyPatty krabbyPatty = new KrabbyPatty ();
addObject (krabbyPatty, 484, 457);
addObject (counter, 100, 100);
KrabbyPatty krabbyPatty1 = new KrabbyPatty ();
addObject (krabbyPatty1, 842, 480);
//Adding object for Plankton to collect
FormulaBottle bottle = new FormulaBottle ();
addObject (bottle, 329, 374);
FormulaBottle bottle1 = new FormulaBottle ();
addObject (bottle1, 691, 436);
//
}
}
if (isTouching(ClassName.class))
{
removeTouching(ClassName.class);
((Level1) getWorld()).counter.add(1);
} if (isTouching (KrabbyPatty.class))
{
removeTouching (KrabbyPatty.class);
((Level1) getWorld()).counter.add(1);
}
}}import java.awt.Color;
/**
* Write a description of class Counter here.
*
* Jacqueline Reilly
* January 18, 2017
*/
public class Counter extends Actor
{
int score = 0;
public Counter()
{
setImage(new GreenfootImage("Krabby Patty's : " + score, 24, Color.GREEN, Color.BLACK));
}
/**
* Act - do whatever the Counter wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
setImage(new GreenfootImage("Krabby Patty's : " + score, 24, Color.GREEN, Color.BLACK));
}
public void setScore()
{
score++;
}
}