Hey guys.. In my world class, i want to execute some methods if the mouse is clicked on an actor class. But the
if ((Greenfoot.mouseClicked(Object)) seems to only work if i sinsert "this" instead of Object. If i insert an Actor class in there, it does not work.
Here is my code
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();
LifeCounter lifeCounter = new LifeCounter();
Skydemand skydemand = new Skydemand();
Køremand køremand = new Køremand(pointCounter);
Hjerne hjerne = new Hjerne ();
Zombie zombie = new Zombie();
SinglePlayer singlePlayer = new SinglePlayer();
TwoPlayer twoPlayer = new TwoPlayer();
GreenfootSound shooting =new GreenfootSound("Shooting.wav");
boolean Started = false;
/**
* 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);
addObject (new Instructions(), getWidth()/2, 100);
addObject (new SinglePlayer(), 200, getHeight()/2);
addObject (new TwoPlayer(), 900, getHeight()/2);
//PrepareSingle();
//PrepareTwo();
setPaintOrder(Skydemand.class,Hjerne.class,Køremand.class,Bullet.class,Zombie.class,Splat.class);
}
public void act()
{
ZombieAdd();
StartUp();
}
private void StartUp()
{
if (Started==false)
{
if ((Greenfoot.mouseClicked(singlePlayer)==true))
{
PrepareSingle();
removeObject (singlePlayer);
removeObject(twoPlayer);
Started=true;
}
if ((Greenfoot.mouseClicked(twoPlayer)==true))
{
PrepareTwo();
removeObject (singlePlayer);
removeObject(twoPlayer);
Started=true;
}
}
}
/**
* Prepare the world for the start of the program(if single player is selected). That is: create the initial
* objects and add them to the world.
*/
private void PrepareSingle()
{
addObject(hjerne, getWidth()/2, getHeight()/2);
addObject(skydemand, getWidth()/2+65, getHeight()/2);
addObject(pointCounter,50,25);
addObject(lifeCounter, getWidth()/2, 40);
}
/**
* Prepare the world for the start of the program(if two player is selected. That is: create the initial
* objects and add them to the world.
*/
private void PrepareTwo()
{
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);
addObject(lifeCounter, getWidth()/2, 40);
}
private void ZombieAdd()
{
if (Started==true)
{
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);
}
}
}
