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

2017/12/13

How do I make my own MyWorld constructor

jkfrmsttfrm jkfrmsttfrm

2017/12/13

#
I am using this method to reset the world:
public void nextLevel(int thisLevel){
        int nextLevel = thisLevel + 1;
        Greenfoot.setWorld(new MyWorld());
    }
I need a way to make the world constructor take an int so I can set the level number. When I try:
public MyWorld(int level){

}
I get an error that says: "no suitable constructor found for World(no arguments) constructor greenfoot.World.World(int, int, int) is not applicable (actual and formal argument lists differ in length) constructor greenfoot.World.World(int, int, int, boolean) is not applicable (actual and formal argument lists differ in length)"
danpost danpost

2017/12/13

#
Move the code in your current constructor into your new constructor and replace your old no-parameter constructor with the following:
public MyWorld()
{
    this(0);
}
You can then work with the parameter value within the new constructor.
You need to login to post a reply.