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

2018/5/16

Help with world casting and how to make it work

Shade1177 Shade1177

2018/5/16

#
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:
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));
        }
    }
my Player (uses most of the Variables, just given as an example as most classes use world variables)
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());
        }
    }
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.
danpost danpost

2018/5/16

#
Shade1177 wrote...
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: << Error Omitted >> << Code Omitted >> 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
You could create an intermediate class between World and your different world levels and place those common fields and methods in it so that regardless of which level is running, the cast will always be to the same place.
Shade1177 Shade1177

2018/5/17

#
yeah but wont that class reset every time i set the world to a new level? idk maybe im not understanding fully...
Shade1177 Shade1177

2018/5/17

#
(sorry if i need explanation of every little thing... its my first year being taught how to code so everything is coming newly to me)
danpost danpost

4 days ago

#
Shade1177 wrote...
yeah but wont that class reset every time i set the world to a new level? idk maybe im not understanding fully...
Yes -- it will reset also; but, you can also adjust the field values during the transition. The main purpose for the intermediate class is so that there is no question as to which class the fields are in (you can cast directly to that class regardless of which level is active).
Shade1177 Shade1177

4 days ago

#
ok ill try to set something like that tonight then and come back to you if i need help... thanks for giving me and idea on what to do... ill try your suggestion and see how it turns out
You need to login to post a reply.