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

2021/2/5

why i cant make new levels with super()

Vitalka Vitalka

2021/2/5

#
Hello, I want to make two levels that extending from class level with a method that restarts the level. This class level extends from the class world. But why I get the error constructor Level in class Level cannot be applied to given types. Also it marks super inside of the classes Level 1 and Level 2. }
public class Level extends World
{
    public Level()
    {    
        super(10000, 600, 1); 
    }

    public void LevelNeustarten(){}
}
public class Level1 extends Level
{
    public Level1()
    {    
        super(10000, 600, 1); 
        prepare();
        addObject(Bild.figur,630,493);
    }

    private void prepare()
    {
        Stein stein = new Stein();
        addObject(stein, 1231, 482);
    }

    public void LevelNeustarten()
    {
        Greenfoot.setWorld(new Level1());
    }
public class Level2 extends Level
{
    public Level2()
    {    
        super(10000, 600, 1); 
        prepare();
        addObject(Bild.figur,630,493);
    }

    private void prepare()
    {

    }

    public void LevelNeustarten()
    {
        Greenfoot.setWorld(new Level2());
    }
}
danpost danpost

2021/2/5

#
In Level1 and Level2 classes, change line 5 (in both) to:
super();
Btw, 10000 is HUGE. 1000 is even a bit large. I usually limit the width of my worlds to 800.
Vitalka Vitalka

2021/2/5

#
it worked but if I play my game and die, I get an error. I seared on the internet for solutions and found this code , which worked for me even if I died:
public class Level extends World
{
    public Level()
    {    
        super(10000, 600, 1); 
    }
    
    public Level(int B, int H, int c) 
    { 
        this(B, H, c, true); 
    } 

    public Level(int B, int H, int c, boolean b) 
    {
        super(B, H, c, b); 
    }

    public void LevelNeustarten(){}
}
unfortunately I don't understand what the code does xD. Could someone explain this please because I need to explain my code in school.
danpost danpost

2021/2/5

#
Vitalka wrote...
it worked but if I play my game and die, I get an error.
What (exactly) was the error? What is the coding around which the error occurred.
I seared on the internet for solutions and found this code , which worked for me even if I died: << Code Omitted >> unfortunately I don't understand what the code does xD. Could someone explain this please because I need to explain my code in school.
This class is intended to be a parent class of your game worlds. It contains 3 constructors (beginning on lines 3, 8 and 13) and a method (to be overridden in some or all of its subclasses. The 3 constructors are to allow your game worlds multiple ways to define your world. Using the line I gave above calls the line 3 constructor. Your Level1 (or Level2) line 5 calls the line 8 constructor in your Level class, which redirects (using "this") to the line 13 constructor. If a game world requires an unbounded state, it can call the line 13 constructor with a fourth argument being false.
You need to login to post a reply.