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

2015/1/13

please help. level2 cannot be cast to level1 terminal window error

khalid11 khalid11

2015/1/13

#
java.lang.ClassCastException: Level2 cannot be cast to Level1 at bullet.destroyEnemies(bullet.java:54) at bullet.act(bullet.java:29)
danpost danpost

2015/1/13

#
No code was supplied with this new post discussion thread. The issues was already dealt with in other threads -- TWICE!
khalid11 khalid11

2015/1/13

#
i tried but it didnt work
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.
    }
}
You need to login to post a reply.