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

2014/12/14

Constructors not working right way

berdie berdie

2014/12/14

#
I tried to make a constructor in a Superclass but it outputs the system.out.print i put in it but won't create the new object in the World World code:
 public game1()
    {    
        // Create a new world with 600x400 cells with a cell size of 1x1 pixels.
        super(900,900, 1); 

        prepare();
    }

    /**
     * Prepare the world for the start of the program. That is: create the initial
     * objects and add them to the world.
     */
    private void prepare()
    {
        MainBoat b1 = new MainBoat();
        addObject(b1, 300, 57);
        MainBoat b2 = new MainBoat(20,20);
        addObject(b2, 60, 300);
        MainBoat b3 = new MainBoat(220,20);
        addObject(b3, 60, 300);
} 
and the code in the Superclass
 public MainBoat() {
        size = 10;
        setImage("images/Boatbig.png");
            
        System.out.println("1");
    } 
    public MainBoat(int newSize, int loadtime) {
        size = newSize;
        
        loadingTime = loadtime;
        if (size == 20){
            setImage("images/Boatmedium.png");
            System.out.println("2");
        }
        else {
            setImage("images/Boatsmall.png");
            System.out.println("3");
        }
    }
Anyone have any ideas on how to solve it that i will get the object (including the image) on the world at that location
ds06 ds06

2014/12/14

#
try
setImage("Boatbig.png");
maybe the objects are created but you cant see them because they have no picture.. just a guess
berdie berdie

2014/12/14

#
That´s not the problem :S
danpost danpost

2014/12/14

#
berdie wrote...
That´s not the problem :S
I think that it is indeed the problem (although, it could possibly be something else. Remove 'images/' from the filenames in the 'setImage' lines (unless you have a folder called 'images' inside the 'images' folder in your project directory. On the other hand, you did not mention any exception throws or error messages which leads me to believe that either you are not being very helpful in describing your problems or something else is creating the problem to begin with.
You need to login to post a reply.