Hello, I need some help with my code. I am trying to make the Score Counter go up every time my fish eats a cherry. I am not sure if I am putting the code in the correct place or not. I would greatly appreciate an explanation if possible.
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
* Write a description of class Counter here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Counter extends Actor
{
int score = 0;
/**
* 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("Score : " + score, 24, Color.GREEN, Color.BLACK));
}
public void addScore()
{
score++;
}
}import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Fish1 here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Fish extends Player
{
public int cherriesEaten;
public Fish()
{
cherriesEaten = 0;
}
/**
* Act - do whatever the Fish1 wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
if (foundCherry()) {
eatCherry();
}
if (Greenfoot.isKeyDown("left"))
{
setLocation(getX() - 10, getY());
}
if (Greenfoot.isKeyDown("right"))
{
setLocation(getX() + 5, getY());
}
if (Greenfoot.isKeyDown("up"))
{
setLocation(getX(), getY() - 5);
}
if (Greenfoot.isKeyDown("down"))
{
setLocation(getX(), getY() + 5);
}
}
public boolean foundCherry()
{
Actor cherry = getOneObjectAtOffset(0, 0, Cherry.class);
if(cherry != null) {
return true;
}
else {
return false;
}
}
public void eatCherry()
{
Actor cherry = getOneObjectAtOffset(0, 0, Cherry.class);
if(cherry != null) {
World myWorld = getWorld();
getWorld().removeObject(cherry);
Ocean ocean = (Ocean)myWorld;
Counter counter = ocean.getCounter();
counter.addScore();
}
}
public int getCherriesEaten()
{
return cherriesEaten;
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* An Ocean Experience
*
* @author Gabe Piper
* @version 1.0.1
*/
public class Ocean extends World
{
Counter counter = new Counter();
public Ocean()
{
super(600, 400, 1, false);
}
public Counter getCounter()
{
return counter;
}
public void act()
{
if(Greenfoot.getRandomNumber(150) < 1)
{
addObject(new Seal(), getWidth(), Greenfoot.getRandomNumber(getHeight()));
}
if(Greenfoot.getRandomNumber(100) < 1)
{
addObject(new Cherry(), getWidth(), Greenfoot.getRandomNumber(getHeight()));
}
}
}

