my lives aren't getting removed from the world and i don't know why. Can someone please help.
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class MyWorld here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class MyWorld extends World
{
// int getX() x = Car3X;
int fastcarTimer = 100;
int slowcarTimer = 100;
Lives life1 = new Lives();
Lives life2 = new Lives();
Lives life3 = new Lives();
Frog frog = new Frog();
/**
* Constructor for objects of class MyWorld.
*
*/
public MyWorld()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(1000, 900, 1);
addObject(new Finish(), 305, 32); // adding the finish lilypad
addObject(new Finish(), 515, 32); // adding the finish lilypad
addObject(new Finish(), 713, 32); // adding the finish lilypad
addObject(new Finish(), 905, 32); // adding the finish lilypad
addObject(new Finish(), 95, 32); // adding the finish lilypad
addObject(new Car1(), 10, 630);
addObject(new Car1(), 500, 630);
addObject(new Car1(), 900, 630);
addObject(new Car1(), 300, 630);
addObject(new Car3(), 995, 537);
addObject(new Car3(), 750, 537);
addObject(new Car3(), 395, 537);
addObject(new Car3(), 195, 537);
addObject(new Car3(), 95, 537);
addObject(new Car4(), 195, 736);
addObject(new Car2(), 300, 736);
addObject(new Car4(), 600, 736);
addObject(new Car2(), 994, 736);
addObject(new Car2(), 800, 736);
addObject(new Frog(), 500, 850);
showText("Lives : " , 40, 880);
}
public void act()
{
// if (Car3X == 0)
// {
// }
randomslowCarSpawner();
randomfastCarSpawner();
createLives();
}
public void slowCarSpawn()
{
}
public void fastCarSpawn()
{
}
public void createLives()
{
if(frog.getLives() == 3)
{
addObject(life1, 90, 880);
addObject(life2, 120, 880);
addObject(life3, 150, 880);
}else if(frog.getLives() == 2)
{
addObject(life1, 90, 880);
addObject(life2, 120, 880);
}else if(frog.getLives() == 1)
{
addObject(life1, 90, 880);
}
// public void removeAtWorldEdge()
// {
// if (isAtEdge(Car3()))
// {
// removeObject(Car3());
// spawnTimer();
// }
// }
// public void spawnTimer()
// {
// spawnTimer++;
// if (spawnTimer == 0)
// {
// addObject(new Car3(), 36, 36);
// }
// }
}
public void randomfastCarSpawner()
{
fastcarTimer++;
if(Greenfoot.getRandomNumber(60)<50&&fastcarTimer == 100)
{
addObject(new Car1(),10, 630);
}
if(Greenfoot.getRandomNumber(50)<40&&fastcarTimer == 100)
{
addObject(new Car3(),995, 537);
}
if(fastcarTimer == 101)
{
fastcarTimer = 0;
}
}
public void randomslowCarSpawner()
{
slowcarTimer++;
if(Greenfoot.getRandomNumber(50)<70&&slowcarTimer == 100)
{
addObject(new Car4(),995, 736);
}
if(Greenfoot.getRandomNumber(40)<60&&slowcarTimer == 100)
{
addObject(new Car2(),995, 736);
}
if(slowcarTimer == 101)
{
slowcarTimer = 0;
}
}
public void reset()
{
if(frog.getLives() == 0)
{
Greenfoot.stop();
}
else if(frog.getLives() == 2){
removeObject(life1);
}else{
removeObject(life2);
}
frog.setLocation(800,850);
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Frog here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Frog extends Actor
{
private String[] move = {"frogup1.png","frogup2.png","frogup3.png","frogup4.png","frogup5.png","frogup6.png"};
private int frameCounterMove = 0;
private int reload;
int lives = 3;
Lives life = new Lives();
/**
* Act - do whatever the Frog wants to do. This method is called whenever
* the 'Act' or 'Run' button gets pressed in the environment.
*/
public void act()
{
move();
removeWhenTouching();
}
public void move()
{
if (Greenfoot.isKeyDown("up"))
{
setLocation(getX(), getY()-5);
setRotation(360);
animateOnMove();
}
else if (Greenfoot.isKeyDown("left"))
{
setLocation(getX()-5, getY());
setRotation(270);
animateOnMove();
}
else if (Greenfoot.isKeyDown("right"))
{
setLocation(getX()+5, getY());
setRotation(90);
animateOnMove();
}
else if (Greenfoot.isKeyDown("down"))
{
setLocation(getX(), getY()+5);
setRotation(180);
animateOnMove();
}
}
public void animateOnMove()
{
reload++;
if(reload==3)
{
setImage(move[frameCounterMove]);
frameCounterMove++;
if(frameCounterMove >= move.length)
frameCounterMove=0;
reload = 0;
}
}
public int getLives()
{
return lives;
}
public void loseLife()
{
lives = lives - 1;
}
public void removeWhenTouching()
{
if(isTouching(Car1.class))
{
if(getLives() == 0)
{
}else{
loseLife();
}
reset();
setLocation(800, 850);
}
if(isTouching(Car2.class))
{
if(getLives() == 0)
{
}else{
loseLife();
}
reset();
setLocation(500, 850);
}
if(isTouching(Car3.class))
{
if(getLives() == 0)
{
}else{
loseLife();
}
reset();
setLocation(500, 850);
}
if(isTouching(Car4.class))
{
if(getLives() == 0)
{
}else{
loseLife();
}
reset();
setLocation(500, 850);
}
}
public void reset()
{
if(this.getWorld() instanceof MyWorld)
{
((MyWorld) this.getWorld()).reset();
}
}
}
