I'm trying to create a game that will increase the points of a counter if the object is placed in the correct category.
My class "vegetables" is a subclass of the actor class "category."
The image of the food (actor class) randomly changes between 5 images of objects that belong to 5 different categories.
I want to make the code so that if the image of the object is equal to a certain image ("food-0.png", "food-1.png", etc.), 5 points will be added to the counter.
In my coding, there seems to be an error and I'm not sure how to fix it.
public class Vegetables extends Category
{
private Counter counter;
private GreenfootImage food = getImage(Food.class);
private GreenfootImage vegetables = new GreenfootImage("food-2.png");
private GreenfootImage grains = new GreenfootImage("food-1.png");
private GreenfootImage fruits = new GreenfootImage("food-0.png");
private GreenfootImage protein = new GreenfootImage("food-3.png");
private GreenfootImage dairy = new GreenfootImage("food-4.png");
/**
* Create a new vegetable category with a reference to the counter in the game.
*/
public Vegetables(Counter pointCounter)
{
counter = pointCounter;
}
/**
* Act - do whatever the Category wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
Actor Food = getOneIntersectingObject(Food.class);
if (Food!=null)
{
World GameWorld = getWorld();
GameWorld.removeObject(Food);
checkFood();
}
}
/**
* Check Food
*/
public void checkFood()
{
if (food == vegetables)
{
counter.add(5);
createNewFood();
if (counter.getValue() >= 50)
{
gameWin();
}
}
else if (food != vegetables)
{
reset();
gameOver();
}
}
