This site requires JavaScript, please enable it in your browser!
Greenfoot back
KimoDev
KimoDev wrote ...

2017/4/27

Help with error! (World Switching)

KimoDev KimoDev

2017/4/27

#
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
java.lang.ClassCastException: LevelTwo cannot be cast to LevelOne
	at Ball.act(Ball.java:23)
Ball Code
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);
        }
    }

}
Meteorite Code
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);
    }
    
  }
thanks
danpost danpost

2017/4/27

#
If there is only one healthBar object in each of your worlds, then you can change lines 21 and 22 of the Meteorite class to this:
healthBar healthBar = (healthBar)myWorld.getObjects(healthBar.class).get(0);
You can do similarly to lines 15 and 16 of the Ball class if you only have one Counter object in each of your worlds.
You need to login to post a reply.