I need help with creating levels. I have created a level one of my game and now want to create level two. however, level two world gets an error when a ball hits a meteorite. i know why this happens but i don't know a solution for it. please help
Error Code
Ball Code
Meteorite Code
thanks
java.lang.ClassCastException: LevelTwo cannot be cast to LevelOne at Ball.act(Ball.java:23)
public class Ball extends MissileLauncher { /** * Act - do whatever the Ball wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { setLocation(getX(), getY() - 4); Actor ball = getOneIntersectingObject(Meteorite.class); if (ball != null) { World myWorld = getWorld(); LevelOne levelone = (LevelOne)myWorld; Counter counter = levelone.getCounter(); counter.addScore(); getWorld().removeObject(ball); } removeBall(); } public void removeBall() { if (getY() < 25) { getWorld().removeObject(this); } } }
public class Meteorite extends Actor { /** * Act - do whatever the Meteorite wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { move(); removeHealth_Object(); } public void removeHealth_Object() { if (getY() > 350) { Actor Meteorite = getOneIntersectingObject(Planet.class); if (Meteorite != null) { World myWorld = getWorld(); LevelOne levelone = (LevelOne)myWorld; healthBar healthbar = levelone.getHealthBar(); healthbar.loseHealth(); if (healthbar.health <= 0) { Greenfoot.setWorld(new Control()); Greenfoot.stop(); } } getWorld().removeObject(this); } } public void move() { setLocation(getX(), getY() + 2); } }