I'm sorry, I typed instead of . That's why my normal text now also is displayed as code. I hope it isn't annoying for you.
int y = 0;
spaceinvaders spi = (spaceinvaders) getWorld();
Actor invader = getOneIntersectingObject(Invader.class);
if (invader != null)
{
spi.removeObject(invader);
spi.removeObject(this);
Counter counter = spi.getCounter();
if (y==0)
{
counter.Counter();
y = 1;
}
counter.bumpCount();
return;
} public Counter getCounter()
{
Counter counter = new Counter();
return counter;
}private Counter cc;
public Counter getCounter()
{
return cc;
}private Counter counter;
public yourWorld()
{
... Create your world here...
counter = new Counter();
}public Counter getCounter()
{
return counter;
}public Counter getCounter()
{
Counter counter = new Counter();
return counter;
}Counter counter = new Counter(); // sets up an instance world Counter variable with the counter
// Your world constructor
public spaceinvaders()
{
super (600, 400, 1);
prepare();
}
// add your objects to the world
public void prepare()
{
// add some objects here
addObject(counter, 100, 10);
// maybe, add some more objects here
}
// The following method return a reference to the already created counter
public Counter getCounter()
{
return counter;
}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()
{
World world;
world = getWorld();
System.out.println("The value of the counter is: " + totalCount);
if (totalCount == 4 && world.getObjects(GameWon.class).isEmpty())
{
world.addObject(new GameWon(), 450, 300);
}
}
public void bumpCount()
{
totalCount = totalCount+ 1;
//System.out.println("The value of the counter is: " + totalCount);
}
}System.out.println("The value of counter is: " + Integer.toString(totalCount) );System.out.println("The value of counter is: " + totalCount);[.code]
And another tip, I see you doing this:
[code]World world;
world = getWorld();
world.dosomething;getWorld().dosomething;
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()
{
}
public void bumpCount()
{
totalCount = totalCount+ 1;
//System.out.println("The value of the counter was: " + totalCount);
World world;
world = getWorld();
if (totalCount == 4)
{
spaceinvaders spaceWorld = (spaceinvaders) getWorld();
spaceWorld.finish();
return;
}
}
}public void finish()
{
GameOver go;
go = new GameOver ();
addObject(go, 900, 500);
}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 int totalCount = 0;
private int totalLife = 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()
{
}
public void bumpCount()
{
totalCount = totalCount+ 1;
//System.out.println("The value of the counter was: " + totalCount);
World world;
world = getWorld();
if (totalCount == 4)
{
spaceinvaders spaceWorld = (spaceinvaders) getWorld();
spaceWorld.finish();
}
}
//this is another bit of code I'm working on to remove lives. It's not working yet (hence the comments) but I'll be able to work it out after I get bumpCount worked out :)
public void bumpLife()
{
totalLife = totalLife +1;
World world;
world = getWorld();
Actor invader = getOneIntersectingObject(Invader.class);
if (totalCount == 1)
{
//world.removeObject(heart1);
}
}
}import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
/**
* Write a description of class Bullet here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Bullet extends Rocket
{
/**
* 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()
{
// Add your action code here.
setLocation(getX() + 30, getY() );
spaceinvaders spi = (spaceinvaders) getWorld();
Actor invader = getOneIntersectingObject(Invader.class);
if (invader != null)
{
spi.removeObject(invader);
spi.removeObject(this);
Counter counter = spi.getCounter();
counter.bumpCount();
return;
}
if (this.atWorldEdge()==true)
{
World world;
world = getWorld();
world.removeObject(this);
return;
}
}
// this code for removing the bullet from the edge of the screen was found here: http://www.greenfoot.org/topics/1088/15
public boolean atWorldEdge()
{
if (getX() > getWorld().getWidth() - getImage().getWidth() || getY() > getWorld().getHeight() - getImage().getHeight())
{
return true;
}
else
{
return false;
}
}
}import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class spaceinvaders here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class spaceinvaders extends World
{
/**
* Constructor for objects of class spaceinvaders.
*
*/
private Counter counter;
private Counter li;
//spaceinvaders gw = new spaceinvaders();
public spaceinvaders()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(900, 600, 1);
startscreen ss;
ss = new startscreen();
addObject(ss, 450, 300);
}
public void game()
{
Rocket r;
r = new Rocket ();
addObject(r, 100, 100);
//Creepinginvader
Creepinginvader c;
c = new Creepinginvader ();
addObject(c, 900, 300);
//spasmInvader
spasmInvader s;
s = new spasmInvader ();
addObject(s, 900, 150);
//ZigZagInvader
ZigZagInvader z;
z = new ZigZagInvader ();
addObject(z, 900, 450);
//CustomInvader
CustomInvader u;
u = new CustomInvader ();
addObject(u, 900, 100);
speed sp;
sp = new speed ();
shots sh;
sh = new shots ();
addObject(sh, 250, 250);
addObject(sp, 150, 150);
counter = new Counter();
li = new Counter();
additionalinvaders ai;
ai = new additionalinvaders();
addObject(ai,100,100);
Counter cblah;
cblah = new Counter();
addObject(cblah,200,200);
heart1 h1;
h1 = new heart1();
addObject(h1,400,50);
}
public void add()
{
Creepinginvader c2;
c2 = new Creepinginvader ();
addObject(c2, 900, 300);
}
public void add2()
{
ZigZagInvader z2;
z2 = new ZigZagInvader ();
addObject(z2, 900, 20);
}
public void add3()
{
CustomInvader u2;
u2 = new CustomInvader ();
addObject(u2, 900, 500);
}
public void finish()
{
GameOver go;
go = new GameOver ();
addObject(go, 900, 500);
}
public Counter getCounter()
{
return counter;
}
public Counter getLife()
{
return li;
}public spaceinvaders()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(900, 600, 1);
startscreen ss;
ss = new startscreen();
addObject(ss, 450, 300);
}public spaceinvaders()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(900, 600, 1);
startscreen ss;
ss = new startscreen();
addObject(ss, 450, 300);
counter = new Counter();
li = new Counter();
}