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

2013/1/11

call a method only once

Paddy-Boom Paddy-Boom

2013/1/11

#
I want an image to only be added once to the world, but if the run button is clicked the method is called over and over again. How can i add an object only once?
danpost danpost

2013/1/11

#
If you want your scenario to start with the image, then move the code that adds the image to the world constructor. Example 'MyWorld' world:
import greenfoot.*;
// possible other imports

public class MyWorld extends World
{
    // possible field declarations

    public MyWorld() // constuctor header
    {
        super(600, 400, 1);
        // place code to add image here
        // possible other initial set-up code
    }

    // any methods you may have
}
Paddy-Boom Paddy-Boom

2013/1/11

#
I want the image to be added if the gameOver() method is called.. As another solution i created an GameOver World which worked out perfectly, the only problem is, that i can't get the value from the counter i added in the Level-World I uploaded the scenario please have look.
danpost danpost

2013/1/11

#
In your GameOver world code, instead of 'public GameOver()', use 'public GameOver(int finalScore)'; and after creating the Counter object in the GameOver world, use 'the2counter.setValue(finalScore);'. Finally, create the new world from within Level with 'Greenfoot.setWorld(new GameOver(theCounter.getValue());'.
Paddy-Boom Paddy-Boom

2013/1/11

#
it works! thanks a lot :)
You need to login to post a reply.