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.
this is my code of the others-class.
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!
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();
}
}
}
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;
}
}
}


