Pretty new to Java can someone help with an addObject() problem?
Hi I'm pretty knew to programming and I have an assignment which I've been working on but I am stuck
I have to create a 'space invaders' style game, and I've got some basics working. I wanted to add a boss enemy, which appears when all other enemies have been destroyed. It kind of works, only the program spawns a seemingly infinite amount of the boss which then creates a 'wipe' effect across the top of the screen.
Here is my code:
Code :
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.util.List;
public class Space extends World
{
/**
* Constructor for objects of class Space.
*
*/
public Space()
{
// Create a new world with 200x250 cells with a cell size of 3x3 pixels.
super(200, 250, 3);
//add class with starstreaks
addObject(new EmptyBox(),1,1);
//add player character
addObject(new Player(), 100, 228);
//add 4 romulan enemies row 1
for(int i=0; i<4; i++){
addObject(new Romulan(), (25 + 50 * i), 15);
}
//add 3 klingon enemies row 2
for(int i=0; i<3; i++){
addObject(new Klingon(), (50 + 50 * i), 40);
}
//add 4 romulan enemies row 3
for(int i=0; i<4; i++){
addObject(new Romulan(), (25 + 50 * i), 65);
}
//add 3 klingon enemies row 4
for(int i=0; i<3; i++){
addObject(new Klingon(), (50 + 50 * i), 90);
}
//add 3 shields
for(int i=0; i<3; i++){
addObject(new Shield(), (30 + 70 * i), 180);
}
}
[B]public void act(){
List<Enemy> enemies = getObjects(Enemy.class);
if(enemies.size()==0 ){
addObject(new Borg(), 100, 30 );
}
}[/B]
}
I believe the part in bold is the problem, but I dont know why. Klingon and Romulan are subclasses of the Enemy class. The Borg class is it's own seperate class, but with the exact same functions as Enemy. I made it seperate as I figured it would keep respawning if it was a subclass of Enemy.
Any ideas?
Re: Pretty new to Java can someone help with an addObject() problem?
Its ok, after more than an hour I've got it.
I simply added private int bossValue = 0; at the beginning and then changed act to this:
Code :
public void act(){
List<Enemy> enemies = getObjects(Enemy.class);
if(enemies.size()==0 && bossValue==0 ){
//for(int i=0; i<1; i++){
addObject(new Borg(), 100, 30 );
bossValue =1;
//}
}
}