Hello guys.. I have been struggling for a while now with getting a PointCounter to work properly. There are two actors, that should "trigger" the PointCounter to add up. In my world, i have created a reference to an object. With this object, there are no problems either, because i can create the object directly in the world. But i also want to somehow create a working reference to an object, that gets created by another object. This i cannot make work correctly. Here are the codes for my game so far:
WORLD CODE
MY SHOOTER:
BULLET:
I guess there are no need for the Pointcounter's code to be written in here, but i can send it, if needed :)
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class ZombieWorld here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class ZombieWorld extends World
{
int ZombieDelay = 0;
PointCounter pointCounter = new PointCounter();
Skydemand skydemand = new Skydemand();
Køremand køremand = new Køremand(pointCounter);
Hjerne hjerne = new Hjerne ();
Zombie zombie = new Zombie();
GreenfootSound shooting =new GreenfootSound("Shooting.wav");
/**
* Constructor for objects of class ZombieWorld.
*/
public ZombieWorld()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(1100, 600, 1);
Prepare();
setPaintOrder(Skydemand.class,Hjerne.class,Køremand.class,Bullet.class,Zombie.class,Splat.class);
}
public void act()
{
ZombieAdd();
}
/**
* Prepare the world for the start of the program. That is: create the initial
* objects and add them to the world.
*/
private void Prepare()
{
addObject(hjerne, getWidth()/2, getHeight()/2);
addObject(skydemand, getWidth()/2+65, getHeight()/2);
addObject(køremand, getWidth()/2, getHeight()/2-80);
addObject(pointCounter,50,25);
}
private void ZombieAdd()
{
ZombieDelay++;
if (ZombieDelay>200)
{
RandomPlacement();
ZombieDelay=0;
}
}
/**
* generates the "Zombies" in random places at the edge of the world
*/
public void RandomPlacement()
{
int chooser = Greenfoot.getRandomNumber(4);
if (chooser==0)
{
int x = Greenfoot.getRandomNumber(getWidth());
int y = 0;
addObject (new Zombie(),x,y);
}
if (chooser==1)
{
int x = Greenfoot.getRandomNumber(getWidth());
int y = getHeight();
addObject (new Zombie(),x,y);
}
if (chooser==2)
{
int x = 0;
int y = Greenfoot.getRandomNumber(getHeight());
addObject (new Zombie(),x,y);
}
if (chooser==3)
{
int x = getWidth();
int y = Greenfoot.getRandomNumber(getHeight());
addObject (new Zombie(),x,y);
}
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Skydemand here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Skydemand extends SmoothMover
{
int change = 0;
int speed = 2;
GreenfootSound swap = new GreenfootSound("swap.wav");
GreenfootSound clockwiseMove = new GreenfootSound("ClockwiseMove.wav");
GreenfootSound counterClockwiseMove = new GreenfootSound("CounterClockwiseMove.wav");
GreenfootSound shooting =new GreenfootSound("Shooting.wav");
/**
* Act - do whatever the Skydemand wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
moving();
checkKeys();
}
private void checkKeys()
{
String key = Greenfoot.getKey();
if("space".equals(key))
{
fire();
}
if ("w". equals(key))
{
change=change+180;
swap.play();
}
if ("s". equals(key))
{
change=change-180;
swap.play();
}
}
/**
* moving the object
*/
private void moving()
{
if (Greenfoot.isKeyDown("d"))
{
change=change+speed;
clockwiseMove.play();
clockwiseMove.setVolume(75);
}
if (Greenfoot.isKeyDown("a"))
{
change=change-speed;
counterClockwiseMove.play();
counterClockwiseMove.setVolume(70);
}
setRotation(change);
setLocation(getWorld().getWidth()/2, getWorld().getHeight()/2);
move(65);
}
/**
* Fire the cannon
*/
private void fire()
{
ZombieWorld zombieWorld = new ZombieWorld();
Bullet bullet = new Bullet(zombieWorld.pointCounter);
getWorld().addObject(bullet,getX(),getY());
bullet.setRotation(getRotation());
bullet.move(40.0);
shooting.play();
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Bullet here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Bullet extends Mover
{
int Detected = 0;
private int life = Greenfoot.getRandomNumber(5)+20;
GreenfootSound hit = new GreenfootSound("Hit.wav");
GreenfootSound miss = new GreenfootSound("Miss.wav");
private PointCounter pointCounter;
public Bullet(PointCounter pointcounter)
{
pointCounter = pointcounter;
}
/**
* Act - do whatever the Bullet wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
move(15.0);
lifeChecker();
}
private void lifeChecker()
{
life--;
if (life==0)
{
getWorld().addObject(new Splat(),getX(), getY());
getWorld().removeObject(this);
miss.play();
miss.setVolume(90);
}
if (life!=0)//checks whether the object has been removed yet or not.
{
checkCollision();
}
}
private void checkCollision()
{
Actor zombie = getOneIntersectingObject(Zombie.class);
if (zombie != null)
{
getWorld().removeObject(zombie);
getWorld().addObject (new Splat(), getX(), getY());
hit.play();
pointCounter.setValue(pointCounter.getValue() + 1);
getWorld().removeObject(this);
}
}
}

