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

2019/3/16

Error: BossFight cannot be cast to game

Razaph Razaph

2019/3/16

#
I'm trying to add a Ingame-shop System to my game, but now I get this Error and don't know what to do. Please help :)
java.lang.ClassCastException: BossFight cannot be cast to game
	at Player.shop(Player.java:76)
	at Player.act(Player.java:29)
	at greenfoot.core.Simulation.actActor(Simulation.java:567)
	at greenfoot.core.Simulation.runOneLoop(Simulation.java:530)
	at greenfoot.core.Simulation.runContent(Simulation.java:193)
	at greenfoot.core.Simulation.run(Simulation.java:183)
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Player here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Player extends Actor
{
    int life = 3;
    boolean canFire = true;
    boolean canBuy = true;
    boolean canBuy2 = true;
    int abzug = 0;
    int money = 0;
    /**
     * Act - do whatever the Player wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public Player()
    {
        setRotation(180);
    }
    public void act() 
    {
        move();
        fire();
        shop();
        hitByProjectile();
        hitByProjectileMultiplayer();
    }  
    public void move()
    {
        if(Greenfoot.isKeyDown("right"))
        {
            setLocation(getX()+8,getY());
        }
        if(Greenfoot.isKeyDown("left"))
        {
            setLocation(getX()-8,getY());
        }
    }
    public void fire()
    {
        if (Greenfoot.isKeyDown("space") && canFire == true)
        {
            if (Greenfoot.getRandomNumber(100)<50) 
            {
                getWorld().addObject(new Shot(),getX()+60, getY()-30);
                canFire = false;
            } 
                else
            {
                getWorld().addObject(new Shot(),getX()-60, getY()-30);
                canFire = false;
            }
        }
        else if (!Greenfoot.isKeyDown("space"))
        {
           canFire = true; 
        }
    }
    public void hitByProjectile()
    {
        Actor bossshot = getOneIntersectingObject(BossShot.class);
        if (bossshot != null)
        {
            Greenfoot.setWorld(new CreditsLose());
        }
    }
    
    public void shop()
    {
        World world = getWorld();
        game game = (game)world;
        Coins coins = game.getCoins();
        money = game.getCoins().coins;
        if (Greenfoot.isKeyDown("1") && canBuy == true && money > 50)
        {
            getWorld().addObject(new smallfleet(), 100, 500);
            getWorld().addObject(new smallfleet(), 250, 500);
            getWorld().addObject(new smallfleet(), 650, 500);
            getWorld().addObject(new smallfleet(), 800, 500);
            abzug = abzug+50;
            canBuy = false;
        } else if (!Greenfoot.isKeyDown("1"))
        {
           canBuy = true; 
        }
        
        if (Greenfoot.isKeyDown("2") && canBuy2 == true && money > 100)
        {
            getWorld().addObject(new bigfleet(), 125, 450);
            getWorld().addObject(new smallfleet(), 350, 500);
            getWorld().addObject(new smallfleet(), 600, 500);
            getWorld().addObject(new bigfleet(), 775, 450);
            abzug = abzug+100;
            canBuy = false;
        } else if (!Greenfoot.isKeyDown("2"))
        {
           canBuy2 = true; 
        }
    }
    
        public void hitByProjectileMultiplayer()
    {
        Actor player2shot = getOneIntersectingObject(Player2Shot.class);
        if (player2shot != null)
        {
            getWorld().removeObject(player2shot);
            life--;
        }
        
        if (life == 0)
        {
            Greenfoot.setWorld(new Credits());
        }
    }
}
Can you help me?
Razaph Razaph

2019/3/16

#
This is the class which changes the world
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)


/**
 * Write a description of class Counter here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Counter extends Actor
{
    int score = 0;
    /**
     * Act - do whatever the Counter wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public Counter()
    {
        setImage(new GreenfootImage("Punkte: " + score, 40, Color.GREEN, Color.BLACK)); 
    }
    public void act() 
    {
        setImage(new GreenfootImage("Punkte: " + score, 40, Color.GREEN, Color.BLACK));
        CheckBoss();
    }
    public void addScore()
    {
        score++;
    }
    public void CheckBoss()
    {
        if (score >= 10)
        {
            getWorld().removeObjects(getWorld().getObjects(null));
            Greenfoot.setWorld(new BossFight());
        }
    }
}
Razaph Razaph

2019/3/16

#
Already fixed it ^^ When I joined the BossFight World I tried to Access to the game world and that was the reason
You need to login to post a reply.