java.lang.ClassCastException: Level2 cannot be cast to Level1
at bullet.destroyEnemies(bullet.java:54)
at bullet.act(bullet.java:29)
import greenfoot.*; // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)
/**
* Write a description of class bullet here.
*
* @author (your name)
* @version (a version number or a date)
*/
public class bullet extends Actor
{
int time=10;
public boolean atWorldEdge()
{
if(getX() < 10 || getX() > getWorld().getWidth() - 10)
return true;
if(getY() < 10 || getY() > getWorld().getHeight() - 10)
return true;
else
return false;
}
/**
* 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()
{
destroyEnemies ();
move(5);
kill();
if (this.atWorldEdge()) {
getWorld().removeObject(this);
}
}
public bullet()
{
GreenfootImage image=getImage();
image.scale(40,6);
setImage(image);
}
public void destroyEnemies()
{
//"Enemy" can be any class that you want the bullet to destroy.
Actor enemy = getOneIntersectingObject(enemy.class);
if(enemy != null)
{
World myWorld = getWorld();
getWorld().removeObject(enemy);
Level1 level1 = (Level1)myWorld;
Counter counter = level1.getCounter();
counter.addScore();
if (getWorld() instanceof Level1) ((Level1)getWorld()).getCounter().addScore();}
}
public void kill()
{
//"Enemy" can be any class that you want the bullet to destroy.
Actor blueenemy = getOneIntersectingObject(blueenemy.class);
if(blueenemy != null)
{
getWorld().removeObject(blueenemy);
World myWorld = getWorld();
Level2 level2 = (Level2)myWorld;
Counter counter = level2.getCounter();
counter.addScore();
}
else if (getWorld() instanceof Level2) ((Level2)getWorld()).getCounter().addScore();
// etc.
}
}