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

2018/1/13

Going from a subclass to another

RipplyPear RipplyPear

2018/1/13

#
Ok so I've been working on a game. You have to collect robots and avoid meteors. I goes through the introduction but, once that's over, it goes through all 3 levels and straight to the deathscreen. I tried to made so that if there are no rockets (there is only one, the player) it goes to the deathscreen and if there are no reckonstations (robots) you go further and in the end the same goes for the rocket but you have to collect your friend instead of robots and you win. The whole code is here:
public class Introduction extends World
{
    String[] items = new String[ 6 ];
    boolean eu = false;
    /**
     * Constructor for objects of class Introduction.
     * 
     */
    public Introduction()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 600, 1); 
    }

    public void act()
    {
        items[0] = ("You are the well-knowwn astronaut Serban Jinga." +
            "\nYou were on a mission to find your lost friend Horia." +
            "\nBut your ship hit an asteroid and lost the reckonstations." +
            "\nNow, you must find them all avoiding the other asteroids." +
            "\nPress 2 to continue");
        items[1] = ("You can move using ←↑→↓ Press 3 to continue");
        items[2] = ("Good luck! Press 4 to start");
        items[3] = ("Welcome! Press 1 to begin");
        if (eu == false)
        {
            showText( items[3] , (getWidth()/3), (getHeight()/3) + 110 );
            eu = true;
        }
        if (Greenfoot.isKeyDown("1"))
        {
            showText( items[0] , (getWidth()/3), (getHeight()/3) + 110 );
        }
        if (Greenfoot.isKeyDown("2"))
        {
            showText( items[1] , (getWidth()/3), (getHeight()/3) + 110 );
        }
        if (Greenfoot.isKeyDown("3"))
        {
            showText( items[2] , (getWidth()/3), (getHeight()/3) + 110 );
        }
        if (Greenfoot.isKeyDown("4"))
        {
            Greenfoot.setWorld(new level() );
        }
    }
}
public class level extends World
{
    Actor Reckonstation ;
    Actor Rocket;
    int number= getObjects(Reckonstation.class).size();
    int number2= getObjects(Rocket.class).size();
    /**
     * Constructor for objects of class level.
     * 
     */
    public level()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(800, 600, 1); 
        prepare();
    }
    private void prepare()
    {
         Rocket rocket = new Rocket();
        addObject(rocket,82,308);
        Reckonstation reckonstation = new Reckonstation();
        addObject(reckonstation,96,515);
        Reckonstation reckonstation2 = new Reckonstation();
        addObject(reckonstation2,276,519);
        Reckonstation reckonstation3 = new Reckonstation();
        addObject(reckonstation3,525,525);
        Reckonstation reckonstation4 = new Reckonstation();
        addObject(reckonstation4,742,524);
        Reckonstation reckonstation5 = new Reckonstation();
        addObject(reckonstation5,635,430);
        Reckonstation reckonstation6 = new Reckonstation();
        addObject(reckonstation6,419,424);
        Reckonstation reckonstation7 = new Reckonstation();
        addObject(reckonstation7,173,430);
        Reckonstation reckonstation8 = new Reckonstation();
        addObject(reckonstation8,295,323);
        Reckonstation reckonstation9 = new Reckonstation();
        addObject(reckonstation9,539,330);
        Reckonstation reckonstation10 = new Reckonstation();
        addObject(reckonstation10,744,341);
        Reckonstation reckonstation11 = new Reckonstation();
        addObject(reckonstation11,660,230);
        Reckonstation reckonstation12 = new Reckonstation();
        addObject(reckonstation12,420,229);
        Reckonstation reckonstation13 = new Reckonstation();
        addObject(reckonstation13,170,222);
        Reckonstation reckonstation14 = new Reckonstation();
        addObject(reckonstation14,41,145);
        Reckonstation reckonstation15 = new Reckonstation();
        addObject(reckonstation15,316,148);
        Reckonstation reckonstation16 = new Reckonstation();
        addObject(reckonstation16,553,149);
        Reckonstation reckonstation17 = new Reckonstation();
        addObject(reckonstation17,770,156);
        Reckonstation reckonstation18 = new Reckonstation();
        addObject(reckonstation18,661,61);
        Reckonstation reckonstation19 = new Reckonstation();
        addObject(reckonstation19,446,48);
        Reckonstation reckonstation20 = new Reckonstation();
        addObject(reckonstation20,163,57);
        Asteroid asteroid = new Asteroid();
        addObject(asteroid,164,159);
        Asteroid asteroid2 = new Asteroid();
        addObject(asteroid2,662,362);
    }
    public void act()
    {
    if (number == 0)
        {
     Greenfoot.setWorld(new Level2() );
        } 
    if (number2 == 0)
        {
     Greenfoot.setWorld(new Death() );
        }   
   }
}
public class Level2 extends World
{
    Actor Reckonstation ;
    Actor Rocket;
    int number= getObjects(Reckonstation.class).size();
    int number2= getObjects(Rocket.class).size();
    /**
     * Constructor for objects of class Level2.
     * 
     */
    public Level2()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        prepare();
    }
        private void prepare()
    {
        Rocket rocket = new Rocket();
        addObject(rocket,53,314);
        Reckonstation reckonstation = new Reckonstation();
        addObject(reckonstation,71,547);
        Reckonstation reckonstation2 = new Reckonstation();
        addObject(reckonstation2,221,555);
        Reckonstation reckonstation3 = new Reckonstation();
        addObject(reckonstation3,420,550);
        Reckonstation reckonstation4 = new Reckonstation();
        addObject(reckonstation4,595,559);
        Reckonstation reckonstation5 = new Reckonstation();
        addObject(reckonstation5,760,560);
        Reckonstation reckonstation6 = new Reckonstation();
        addObject(reckonstation6,687,470);
        Reckonstation reckonstation7 = new Reckonstation();
        addObject(reckonstation7,526,466);
        Reckonstation reckonstation8 = new Reckonstation();
        addObject(reckonstation8,345,469);
        Reckonstation reckonstation9 = new Reckonstation();
        addObject(reckonstation9,161,472);
        Reckonstation reckonstation10 = new Reckonstation();
        addObject(reckonstation10,44,397);
        Reckonstation reckonstation11 = new Reckonstation();
        addObject(reckonstation11,258,404);
        Reckonstation reckonstation12 = new Reckonstation();
        addObject(reckonstation12,448,410);
        Reckonstation reckonstation13 = new Reckonstation();
        addObject(reckonstation13,612,403);
        Reckonstation reckonstation14 = new Reckonstation();
        addObject(reckonstation14,772,414);
        Reckonstation reckonstation15 = new Reckonstation();
        addObject(reckonstation15,174,308);
        Reckonstation reckonstation16 = new Reckonstation();
        addObject(reckonstation16,341,321);
        Reckonstation reckonstation17 = new Reckonstation();
        addObject(reckonstation17,541,322);
        Reckonstation reckonstation18 = new Reckonstation();
        addObject(reckonstation18,697,330);
        Reckonstation reckonstation19 = new Reckonstation();
        addObject(reckonstation19,54,217);
        Reckonstation reckonstation20 = new Reckonstation();
        addObject(reckonstation20,288,236);
        Reckonstation reckonstation21 = new Reckonstation();
        addObject(reckonstation21,455,241);
        Reckonstation reckonstation22 = new Reckonstation();
        addObject(reckonstation22,630,242);
        Reckonstation reckonstation23 = new Reckonstation();
        addObject(reckonstation23,779,252);
        Reckonstation reckonstation24 = new Reckonstation();
        addObject(reckonstation24,173,143);
        Reckonstation reckonstation25 = new Reckonstation();
        addObject(reckonstation25,376,154);
        Reckonstation reckonstation26 = new Reckonstation();
        addObject(reckonstation26,537,155);
        Reckonstation reckonstation27 = new Reckonstation();
        addObject(reckonstation27,709,158);
        Reckonstation reckonstation28 = new Reckonstation();
        addObject(reckonstation28,74,58);
        Reckonstation reckonstation29 = new Reckonstation();
        addObject(reckonstation29,316,84);
        Reckonstation reckonstation30 = new Reckonstation();
        addObject(reckonstation30,472,88);
        Reckonstation reckonstation31 = new Reckonstation();
        addObject(reckonstation31,681,91);
        Reckonstation reckonstation32 = new Reckonstation();
        addObject(reckonstation32,783,93);
        Asteroid asteroid = new Asteroid();
        addObject(asteroid,89,137);
        Asteroid asteroid2 = new Asteroid();
        addObject(asteroid2,636,144);
        Asteroid asteroid3 = new Asteroid();
        addObject(asteroid3,431,324);
        Asteroid asteroid4 = new Asteroid();
        addObject(asteroid4,248,506);
    }
    public void act()
    {
    if (number == 0)
        {
     Greenfoot.setWorld(new Level3() );
        }   
    if (number2 == 0)
        {
     Greenfoot.setWorld(new Death() );
        }   
   }
}
public class Level3 extends World
{
    Actor Horia ;
    Actor Rocket;
     int number= getObjects(Horia.class).size();
    int number2= getObjects(Rocket.class).size();
    /**
     * Constructor for objects of class Level3.
     * 
     */
    public Level3()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        prepare();
    }
     private void prepare()
    {
        Rocket rocket = new Rocket();
        addObject(rocket,88,312);
        Horia horia = new Horia();
        addObject(horia,705,309);
        Asteroid asteroid = new Asteroid();
        addObject(asteroid,113,515);
        Asteroid asteroid2 = new Asteroid();
        addObject(asteroid2,391,477);
        Asteroid asteroid3 = new Asteroid();
        addObject(asteroid3,683,532);
        Asteroid asteroid4 = new Asteroid();
        addObject(asteroid4,562,181);
        Asteroid asteroid5 = new Asteroid();
        addObject(asteroid5,134,79);
        Asteroid asteroid6 = new Asteroid();
        addObject(asteroid6,422,71);
        Asteroid asteroid7 = new Asteroid();
        addObject(asteroid7,676,80);
    }
     public void act()
    {
    if (number == 0)
        {
     Greenfoot.setWorld(new Win() );
        }   
    if (number2 == 0)
        {
     Greenfoot.setWorld(new Death() );
        }   
   }
}
public class Death extends World
{
   String[] items = new String[ 6 ];
    boolean eu = false;
    /**
     * Constructor for objects of class Death.
     * 
     */
    public Death()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
    }
    public void act ()
    {
        items[0] = ("Oops! Looks like you died." +
         "\nPress 1 to restart game");
     if (eu == false)
        {
            showText( items[0] , (getWidth()/3), (getHeight()/3) + 110 );
            eu = true;
        }
     if (Greenfoot.isKeyDown("1"))
        {
            Greenfoot.setWorld(new Introduction() );
        }
    }
}
public class Win extends World
{
    String[] items = new String[ 6 ];
    boolean eu = false;
    /**
     * Constructor for objects of class Win.
     * 
     */
    public Win()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
    }
    public void act ()
    {
        items[0] = ("Congratulations!" +
         "\nYou rescued Horia and came back to Earth!" +
         "\nPress 1 to restart game");
     if (eu == false)
        {
            showText( items[0] , (getWidth()/3), (getHeight()/3) + 110 );
            eu = true;
        }
     if (Greenfoot.isKeyDown("1"))
        {
            Greenfoot.setWorld(new Introduction() );
        }
    }
}
Super_Hippo Super_Hippo

2018/1/13

#
A line like this
int number= getObjects(Reckonstation.class).size();
means that it will be checked once and 'number' will be set to 0. And it will stay zero. It does not mean that whenever you use 'number', the method is executed again because it only saved the value. So you have to call the method and you don't need the variable. On the other hand, all these 'items' can be placed in the constructor or directly when creating the string and those strings do not have to be created again all the time. So for example:
String[] items =
{
    String.join(System.getProperty("line.separator"),
    
    "You are the well-known astronaut Serban Jinga.",
    "You were on a mission to find your lost friend Horia.",
    "But your ship hit an asteroid and lost the reckonstations.",
    "Now, you must find them all avoiding the other asteroids.",
    "Press 2 to continue"),
    
    "You can move using ←↑→↓ Press 3 to continue",
    
    "Good luck! Press 4 to start",
    
    "Welcome! Press 1 to begin"
};
danpost danpost

2018/1/13

#
The variables 'number' and 'number2' are set to zero from the get-go. They are assigned their value by way of the 'getObjects/size' calls before any of the actors are placed into the world. Either set their values at the end of the 'prepare' metthod or in the constructor after the call to the 'prepare' method.
You need to login to post a reply.