Hello. I am trying to have my "Bee" class remove my "SuitMan" class while at the same time subtracting 1 life and adding another SuitMan to the World. It does subtract the life and removes it, but it doesn't add another one to the world. Here is the code in the Bee class:
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
public class Bee extends Actor
{
private int hSpeed = 3;
GreenfootImage img = new GreenfootImage("bee-left.png");
GreenfootImage img2 = new GreenfootImage("bee-right.png");
public void act()
{
setLocation(getX()+hSpeed, getY());
if(getX()<50){
setImage(img2);
hSpeed = -hSpeed;
}
if(getX()>700){
setImage(img);
hSpeed = -hSpeed;
}
SuitMan sm = (SuitMan)getOneIntersectingObject(SuitMan.class);
if(sm!=null){
sm.lessLives();
getWorld().addObject(sm, 11, 442);
getWorld().removeObject(sm);
}
}
public void explode()
{
Greenfoot.playSound("Explosion.wav");
}
}
Here is the code in the SuitMan class:
import greenfoot.*; // (World, Actor, GreenfootImage, and Greenfoot)
public class SuitMan extends Actor
{
private final double GRAVITY = 1.2;
private final int JUMP_STRENGTH = 15;
private GreenfootImage left = new GreenfootImage("man-left.png");
private GreenfootImage right = new GreenfootImage("man-right.png");
private double vSpeed = 0.0;
private int speed = 5;
private int level = 1;
private boolean firing=false;
private Scoreboard myScore;
private Scoreboard myBullets;
private Scoreboard myLives;
private boolean moreBullets = true;
public SuitMan(Scoreboard score, Scoreboard bullets, Scoreboard lives)
{
myScore = score;
myBullets = bullets;
myLives = lives;
}
public Scoreboard getScoreboard()
{
return myScore;
}
public Scoreboard getScoreboard2()
{
return myBullets;
}
public Scoreboard getScoreboard3()
{
return myLives;
}
public void act()
{
checkKeys();
Actor ol = getOneIntersectingObject(OpenLift.class);
if(ol!=null) {
if(level==1) {
Greenfoot.setWorld(new SpaceWorld2(this));
}
if(level==2) {
Greenfoot.setWorld(new SpaceWorld3(this));
}
level++;
}
Actor a = getOneIntersectingObject(Airplane.class);
if(a!=null) {
Greenfoot.stop();
}
if(!onAnything()) {
fall();
}
}
public void jump()
{
if(onAnything()) {
vSpeed = -JUMP_STRENGTH;
fall();
}
}
public void fall()
{
vSpeed = vSpeed + GRAVITY;
setLocation( getX(), (int)(getY()+vSpeed) );
if(getY()>getWorld().getHeight()-3){
lessLives();
getWorld().addObject(new SuitMan(myScore, myBullets, myLives), 11, 442);
getWorld().removeObject(this);
}
}
public void checkKeys()
{
if(Greenfoot.isKeyDown("left")) {
setLocation(getX()-speed,getY());
setImage(left);
}
if(Greenfoot.isKeyDown("right")) {
setLocation(getX()+speed,getY());
setImage(right);
}
if(Greenfoot.isKeyDown("up")) {
jump();
}
if(Greenfoot.isKeyDown("space")){
if( firing==false){
//casting
if(haveBullets()){
lessBullets();
getWorld().addObject(new Bullet(), getX(), getY());
Greenfoot.playSound("EnergyGun.wav");
}
firing=true;
}
}
if(!Greenfoot.isKeyDown("space")){
firing=false;
}
}
public void lessBullets()
{
myBullets.changeScore(-1);
if(myBullets.getScore()==0){
moreBullets = false;
}
}
public boolean haveBullets()
{
return moreBullets;
}
public boolean onAnything()
{
int dy = getImage().getHeight()/2;
Actor cliff = getOneObjectAtOffset(0,dy,null);
return cliff != null;
}
public void lessLives()
{
myLives.changeScore(-1);
if(myLives.getScore()==0){
Greenfoot.setWorld(new GameOver2());
}
}
}
Thank you, I really appreciate it.

