i have been trying to make a game with multiple worlds and ive had this problem with variables. i need a way to make them castable from any world, but everything is from level1 in my code. i just need some type of world class variable that goes between classes.
this is all the code i used and the error i got:
warning:
java.lang.ClassCastException: Level2 cannot be cast to Level1
at Player.hurt(Player.java:139)
at Player.act(Player.java:23)
Level1 World:
my Player (uses most of the Variables, just given as an example as most classes use world variables)
all i'm trying to find it a way to bring this variable code into my next world and worlds after, but as said i have no clue how to do this. any help will be appreciated and thank you for the help.
public int score = 0; public int Life = 4; public int money = 0; /** * Constructor for objects of class Level1. * */ public Level1() { // Create a new world with 600x400 cells with a cell size of 1x1 pixels. super(1000, 800, 1); prepare(); } public void act() { Spawn(); } public void adjustScore(int amount) { score += amount; showText("Score:" + score, 940, 40); } public void adjustMoney(int amount1) { money += amount1; showText("Money:" + money, 940, 780); } public void adjustLife(int amount2) { Life += amount2; showText("Life" + Life, 70, 25); } public int getLife() { return Life; } public void Switch() { Greenfoot.setWorld(new TheShop()); } public void Spawn() { if (Greenfoot.getRandomNumber(500) < 1) { addObject(new SkeletonPlace(),Greenfoot.getRandomNumber(1000) , Greenfoot.getRandomNumber(800)); } if (Greenfoot.getRandomNumber(420) < 1) { addObject(new CoinPlace(),Greenfoot.getRandomNumber(1000) , Greenfoot.getRandomNumber(800)); } }
public int BlinkTime = 10; public int HurtTime = 80; /** * Act - do whatever the Player wants to do. This method is called whenever * the 'Act' or 'Run' button gets pressed in the environment. */ public void act() { Move(); blink(); Score(); Money(); hurt(); } /** * Makes the creature go backward */ public void Move() { if ( Greenfoot.isKeyDown("S")) { setLocation(getX(),getY()+2); setRotation(0); Wall wall = null; wall = (Wall)getOneIntersectingObject(Wall.class); if (wall != null) { setLocation(getX(), getY()-4); } } else if ( Greenfoot.isKeyDown("W")) { setLocation(getX(),getY()-2); setRotation(180); Wall wall = null; wall = (Wall)getOneIntersectingObject(Wall.class); if (wall != null) { setLocation(getX(), getY()+4); } } else if ( Greenfoot.isKeyDown("D")) { setLocation(getX()+2,getY()); setRotation(270); Wall wall = null; wall = (Wall)getOneIntersectingObject(Wall.class); if (wall != null) { setLocation(getX()-4, getY()); } } else if ( Greenfoot.isKeyDown("A")) { setLocation(getX()-2,getY()); setRotation(90); Wall wall = null; wall = (Wall)getOneIntersectingObject(Wall.class); if (wall != null) { setLocation(getX()+4, getY()); } } } public void blink() { if(BlinkTime<300) { BlinkTime = BlinkTime + 1; } else if(BlinkTime >= 300) { BlinkTime = 0; } if(BlinkTime >=15 ) { setImage("Player.png"); } else { setImage("Player_Blink.png"); } } public void Score() { if(isTouching(Coin.class)) { ((Level1)getWorld()).adjustScore(100); } } public void Money() { if(isTouching(Coin.class)) { ((Level1)getWorld()).adjustMoney(5); removeTouching(Coin.class); } } public void hurt() { if(HurtTime<80) { HurtTime++; } if(HurtTime==10) { setImage("Player_Nothing.png"); } if(HurtTime==30) { setImage("Player.png"); } if(HurtTime==40) { setImage("Player_Nothing.png"); } if(HurtTime==60) { setImage("Player.png"); } if(HurtTime==70) { setImage("Player_Nothing.png"); } if(isTouching(Skeleton.class)&& (HurtTime >= 80)) { ((Level1)getWorld()).adjustLife(-1); setImage("Player.png"); HurtTime = 0; } if(((Level1)getWorld()).Life == 0) { Greenfoot.setWorld(new GameOver()); } }