Greetings,
I watched the first 2 tutorials on Greenfoot programming, and a couple of youtube videos regarding counters. Copying a bit of code from one, and doing it exactly as the guy in the video, mine didn't work. It is supposed to count how many leaves has this wombat/ant eaten.
World code:
Wombat code:
Leaf code:
Counter code:
The error I'm getting is that the non static variable theCounter cannot be cross referenced with a static variable or something like that.
Thanks in advance.
import greenfoot.*; // imports Actor, World, Greenfoot, GreenfootImage
import greenfoot.Color;
import java.util.Random;
/**
* A world where wombats live.
*
* @author Michael Kolling
* @version 1.0.1
*/
public class WombatWorld extends World
{
public int x;
public int y;
public int wombX;
public int wombY;
public Counter theCounter;
public WombatWorld()
{
super(12, 12, 60);
setBackground("cell.jpg");
prepare();
}
public void prepare()
{
addObject(theCounter,0,0);
getCounter();
}
public Counter getCounter()
{
return theCounter;
}
public void act(){
if (getObjects(Leaf.class).isEmpty()){
NewLeaf();
}
if (getObjects(Wombat.class).isEmpty()){
AddWomb();
}
}
/**
* Populate the world with a fixed scenario of wombats and leaves.
*/
public void populate()
{
Wombat w1 = new Wombat();
addObject(w1, 3, 3);
Wombat w2 = new Wombat();
addObject(w2, 1, 7);
Leaf l1 = new Leaf();
addObject(l1, 5, 3);
Leaf l2 = new Leaf();
addObject(l2, 0, 2);
Leaf l3 = new Leaf();
addObject(l3, 7, 5);
Leaf l4 = new Leaf();
addObject(l4, 2, 6);
Leaf l5 = new Leaf();
addObject(l5, 5, 0);
Leaf l6 = new Leaf();
addObject(l6, 4, 7);
}
public void NewLeaf()
{
Leaf Leaf = new Leaf();
int x = Greenfoot.getRandomNumber(getWidth());
int y = Greenfoot.getRandomNumber(getHeight());
addObject(Leaf, x, y);
}
public void AddWomb()
{
Wombat Klank = new Wombat();
int wombX = Greenfoot.getRandomNumber(getWidth());
int wombY = Greenfoot.getRandomNumber(getHeight());
addObject(Klank,wombX,wombY);
}
/**
* Place a number of leaves into the world at random places.
* The number of leaves can be specified.
*/
public void randomLeaves(int howMany)
{
for(int i=0; i<howMany; i++) {
Leaf leaf = new Leaf();
int x = Greenfoot.getRandomNumber(getWidth());
int y = Greenfoot.getRandomNumber(getHeight());
addObject(leaf, x, y);
}
}
}import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)
import java.util.List;
import java.util.ArrayList;
/**
* Wombat. A Wombat moves forward until it can't do so anymore, at
* which point it turns left. If a wombat finds a leaf, it eats it.
*
* @author Michael Kolling and Marko Radoicic
* @version 1.0.15
*/
public class Wombat extends Actor
{
private static final int EAST = 0;
private static final int WEST = 1;
private static final int NORTH = 2;
private static final int SOUTH = 3;
private int direction;
private int leavesEaten;
public Wombat()
{
setDirection(EAST);
leavesEaten = 0;
}
/**
* Do whatever the wombat likes to to just now.
*/
public void act()
{
if(foundLeaf()) {
eatLeaf();
}
else if(canMove()) {
if (Greenfoot.isKeyDown("left")) {
if (direction==WEST) { move(); } else {
GoLeft(); }
} else if (Greenfoot.isKeyDown("right")) {
if (direction==EAST) { move(); } else {
GoRight(); }
} else if (Greenfoot.isKeyDown("Up")) {
if (direction==NORTH) { move(); } else {
GoUp(); }
} else if (Greenfoot.isKeyDown("Down")) {
if (direction==SOUTH) { move(); } else {
GoDown(); }
} else move();
}
else if (Greenfoot.isKeyDown("left")) {
GoLeft();
}
else if (Greenfoot.isKeyDown("right")) {
GoRight();
}
else if (Greenfoot.isKeyDown("Up")) {
GoUp();
}
else if (Greenfoot.isKeyDown("Down")) {
GoDown();
}
}
/**
* Check whether there is a leaf in the same cell as we are.
*/
public boolean foundLeaf()
{
Actor leaf = getOneObjectAtOffset(0, 0, Leaf.class);
if(leaf != null) {
return true;
}
else {
return false;
}
}
/**
* Eat a leaf.
*/
public void eatLeaf()
{
Actor leaf = getOneObjectAtOffset(0, 0, Leaf.class);
if(leaf != null) {
// eat the leaf...
getWorld().removeObject(leaf);
Counter counter = WombatWorld.getCounter();
WombatWorld.theCounter.bumpCount(1);
}
}
/**
* Move one cell forward in the current direction.
*/
public void move()
{
if (!canMove()) {
return;
}
switch(direction) {
case SOUTH :
setLocation(getX(), getY() + 1);
break;
case EAST :
setLocation(getX() + 1, getY());
break;
case NORTH :
setLocation(getX(), getY() - 1);
break;
case WEST :
setLocation(getX() - 1, getY());
break;
}
}
/**
* Test if we can move forward. Return true if we can, false otherwise.
*/
public boolean canMove()
{
World myWorld = getWorld();
int x = getX();
int y = getY();
switch(direction) {
case SOUTH :
y++;
break;
case EAST :
x++;
break;
case NORTH :
y--;
break;
case WEST :
x--;
break;
}
// test for outside border
if (x >= myWorld.getWidth() || y >= myWorld.getHeight()) {
return false;
}
else if (x < 0 || y < 0) {
return false;
}
return true;
}
public void GoLeft() //if (Greenfoot.isKeyDown("left"))
{
switch(direction) {
case SOUTH :
setDirection(WEST);
break;
case EAST :
setDirection(WEST);
break;
case NORTH :
setDirection(WEST);
break;
}
}
public void GoRight() //if (Greenfoot.isKeyDown("left"))
{
switch(direction) {
case SOUTH :
setDirection(EAST);
break;
case NORTH :
setDirection(EAST);
break;
case WEST :
setDirection(EAST);
break;
}
}
public void GoDown() //if (Greenfoot.isKeyDown("left"))
{
switch(direction) {
case EAST :
setDirection(SOUTH);
break;
case NORTH :
setDirection(SOUTH);
break;
case WEST :
setDirection(SOUTH);
break;
}
}
public void GoUp() //if (Greenfoot.isKeyDown("left"))
{
switch(direction) {
case SOUTH :
setDirection(NORTH);
break;
case EAST :
setDirection(NORTH);
break;
case WEST :
setDirection(NORTH);
break;
}
}
/**
* Sets the direction we're facing.
*/
public void setDirection(int direction)
{
this.direction = direction;
switch(direction) {
case SOUTH :
setRotation(90);
break;
case EAST :
setRotation(0);
break;
case NORTH :
setRotation(270);
break;
case WEST :
setRotation(180);
break;
default :
break;
}
}
/**
* Tell how many leaves we have eaten.
*/
public int getLeavesEaten()
{
return leavesEaten;
}
}import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)
/**
* Leaf - a class for representing leafs.
*
* @author Michael Kolling
* @version 1.0.1
*/
public class Leaf extends Actor
{
public Leaf()
{
}
}import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import greenfoot.Color;
/**
* Write a description of class Counter here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Counter extends Actor
{
private int totalCount = 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 : " + totalCount, 24, Color.GREEN, Color.BLACK));
}
public void bumpCount(int amount)
{
totalCount += amount;
setImage(new GreenfootImage("Score : " + totalCount, 24, Color.GREEN, Color.BLACK));
}
}
