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

2018/4/11

What does this error mean?

1
2
Navique Navique

2018/4/11

#
The world could not be constructed. The world subclass may not have a public constructor without parameters, or may not be public. All my world subclasses are public, and there are no errors in the code there.
danpost danpost

2018/4/11

#
Navique wrote...
The world could not be constructed. The world subclass may not have a public constructor without parameters, or may not be public. All my world subclasses are public, and there are no errors in the code there.
Show the class code to the initial world.
Navique Navique

2018/4/11

#
danpost wrote...
Navique wrote...
The world could not be constructed. The world subclass may not have a public constructor without parameters, or may not be public. All my world subclasses are public, and there are no errors in the code there.
Show the class code to the initial world.
If you have any questions about certain things, I can give the class code and purpose.
public CalamityBeginning(int score)
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        prepare();
        counter counter = getCounter();
        theCounter = new counter();
        addObject(theCounter, 203, 104);
        theCounter.setCount(score);
    }

    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        Button button = new Button();
        addObject (button,308,170);
        button.setLocation(305,180);
        
        
        
        CreditsButton creditsbutton = new CreditsButton();
        addObject(creditsbutton,305,280);
        creditsbutton.setLocation(305,250);
    }
    public counter getCounter()
        {
            return theCounter;
        }
danpost danpost

2018/4/11

#
Your world constructor has a parameter (line 1). "The world could not be constructed. The world subclass may not have a public constructor without parameters, or may not be public." Add the following to the class:
public CalamityBeginning()
{
    this(0); // calls your line 1 above
}
to give the class a constructor without a parameter.
Navique Navique

2018/4/11

#
danpost wrote...
Your world constructor has a parameter (line 1). "The world could not be constructed. The world subclass may not have a public constructor without parameters, or may not be public." Add the following to the class:
public CalamityBeginning()
{
    this(0); // calls your line 1 above
}
to give the class a constructor without a parameter.
I've done this, I've been trying to implement a feature to pass on the score between all screens, so that it may be used as currency, or the like. When I switch to another screen and make it go back to CalamityBeginning, it crashes.
danpost danpost

2018/4/11

#
Navique wrote...
I've done this, I've been trying to implement a feature to pass on the score between all screens, so that it may be used as currency, or the like. When I switch to another screen and make it go back to CalamityBeginning, it crashes.
Please show entire class code of your revised CalamityBeginning class.
Navique Navique

2018/4/11

#
danpost wrote...
Navique wrote...
I've done this, I've been trying to implement a feature to pass on the score between all screens, so that it may be used as currency, or the like. When I switch to another screen and make it go back to CalamityBeginning, it crashes.
Please show entire class code of your revised CalamityBeginning class.
Here is the code for CalamityBeginning.
public class CalamityBeginning extends World
{
    private counter theCounter;
    /**
     * Constructor for objects of class CalamityBeginning.
     * 
     */
    public CalamityBeginning(int score)
    {    
         
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(600, 400, 1); 
        
        prepare();
        counter counter = getCounter();
        theCounter = new counter();
        addObject(theCounter, 203, 104);
        theCounter.setCount(score);
        
    }
    public CalamityBeginning()
    {
        this(0); // calls your line 1 above
    }
    /**
     * Prepare the world for the start of the program.
     * That is: create the initial objects and add them to the world.
     */
    private void prepare()
    {
        Button button = new Button();
        addObject (button,308,170);
        button.setLocation(305,180);
        
        
        
        CreditsButton creditsbutton = new CreditsButton();
        addObject(creditsbutton,305,280);
        creditsbutton.setLocation(305,250);
    }
    public counter getCounter()
        {
            return theCounter;
        }
}
I'll also give the code for the button which opens CalamityBeginnng again.
public class Replay extends Actor
{
    /**
     * Act - do whatever the Replay wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        if (Greenfoot.mouseClicked(this)) {
           World world;
           world = getWorld();
           CalamityBeginning calWorld = (CalamityBeginning) getWorld();
           counter counter = calWorld.getCounter();
           Greenfoot.setWorld(new CalamityBeginning(counter.returnCount()));
        }    
        
    }
}
danpost danpost

2018/4/11

#
Show the counter class also, please.
Navique Navique

2018/4/11

#
danpost wrote...
Show the counter class also, please.
public class counter extends Actor
{
    /**
     * Act - do whatever the counter wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
     
    public void act() 
    {
        
        
    }   
    public int totalCount = 0;
    public void bumpCount(int amount)
    {
     totalCount += amount;
     setImage(new GreenfootImage(""+ totalCount, 30, Color.WHITE, Color.BLACK));
    }
    public int returnCount(){
        return totalCount;
    }
    public void setCount(int amount)
    {
     setImage(new GreenfootImage(""+ amount, 30, Color.WHITE, Color.BLACK));
    }
}
here it is
danpost danpost

2018/4/11

#
Nothing jumps out at me. Post what error message you are getting (copy/paste entire output).
danpost danpost

2018/4/11

#
Found something. In the counter class, your setCount method needs the following for its first line:
totalCount = amount;
However, as an alternative, you can remove the setCount method from the counter class and change "setCount" at line 18 in your CalamityBeginning class to "bumpCount".
Navique Navique

2018/4/11

#
danpost wrote...
Found something. In the counter class, your setCount method needs the following for its first line:
totalCount = amount;
However, as an alternative, you can remove the setCount method from the counter class and change "setCount" at line 18 in your CalamityBeginning class to "bumpCount".
Here is the error, in Replay. ava.lang.ClassCastException: Gameover cannot be cast to CalamityBeginning at Replay.act(Replay.java:20) at greenfoot.core.Simulation.actActor(Simulation.java:604) at greenfoot.core.Simulation.runOneLoop(Simulation.java:562) at greenfoot.core.Simulation.runContent(Simulation.java:221) at greenfoot.core.Simulation.run(Simulation.java:211) I've also gone with the second option.
danpost danpost

2018/4/11

#
You lost all connection to the CalamityBeginning class when you went to your Gameover world. So the score cannot be recovered. Show your Gameover class codes.
Navique Navique

2018/4/11

#
here it is, keep in mind CalamityBeginning is also a world.
public class Gameover extends World
{
    private counter theCounter;
    private Replay theButton;
    /**
     * Constructor for objects of class Gameover.
     * 
     */
    public Gameover(int score)
    {    
        
        super(600, 400, 1); 
        prepare();
        
        counter counter = getCounter();
        theCounter = new counter();
        addObject(theCounter, 203, 104);
        theCounter.bumpCount(score);
        
        theButton = new Replay();
        addObject(theButton, 290, 225);
        
        
    }
    public void prepare ()
    {

    }
     
     public counter getCounter()
        {
            return theCounter;
        }
    
}
danpost danpost

2018/4/11

#
Okay. Change your Replay codes (what is shown above) to this:
public class Replay extends Actor
{
    public void act()
    {
        if (Greenfoot.mouseClicked(this)) {
           Gameover goWorld = (Gameover) getWorld();
           counter counter = goWorld .getCounter();
           Greenfoot.setWorld(new CalamityBeginning(counter.returnCount()));
        }
    }
}
There are more replies on the next page.
1
2