I'm having a problem with my counter, i with the form of the game being a zombie survival game i can't make it so the counter adds 1 when the bullet makes contact with the zombie.
INSIDE Counter Class
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Counter here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Counter extends Actor
{
private static final Color transparent = new Color(0,0,0,0);
private GreenfootImage background;
private int value;
private int target;
public Counter(){
background = getImage();//gets the image from the class
value = 0;
target = 0;
updateImage();
}
/**
* 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()
{
if (value < target) {
value ++;
updateImage();
}
else if (value > target) {
value --;
updateImage();
}
}
public void add(int score){
target += score;
}
public int getValue(){
return value;
}
public void setValue(int newValue)
{
target = newValue;
value = newValue;
updateImage();
}
public void updateImage()
{
GreenfootImage image = new GreenfootImage(background);
GreenfootImage text = new GreenfootImage("" + value, 22, Color.YELLOW, transparent);
image.drawImage(text, (image.getWidth()-text.getWidth())/2,
(image.getHeight()-text.getHeight())/2);
setImage(image);
}
}
INSIDE Bullet Class
private Counter counter;
public Bullet(Counter pointCounter){
counter = pointCounter;
}
public void destroyEnemy(){
if(collectItem(Enemy.class)){
collect(Enemy.class);
getWorld().removeObject(this);
}
else
if(atWorldEdge()){getWorld().removeObject(this);}
}
INSIDE PLAYER1 Class
import greenfoot.*; // collectToken (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Player1 here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Player1 extends Characters
{
private int shotdelay;
/**
* Act - do whatever the Player1 wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
// Add your action code here.
keyPress();
fireBullet();
turnAtEdge();
}
public void keyPress() {
if(Greenfoot.isKeyDown("left")){
turn(-5);
}
if (Greenfoot.isKeyDown("right")){
turn(5);
}
if (Greenfoot.isKeyDown("up")){
move(5);
}
}
private Counter counter;
public Player1(Counter pointCounter){
counter = pointCounter;
}
public void fireBullet(){
if(shotdelay > 0) shotdelay --;
if (shotdelay == 0 && Greenfoot.isKeyDown("space")){
Bullet b = new Bullet(counter);
b.setRotation(getRotation());
getWorld().addObject(b, getX(), getY());
b.move(20);
shotdelay = 20;
}
}
public void turnAtEdge()
{
if (atWorldEdge())
{
turn(5);
}
}
public void checkKeys()
{
if (Greenfoot.isKeyDown("left"))
{
turn(-5);
}
if (Greenfoot.isKeyDown("right"))
{
turn(5);
}
}
public Player1() {
GreenfootImage image = getImage();
image.scale(50,100);
setImage(image);
}
}
