Hello, I have a question. I have a world and its my level 2 world, and everytime I get hit I am supposed to lose a life which works perfectly fine in my level 1 world. I am getting an error saying java.lang.ClassCastAException. Level2 cannot be cast to MyWorld. Then it highlights two lines in red which I will show on the next line. I put my level2 source code on the bottom along with my life counter just in case. See anything I can add?
at Man.checkHit(Man.java:64)
at Man.act(man.java:20)
//man class, fyi its a plane
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Man here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Man extends Actor
{
public Man()
{
GreenfootImage myImage = getImage();
int myNewHeight = (int)myImage.getHeight()/3;
int myNewWidth = (int)myImage.getWidth()/3;
myImage.scale(myNewWidth, myNewHeight);
}
public void act()
{
checkHit();
checkKeys();
}
private void checkKeys()
{
if (Greenfoot.isKeyDown("up"))
{
setLocation(getX(), getY()-8);
}
if (Greenfoot.isKeyDown("down"))
{
setLocation(getX(), getY()+8);
}
if (Greenfoot.isKeyDown("right"))
{
setLocation(getX()+4, getY());
}
if (Greenfoot.isKeyDown("left"))
{
setLocation(getX()-4, getY());
}
if ("space".equals(Greenfoot.getKey()))
{
shoot();
}
}
private void shoot()
{
Ammo ammo = new Ammo();
getWorld().addObject(ammo, getX(), getY());
}
public void checkHit()
{
Actor a = this.getOneIntersectingObject(Rocket.class);
if (a != null)
{
this.getWorld().removeObject(a);
World myWorld = getWorld();
MyWorld world = (MyWorld)(myWorld);
LifeCounter count = world.getCounter();
count.countLives();
}
Actor b= this.getOneIntersectingObject(Enemy.class);
if (b != null)
{
this.getWorld().removeObject(b);
World myWorld = getWorld();
MyWorld world = (MyWorld)(myWorld);
LifeCounter count = world.getCounter();
count.countLives();
}
}
}
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class Level2 here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class Level2 extends World
{
boolean startMusic = true;
private int time;
private int span = 3;
GreenfootSound backGroundMusic = new GreenfootSound("MySounds.mp3");
public Level2()
{
// Create a new world with 600x400 cells with a cell size of 1x1 pixels.
super(1024,768,1);
time = 1500;
Music();
prepare();
}
public void Music()
{
if (startMusic)
{
backGroundMusic.playLoop();
startMusic= false;
}
}
private void prepare()
{
Man man = new Man();
addObject(man,702,241);
Level level = new Level();
addObject(new Level(), 512, 260);
LifeCounter life = new LifeCounter();
addObject(life, 880, 20);
}
public void act()
{
showTheTime();
Music();
if (Greenfoot.getRandomNumber(100) <3 )
{
addObject(new Enemy(), Greenfoot.getRandomNumber(100), Greenfoot.getRandomNumber(500));
}
if (Greenfoot.getRandomNumber(100) <3)
{
addObject(new Rocket(), Greenfoot.getRandomNumber(100), Greenfoot.getRandomNumber(500));
}
}
public void showTime()
{
showText("Time: " + time, 50, 20);
}
public void showTheTime()
{
time--;
showTime();
if(time==0)
{
Greenfoot.stop();
showText("You Won! ", 450, 275);
backGroundMusic.stop();
}
}
}
[code]import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
import java.awt.Color;
/**
* Write a description of class LifeCounter here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class LifeCounter extends Actor
{
int lives = 2;
boolean startMusic = true;
GreenfootSound backGrounddMusic = new GreenfootSound("Sound2.mp3");
public void act()
{
setImage(new GreenfootImage("Lives: " + lives, 24, Color.GREEN, Color.BLACK));
gameOver();
Music();
}
public void countLives()
{
lives--;
}
public void gameOver()
{
if (lives == 0)
{
Greenfoot.stop();
World myWorld = getWorld();
GameOver game = new GameOver();
myWorld.addObject(game, 450,216);
backGrounddMusic.stop();
}
}
public void endMusic()
{
}
public void Music()
{
if (startMusic)
{
backGrounddMusic.playLoop();
startMusic= false;
}
}

