I'm making a game for college and I'm not the best of coders anyway, but basically I want to add a smaller version of a ball chosen from a world called 'choose_ball' into a world called 'main'. When I run it, it says;
java.lang.ClassCastException: choose_ball cannot be cast to main
at yel_ball.act(yel_ball.java:13)
at greenfoot.core.Simulation.actActor(Simulation.java:604)
at greenfoot.core.Simulation.runOneLoop(Simulation.java:562)
at greenfoot.core.Simulation.runContent(Simulation.java:221)
at greenfoot.core.Simulation.run(Simulation.java:211)
I have 4 different ball classes all with the same code,
Any ideas of why this error is happening and how to fix it?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | private BallCol colour ; public main() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super ( 600 , 400 , 1 ); grass_1 gs= new grass_1(); addObject (gs, 300 , 200 ); hole ho= new hole(); addObject (ho, 545 , 320 ); colour= new BallCol(); addObject(colour, 100 , 250 ); } public BallCol getBallCol() { return colour ; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | public class BallCol extends Actor { public int set; public void choose( int col) { set=col; GreenfootImage myImage= null ; if (set== 1 ){ myImage= new GreenfootImage( "red_ballXS.fw.png" ); } else if (set== 2 ){ myImage= new GreenfootImage( "blue_ballXS.fw.png" ); } else if (set== 3 ){ myImage= new GreenfootImage( "black_ballXS.fw.png" ); } else if (set== 4 ){ myImage= new GreenfootImage( "yel_ballXS.fw.png" ); } setImage(myImage); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | public class red_ball extends Actor { public void act() { // Add your action code here. if (Greenfoot.mouseClicked( this )) { Greenfoot.setWorld( new main()); main mainWorld=(main) getWorld(); BallCol BallCol=mainWorld.getBallCol(); BallCol.choose( 1 ); } } |