Hi.
I have a class called Engine that extends SimpleApplication and handles most of the game. I also have a controller for the Nifty gui that extends Engine and implements ScreenController. Now my problem is that I need to call methods in Engine in order to affect the game through the gui, and by extension the controller.
I can’t seem to figure out how I am supposed to get a reference to one class from the other. I read somewhere that I should extend the class that extended SimpleApplication but that doesn’t seem to resolve anything. As far as I can see, the application and controller have no relationship and I don’t know how the innards of Nifty works.
Isn’t there some way I can call Engine’s methods from the controller?
I’m hopelessly stuck and have been for hours. Please help
You need to either pass an object of the desired type and save it as a local variable in the other class or create a static accessor:
[snippet id=“12”]
Hi normen.
I didn’t find the original post when I was searching the forum and wiki for an answer - sorry about that.
Well your post completely solves my problem. Thanks a lot
I ended up with the following code:
[java]
private static Engine staticApplication;
public Engine(){ }
public static Engine getApplication(){
if(staticApplication == null)
staticApplication = new Engine();
return staticApplication;
}
public Engine(Main m) {
this.m = m;
staticApplication = this;
}
[/java]
Here’s how I did it (I think it’s an ok way…)
Just declare the Main class (Engine in your case) as a class variable, and make sure the controller class is in the same package. e.g.:
[java]
public class Main extends SimpleApplication {
static Main app;
// main() method, controllerClick() method, etc.
}
public class ScreenControllerRotate implements ScreenController {
public void onClick(){
Main.app.controllerClick();
}
//code etc.
}
[/java]
I’m a noob programmer though so let me know if I have some bad practices in this ‘kludge’.