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

2014/12/13

Add general functions to World()

Hey, I want to create a class for buttons sending a signal to the current world they're spawned in. The constructor of the button looks like this:
public Button(int buttonId, World world) {
    id = buttonId;
    currentWorld = world;
}
And the function which handles the click looks like this:
public void click() {
        currentWorld.buttonClicked(1);
}
This is how the button is created:
addObject(new Button(0, this), 100, 200);
And this is the function buttonClicked:
public void buttonClicked(int buttonId) {
        switch(buttonId) {
            case 0:     //Spiel stumm schalten
                addObject(new Text("*Spiel stumm*"), 1000, 200);
        }
    }
Or would it be better to check wether a button is pressed or not in the current world and not in the class button? thanks for your help, Nicotinamidadenindinukleotid
Ahh I fixed it. Here's the solution: Constructor:
 public Button(int buttonId, Menu world) {
        id = buttonId;
        currentMenu = world;
    }
and click:
public void click() {
        currentMenu.buttonClicked(0);
    }
You need to login to post a reply.