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

2012/3/27

keep getting an error message

1
2
michelman michelman

2012/3/27

#
hello everyone, for school I am working on a sort of mario-like game. I am pretty close to an end now, but I keep getting an error message after trying to make multiple levels. java.lang.ClassCastException: ScrollingWorld2 cannot be cast to ScrollingWorld at Others.scrollingMethods(Others.java:73) at moving_Ground.act(moving_Ground.java:17) at greenfoot.core.Simulation.actActor(Simulation.java:507) at greenfoot.core.Simulation.runOneLoop(Simulation.java:470) at greenfoot.core.Simulation.runContent(Simulation.java:204) at greenfoot.core.Simulation.run(Simulation.java:194) at first I had way more error codes, because some classes called ScrollingWorld instead of ScrollingWorld2, so I made an if-statement so that the game can see which world it is in at the moment.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * The superclass of all actors that need to scroll (i.e. all the actors except Player)
 * 
 * @author Bertie Wheen
 * @version 0.1
 */
public class Others extends Erm
{
    public int scrollX; //The variable used when scrolling.

    public Others()
    {
        scrollX = 0;
    }

    /**
     * Used for setting the actor's location.
     */
    public void setLocation()
    {
        setLocation(getX() + scrollX, getY());
    }
    
    /**
     * This is used for responding to keyboard input by the user.
     * This will only happen when the player is far enough to the left or right of the screen for scrolling to be needed.
     * @param How fast the scrolling should be. 3-4 is recommended. If a negative value is used, the controls will be inverted.
     */
    public void checkKeyPress(int amount)
    {
        ScrollingWorld theWorld = (ScrollingWorld) getWorld();
        Erm erm = (Erm) theWorld.getErm();
        if(Greenfoot.isKeyDown("left") && erm.shouldScroll == true)
        {
            scrollX = amount;
        }
        else if(Greenfoot.isKeyDown("right") && erm.shouldScroll == true)
        {
            scrollX = -amount;
        }
        else
        {
            scrollX = 0;
        }
    }
    
    public void checkKeyPress2(int amount)
    {
        ScrollingWorld2 theWorld2 = (ScrollingWorld2) getWorld();
        Erm erm = (Erm) theWorld2.getErm();
        if(Greenfoot.isKeyDown("left") && erm.shouldScroll == true)
        {
            scrollX = amount;
        }
        else if(Greenfoot.isKeyDown("right") && erm.shouldScroll == true)
        {
            scrollX = -amount;
        }
        else
        {
            scrollX = 0;
        }
    }
    
    /**
     * A call to the following should be in all scrolling actor's act methods.
     * hierin staat ook de rensnelheid en de normale loopsnelheid, wanneer die dus verandert worden moet dat ook hier gebeuren!
     */
    public void scrollingMethods()
    {
        ScrollingWorld theWorld = (ScrollingWorld) getWorld();
        if(theWorld.getLevel()==0)
        {        
            if(Greenfoot.isKeyDown("shift"))
            {
                checkKeyPress(7);
            }
            if(!Greenfoot.isKeyDown("shift"))
            {
                checkKeyPress(5);
            }
            setLocation();
        }
        
        if(theWorld.getLevel()!=0)
        {        
            if(Greenfoot.isKeyDown("shift"))
            {
                checkKeyPress2(7);
            }
            if(!Greenfoot.isKeyDown("shift"))
            {
                checkKeyPress2(5);
            }
            setLocation();
        }
    }
}
this is my code of the others-class.
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class moving_Ground here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class moving_Ground  extends Others
{
    /**
     * Act - do whatever the moving_Ground wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        scrollingMethods();
        moveAround();
    }    
    
    private int distance_moved = 0;
    public void moveAround()
    {
        move(2);
        distance_moved++;
        distance_moved++;
        
           if (distance_moved >= 240)
        {
            turn(180);
            distance_moved = 0;
        }
    }
}
this is the code of the moving_ground class. I really have no idea what to do now! is there anyone who knows how I can fix this? thanks in advance!
kiarocks kiarocks

2012/3/27

#
When you call
ScrollingWorld theWorld = (ScrollingWorld) getWorld();
you happen to be in ScrollingWorld2, so Because it is not ScrollingWorld and does not extend ScrollingWorld, it throws that exception.
michelman michelman

2012/3/27

#
I dont really understand what you mean by that. Could you explain?
kiarocks kiarocks

2012/3/27

#
ScrollingWorld2 is not extending ScrollingWorld, right?
michelman michelman

2012/3/27

#
I guess not? It is a whole new world if that is what you mean
kiarocks kiarocks

2012/3/27

#
Can I see the ScrollingWorld2 code?
michelman michelman

2012/3/27

#
import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class ScrollingWorld here.
 * 
 * @author Bertie Wheen
 * @version 0.1
 */
public class ScrollingWorld2 extends World
{
    public Enemy1 theEnemy1;
    public Player thePlayer;
    public Erm theErm;
    
    private Score scr;
    private Life life;
    private MenuButton button;
    private GameOver gameOver;
    
    
    private enum GameState{MENU,PLAYING,GAMEOVER};   
    private GameState state;
    
   
    public ScrollingWorld2()
    {    
        super (1594, 560 , 1, false); //The extra 'false' parameter means that the world is unbounded.
        
        preparePlaying();
        
       
        
        
    }
    
    private void prepareMenu()
    {
        // Clear the world
        removeObjects(getObjects(Actor.class));
                
        // Create the menu
        button = new MenuButton();        
        addObject(new MenuBackground(),getWidth()/2,getHeight()/2);
        addObject(button,getWidth()-60,getHeight()-60);
        
        // Set the game state
        state = GameState.MENU;
    }
    
    private void preparePlaying()
    {
        // Clear the world
        removeObjects(getObjects(Actor.class));
        
        // Add score, life, and cannon actors
        scr = new Score();
        addObject(scr,getWidth()/2,15);  
        
        life = new Life();
        addObject(life,getWidth()-50,15);
      
        addEnemies();
        addTiles();
        hindernissenkort();
        hindernissenlang();
        muntjes();
        addPlayer();
        // Set the game state
        state = GameState.PLAYING;
    }
    
     private void prepareGameOver()
    {
        // Clear the world
        removeObjects(getObjects(Actor.class)); 
      
        // Add gameover screen
        gameOver = new GameOver();
        addObject(gameOver,getWidth()/2,getHeight()/2);
        
        // Add the final score
        int fscore = scr.getScore();
        addObject(new FinalScore(fscore),getWidth()-75,60);
        
                                
        // Set the game state
        state = GameState.GAMEOVER;           
    }
    
    public void dagSter()
    {
        scr.addScore(5);
        addScore(5);
    }
    
    public void dagMuntje()
    {
        scr.addScore(1);
        addScore(1);
    }
    
    public void actMenu()
    {
        if(Greenfoot.mouseClicked(button))
        {
            preparePlaying();
            addTiles();
            hindernissenkort();
            hindernissenlang();
            muntjes();
            
            theErm = new Erm();
            addObject(theErm, 0, -10);
        
        
            
            
            addObject(new Win(), 4650, getHeight()/2+150);
        }
    }
    
    public void Gedood()
    {
        life.takeLife();
        
        if (life.isDead())
        {
            prepareGameOver();
        }
    }
    
    public void act()
    {
        switch(state)
        {
            case MENU:
            {
                actMenu();
                break;
            }            
            case PLAYING:
            {                  
                actPlaying();   
                break;
            }
            case GAMEOVER:
            {
                actGameOver();
                break;
            }
            
        }
    }
    
     public void actPlaying()
    {
        
    }
    
    
    private GreenfootSound music = new GreenfootSound("algemeen2.mp3");

    public void started()
    {
    music.playLoop();
    }
   
    /**
     * Creates tiles for building your world.
     */
    public void addTiles()
    {
        
        addObject(new moving_Ground(), 390, 300);
        addObject(new moving_Ground(), 610, 200);
        for(int i=1;i<10;i++) //aantal blokjes die die achter elkaar zet
        {
            addObject(new Ground(), i*30-15, 552);
            
            //addObject(new Kasteel(), 1500, getHeight() /2 -20);
        }
    }
  
    public void hindernissenkort()
    {
        for(int i=1;i<5;i++)
        {
            addObject(new Ground(), i*30-15+270, 450);
            addObject(new Ground(), i*30-15+850, 552);
            
            
        }
        for(int i=1;i<20;i++)
        {
            addObject(new Afgrond(), i*30-15 + 270, 552);
        }
    }
   
    public void hindernissenlang()
    {
        for(int i=1;i<15;i++)
        {
            addObject(new Ground(), i*30-15+970, 552);
            addObject(new Ground(), i*30-15+1390, 400);
            addObject(new Afgrond(), i*30-15+1390, 552);
        }
    }
    
    public void muntjes()
    {
            addObject(new Ster(), 1125, 465); 
            addObject(new Ster(), 1175, 465); 
            
            addObject(new Ijs(), 1100, 480);
            addObject(new Ijs(), 1184, 312);
            addObject(new Ijs(), 1268, 480);
    }
    
    /**
     * Creates enemies.
     */
    public void addEnemies()
    {
        
        theEnemy1 = new Enemy1();
        
        
        
        
        
        
    }
    
    public void addPlayer()
    {
        
        thePlayer = new Player();
        addObject(thePlayer, 20, getHeight()/2+165); //Creates the player.
        if (getObjects(Player.class) == null) 
            {   
                addObject(thePlayer, 20, getHeight()/2+165); 
            }
    }
    
    public Player getPlayer()
    {
        return thePlayer;
    }
    
    public Erm getErm()
    {
        return theErm;
    }
    
     public void actGameOver()
    {
        if(Greenfoot.mouseClicked(gameOver))
        {
            prepareMenu();
        }
    }
    
    public int points2 = 0;

    public void addScore(int newPoints2)
    {
        points2 += newPoints2;
    }
    
    public int getScore()
    {
        return points2;
    }
    
    public void shoarma()
    {
        for(int i=1;i<15;i++)
        {
            addObject(new Muntje(), i*50-15, 465);
            addObject(new Muntje(), i*50-15, 415);
            addObject(new Ster(), i*50-15, 350);
        }
    }
    
    public void Dialog()
    {
    
    }
    
    public int world = 1;
    
    public int getLevel()
    {
        return world;
    }
}
kiarocks kiarocks

2012/3/27

#
So, you cannot cast ScrollingWorld2 to ScrollingWorld because ScrollingWorld2 is not a ScrollingWorld. Therefore, a ClassCastException.
michelman michelman

2012/3/27

#
I still don't know what you mean. I've started with this about a week ago, searching the internet for all the things. Could you please tell me how to change the code or something so that it will work?
danpost danpost

2012/3/27

#
The method scrollingMethods() (lines 71 through 100 of the Others class) is apparently called by both worlds (ScrollingWorld and ScrollingWorld2), but the first executable line of code automatically 'tries' to cast it to ScrollingWorld, not taking into account that it could be a world instance of ScrollingWorld2 instead. I realize that the 'if' statement cannot execute as is, if the world instance is not cast, but there is good news (a way to get around it). Have your 'if' statement check the class, and cast it appropriately inside the 'if'-'else' structure.
if (ScrollingWorld.class.equals(getWorld().getClass()))
{
    ScrollingWorld theWorld = (ScrollingWorld) getWorld();
    // check keys
}
else
{
    ScrollingWorld2 theWorld = (ScrollingWorld2) getWorld();
    // check keys
}
danpost danpost

2012/3/27

#
Upon further examination, you do not even have to cast it to check the keys; so you can remove line 3 and line 8 of the code I just provided. As a side note (do not change your code for this, unless you understand and you really wanted to): (and this will only work if ScrollingWorld and ScrollingWorld2 were your only two worlds) but, if the check to see what world is active was moved to the 'checkKeyPress(int)' method, then you would simply have the following as scrollingMethods:
public void scrollingMethods()
{
    if (Greenfoot.isKeyDown("shift")) checkKeyPress(7); else checkKeyPress(5);
    setLocation();
}
and your only checkKeyPress(int) method would be like:
public void checkKeyPress(int)
{
    if (ScrollingWorld.class.equals(getWorld().getClass()))
    {
        ScrollingWorld theWorld = (ScrollingWorld) getWorld(); // if needed
        // code from original checkKeyPress(int)
    }
    else
    {
        ScrollingWorld2 theWorld = (ScrollingWorld2) getWorld(); // if needed
        // code from checkKeyPress2(int)
    }
}
Then you would have only one checkKeyPress(int) method and a very simple scrollingMethods method.
nccb nccb

2012/3/28

#
@DanPost: Your code:
if (ScrollingWorld.class.equals(getWorld().getClass()))
Can be more simply written as:
if (getWorld() instanceof ScrollingWorld)
The latter handles derived classes (any sub-class of ScrollingWorld will pass the check), whereas the previous won't.
michelman michelman

2012/3/28

#
Thank you! that worked. now that error is gone, but a few new ones came up. I'll try to fix it myself, but if I need help I know where to find you guys! ;)
michelman michelman

2012/3/28

#
java.lang.NullPointerException at Others.checkKeyPress(Others.java:61) at Others.scrollingMethods(Others.java:88) at moving_Ground.act(moving_Ground.java:17) at greenfoot.core.Simulation.actActor(Simulation.java:507) at greenfoot.core.Simulation.runOneLoop(Simulation.java:470) at greenfoot.core.Simulation.runContent(Simulation.java:204) at greenfoot.core.Simulation.run(Simulation.java:194) this is the next error. I can't make anything out of it again :S
sp33dy sp33dy

2012/3/28

#
In your Others class, line 61, a null value is either in a variable that is being used. Open up editor, press Ctrl + L, enter 61 and then look at the line of code. Think about what would happen if null is in amongst that code.
There are more replies on the next page.
1
2