I'm trying to use the code from the tutorial on counters (https://www.greenfoot.org/doc/howto-1) to create a health and score counter. Using the tutorials code, the counter for when the player collides with a coin increases just fine, but when I try to use the same code for making health go up or down when they collide with either medicine or drugs, I get an error about the method bumpCount and reduceCount not being able to be referenced from a static context. I'm not sure what it is that I am doing wrong - I would have thought that I could use the same way of scoring multiple times, I'm new to Greenfoot and do not know what static context means, nor how to make it non-static (if that would fix the issue).
Here is all the code related to my issue:
Player:
hp:
coin_counter:
test_room:
Any help with this is greatly appreciated.
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Player here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Player extends Actor
{
/**
* Act - do whatever the Player wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
int x = getX();
int y = getY();
if (Greenfoot.isKeyDown("a"))
{
setLocation(x-1,y);
if (getOneIntersectingObject(Wall.class) != null)
setLocation (getX()+1,getY());
if (getOneIntersectingObject(half_wall_side.class) != null)
setLocation (getX()+1,getY());
if (getOneIntersectingObject(half_wall_up.class) != null)
setLocation (getX()+1,getY());
if (getOneIntersectingObject(wall_10x1.class) != null)
setLocation (getX()+1,getY());
if (getOneIntersectingObject(wall_11x1.class) != null)
setLocation (getX()+1,getY());
if (getOneIntersectingObject(wall_1x7.class) != null)
setLocation (getX()+1,getY());
if (getOneIntersectingObject(blue_wall.class) != null)
setLocation (getX()+1,getY());
if (getOneIntersectingObject(blue_half_wall_side.class) != null)
setLocation (getX()+1,getY());
if (getOneIntersectingObject(blue_half_wall_up.class) != null)
setLocation (getX()+1,getY());
if (getOneIntersectingObject(blue_wall_10x1.class) != null)
setLocation (getX()+1,getY());
if (getOneIntersectingObject(blue_wall_11x1.class) != null)
setLocation (getX()+1,getY());
if (getOneIntersectingObject(blue_wall_1x7.class) != null)
setLocation (getX()+1,getY());
if (getOneIntersectingObject(blue_half_wall_side_10x1.class) != null)
setLocation (getX()+1,getY());
if (getOneIntersectingObject(blue_half_wall_side_11x1.class) != null)
setLocation (getX()+1,getY());
if (getOneIntersectingObject(blue_half_wall_1x7.class) != null)
setLocation (getX()+1,getY());
}
if (Greenfoot.isKeyDown("d"))
{
setLocation(x+1,y);
if (getOneIntersectingObject(Wall.class) != null)
setLocation (getX()-1,getY());
if (getOneIntersectingObject(half_wall_side.class) != null)
setLocation (getX()-1,getY());
if (getOneIntersectingObject(half_wall_up.class) != null)
setLocation (getX()-1,getY());
if (getOneIntersectingObject(wall_10x1.class) != null)
setLocation (getX()-1,getY());
if (getOneIntersectingObject(wall_11x1.class) != null)
setLocation (getX()-1,getY());
if (getOneIntersectingObject(wall_1x7.class) != null)
setLocation (getX()-1,getY());
if (getOneIntersectingObject(blue_wall.class) != null)
setLocation (getX()-1,getY());
if (getOneIntersectingObject(blue_half_wall_side.class) != null)
setLocation (getX()-1,getY());
if (getOneIntersectingObject(blue_half_wall_up.class) != null)
setLocation (getX()-1,getY());
if (getOneIntersectingObject(blue_wall_10x1.class) != null)
setLocation (getX()-1,getY());
if (getOneIntersectingObject(blue_wall_11x1.class) != null)
setLocation (getX()-1,getY());
if (getOneIntersectingObject(blue_wall_1x7.class) != null)
setLocation (getX()-1,getY());
if (getOneIntersectingObject(blue_half_wall_side_10x1.class) != null)
setLocation (getX()-1,getY());
if (getOneIntersectingObject(blue_half_wall_side_11x1.class) != null)
setLocation (getX()-1,getY());
if (getOneIntersectingObject(blue_half_wall_1x7.class) != null)
setLocation (getX()-1,getY());
}
if (Greenfoot.isKeyDown("w"))
{
setLocation(x,y-1);
if (getOneIntersectingObject(Wall.class) != null)
setLocation (getX(),getY()+1);
if (getOneIntersectingObject(half_wall_side.class) != null)
setLocation (getX(),getY()+1);
if (getOneIntersectingObject(half_wall_up.class) != null)
setLocation (getX(),getY()+1);
if (getOneIntersectingObject(wall_10x1.class) != null)
setLocation (getX(),getY()+1);
if (getOneIntersectingObject(wall_11x1.class) != null)
setLocation (getX(),getY()+1);
if (getOneIntersectingObject(wall_1x7.class) != null)
setLocation (getX(),getY()+1);
if (getOneIntersectingObject(blue_wall.class) != null)
setLocation (getX(),getY()+1);
if (getOneIntersectingObject(blue_half_wall_side.class) != null)
setLocation (getX(),getY()+1);
if (getOneIntersectingObject(blue_half_wall_up.class) != null)
setLocation (getX(),getY()+1);
if (getOneIntersectingObject(blue_wall_10x1.class) != null)
setLocation (getX(),getY()+1);
if (getOneIntersectingObject(blue_wall_11x1.class) != null)
setLocation (getX(),getY()+1);
if (getOneIntersectingObject(blue_wall_1x7.class) != null)
setLocation (getX(),getY()+1);
if (getOneIntersectingObject(blue_half_wall_side_10x1.class) != null)
setLocation (getX(),getY()+1);
if (getOneIntersectingObject(blue_half_wall_side_11x1.class) != null)
setLocation (getX(),getY()+1);
if (getOneIntersectingObject(blue_half_wall_1x7.class) != null)
setLocation (getX(),getY()+1);
}
if (Greenfoot.isKeyDown("s"))
{
setLocation(x,y+1);
if (getOneIntersectingObject(Wall.class) != null)
setLocation (getX(),getY()-1);
if (getOneIntersectingObject(half_wall_side.class) != null)
setLocation (getX(),getY()-1);
if (getOneIntersectingObject(half_wall_up.class) != null)
setLocation (getX(),getY()-1);
if (getOneIntersectingObject(wall_10x1.class) != null)
setLocation (getX(),getY()-1);
if (getOneIntersectingObject(wall_11x1.class) != null)
setLocation (getX(),getY()-1);
if (getOneIntersectingObject(wall_1x7.class) != null)
setLocation (getX(),getY()-1);
if (getOneIntersectingObject(blue_wall.class) != null)
setLocation (getX(),getY()-1);
if (getOneIntersectingObject(blue_half_wall_side.class) != null)
setLocation (getX(),getY()-1);
if (getOneIntersectingObject(blue_half_wall_up.class) != null)
setLocation (getX(),getY()-1);
if (getOneIntersectingObject(blue_wall_10x1.class) != null)
setLocation (getX(),getY()-1);
if (getOneIntersectingObject(blue_wall_11x1.class) != null)
setLocation (getX(),getY()-1);
if (getOneIntersectingObject(blue_wall_1x7.class) != null)
setLocation (getX(),getY()-1);
if (getOneIntersectingObject(blue_half_wall_side_10x1.class) != null)
setLocation (getX(),getY()-1);
if (getOneIntersectingObject(blue_half_wall_side_11x1.class) != null)
setLocation (getX(),getY()-1);
if (getOneIntersectingObject(blue_half_wall_1x7.class) != null)
setLocation (getX(),getY()-1);
}
Actor Medicine;
Medicine = getOneObjectAtOffset(0, 0, Medicine.class);
if (Medicine != null)
{
World world;
hit_med();
world = getWorld();
world.removeObject(Medicine);
//add 1 to hp
}
Actor Drug;
Drug = getOneObjectAtOffset(0, 0, Drug.class);
if (Drug != null)
{
World world;
hit_drug();
world = getWorld();
world.removeObject(Drug);
//remove 1 from hp
}
Actor Coin;
Coin = getOneObjectAtOffset(0, 0, Coin.class);
if (Coin != null)
{
World world;
hit_coin();
world = getWorld();
world.removeObject(Coin);
//add 1 to currency
}
Actor Scene_Changer_L1_1;
Scene_Changer_L1_1 = getOneObjectAtOffset(0, 0, Scene_Changer_L1_1.class);
if (Scene_Changer_L1_1 != null)
{
Greenfoot.setWorld(new Level_1_2());
}
Actor Scene_Changer_L1_2;
Scene_Changer_L1_2 = getOneObjectAtOffset(0, 0, Scene_Changer_L1_2.class);
if (Scene_Changer_L1_2 != null)
{
Greenfoot.setWorld(new Level_1_3());
}
Actor Scene_Changer_L1_3;
Scene_Changer_L1_3 = getOneObjectAtOffset(0, 0, Scene_Changer_L1_3.class);
if (Scene_Changer_L1_3 != null)
{
Greenfoot.setWorld(new Level_1_4());
}
Actor Scene_Changer_L1_4;
Scene_Changer_L1_4 = getOneObjectAtOffset(0, 0, Scene_Changer_L1_4.class);
if (Scene_Changer_L1_4 != null)
{
Greenfoot.setWorld(new Level_2_1());
}
Actor Scene_Changer_L2_1;
Scene_Changer_L2_1 = getOneObjectAtOffset(0, 0, Scene_Changer_L2_1.class);
if (Scene_Changer_L2_1 != null)
{
Greenfoot.setWorld(new Level_2_2());
}
}
private void hit_coin()
{
test_room testRoom = (test_room) getWorld();
coin_counter counter = testRoom.getCounter();
counter.bumpCount(1);
}
private void hit_med()
{
test_room testRoom = (test_room) getWorld();
hp counter = testRoom.getHpCounter();
hp.bumpCount(1);
}
private void hit_drug()
{
test_room testRoom = (test_room) getWorld();
hp counter = testRoom.getHpCounter();
hp.reduceCount(1);
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class hp here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class hp extends Actor
{
private int totalCount = 0;
public void Counter()
{
setImage(new GreenfootImage("HP: 3", 20, Color.WHITE, Color.BLACK));
}
public void bumpCount(int amount)
{
totalCount += 1;
setImage(new GreenfootImage("HP: " + totalCount, 20, Color.WHITE, Color.BLACK));
}
public void reduceCount(int amount)
{
totalCount -= 1;
setImage(new GreenfootImage("HP: " + totalCount, 20, Color.WHITE, Color.BLACK));
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class coin_counter here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class coin_counter extends Actor
{
private int totalCount = 0;
public void Counter()
{
setImage(new GreenfootImage("Coins: 0", 20, Color.WHITE, Color.BLACK));
}
public void bumpCount(int amount)
{
totalCount += 1;
setImage(new GreenfootImage("Coins: " + totalCount, 20, Color.WHITE, Color.BLACK));
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class test_room here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class test_room extends World
{
public coin_counter theCounter;
public hp hpCounter;
/**
* Constructor for objects of class test_room.
*
*/
public test_room()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(600, 400, 1);
theCounter = new coin_counter();
addObject(theCounter,545,46);
hpCounter = new hp();
addObject(hpCounter,56,44);
prepare();
}
public coin_counter getCounter()
{
return theCounter;
}
public hp getHpCounter()
{
return hpCounter;
}
/**
* Prepare the world for the start of the program.
* That is: create the initial objects and add them to the world.
*/
private void prepare()
{
Coin coin = new Coin();
addObject(coin,536,304);
Drug drug = new Drug();
addObject(drug,427,337);
Medicine medicine = new Medicine();
addObject(medicine,306,343);
half_wall_side half_wall_side = new half_wall_side();
addObject(half_wall_side,62,198);
half_wall_side.setLocation(102,156);
Wall wall30 = new Wall();
addObject(wall30,110,294);
half_wall_up half_wall_up = new half_wall_up();
addObject(half_wall_up,202,76);
half_wall_up.setLocation(223,65);
Player player3 = new Player();
addObject(player3,320,190);
Coin coin4 = new Coin();
addObject(coin4,548,204);
Coin coin5 = new Coin();
addObject(coin5,404,63);
}
}

