Hello, some basic help

Hello, I’m new here and I’m trying to create a game, however I’ve read a lot of tutorials etc and I thought I knew what I did, but now when I’m trying to create the first game (just a basic start screen witch then takes you into the game) I’m having problem with my code. I’ve made a MainMenuScreen class and now I’m trying to call it in the Main class.

This is the main class code:
[java]
public class Main extends SimpleApplication{
AppStateManager appStateManager = new AppStateManager(); //i define the variables here since I want the to be local, but then I don’t have the variable app witch is defined in the main method, however this seems to be a problem, and should you do it like this or not?
MainMenuScreen mainMenu = new MainMenuScreen(“MainMenu”);

public static void main(String[] args) {
    Main app = new Main();
    app.start();
    mainMenu.stateAttached(appStateManager); //this says: java non-static method cannot be referenced from a static context
    Game game = new Game();//this Game class is nothing wrong with
    game = mainMenu.getGame();// this says: non-static variable mainMenu cannot be refered from a static context (It can't find the variable mainMenu?)
}
//... simpleInitApp(){} ... simpleUpdate(){} ..... simpleRender(){}

}[/java]
And here is the MeainMenuScreen Class:
[java]
//… imports
public class MainMenuScreen extends AbstractAppState implements ScreenController {
private Nifty nifty;
private Screen screen;
private SimpleApplication app;

private ViewPort viewPort;
private Node rootNode;
private Node guiNode;
private AssetManager assetManager;
private Node localRootNode = new Node(“Start Screen RootNode”);
private Node localGuiNode = new Node(“Start Screen GuiNode”);
private final ColorRGBA backgroundColor = ColorRGBA.Gray;
private Game game = new Game();
/** custom methods */

public MainMenuScreen(String data){
/** Your custom constructor, can accept arguments */
this.rootNode = app.getRootNode();
this.viewPort = app.getViewPort();
this.guiNode = app.getGuiNode();
this.assetManager = app.getAssetManager();
}
public Game getGame(){
return game;//this function just returns the variable game
}
//and all other functions, but I don’t think they have anything to do with this
}
[/java]
This is probably a very noob’ish problem or something, but I’ve tried to figuring it out for to long, so I might as well just post it here to get started with the game :slight_smile:
Thanks for any answers that might help :smiley:

Greetings,

At first some hints:

You don’t need to create your own appStateManager as the SimpleApplication has its own.
You shouldn’t name an appState “MainMenuScreen”. A screen is a gui-component containing several other gui components while an appState is a background process which has to do something (like managing bullets, handling the AI, etc.).

Also you should exercise with some Java basics first before starting to develop a game. (what is an object? What is a class? What is an object member and so on). You will need a rock solid understanding of the programming language if you don’t want to waste very much time in bug solving.

The bug you mentioned above is because you try to call an object variable in a static method. Static methods cannot “see” the non-static variables in the same class because they don’t exist there. You would have to make these variables also static ( That’s also why I gave you the hint with practicing Java because this is likely a beginners error).

Regards,

ceiphren

2 Likes

Okay, thank you for your fast respond and yes I’m pretty new to Java.
However I was just following the “Nifty GUI 1.3 - Usecase Scenarios” tutorial and I thought that this was the way you should do it.
But should I even place something in the main method or should I only have the app.start() there, and is there any moments when you need to use the app variable for anything or is it just there to start the game, should i only use the simpleInitApp() method for things that should happen when the program starts(and never use the main method for anything else then start the game)?

Thank you, and I hope I’ll not bother you to much with all these nooby questions^^

hm, I don’t know about the Usecase Scenarios.

The class SimpleApplication has a method simpleInitApp(). This is a good place to start. So derive from that Class (MyAwesomeGameApplication or whatever) and overwrite this method. also for your first games this class is a good place to put stuff like nifty or the gamestates. Later you will try to manage your game components more elegant which will lead you to stuff like the spring framework (or similar). But for a start this will do.

And don’t forget to practice Java. game programming is a hell of work and I saw too many giving up because they were just bad prepared.

1 Like

Okay, again thank you for the fast answer, I’ll try it out!

By the way got any good way/site to practice Java?
Since I’ve not done that much writing by my own, most watching videos on everything and done some pretty easy tasks in my school.

1 Like

Try this one: http://www.javabeginner.com/

It’s very extensive. Also try to write small programs like a calculator or an address book. It will help you getting skilled with Java and you will understand faster why thinks are done in JME in the way they are.

2 Likes

I’ve now come a little bit further, and I’m able to see the nifty screen.
However I think I’ve made everything as the tutorials told me, but it still don’t work to call the functions by clicking the buttons. So if you could help me a little I would love it ^^.

This is my codes:
Here is the main method calling everything (the app):
[java]
public class Main extends SimpleApplication{
private Game game = new Game();
private NiftyJmeDisplay niftyDisplay;
private Nifty nifty;
private Screen screen;
private StartScreenAppState startScreenAppState;

public static void main(String[] args){
    Main app = new Main();
    app.setPauseOnLostFocus(true);
    app.start();
}
@Override
public void simpleInitApp(){
    niftyDisplay =  new NiftyJmeDisplay(assetManager,inputManager,audioRenderer,guiViewPort);
    startScreenAppState = new StartScreenAppState(stateManager);
    nifty = niftyDisplay.getNifty();
    ShowMainMenu();
    startScreenAppState.bind(nifty, screen);
    stateManager.attach(startScreenAppState);
    startScreenAppState.stateAttached(stateManager);
}
public void ShowMainMenu(){
    nifty.fromXml("Interface/xmls/MainMenuScreen.xml", "start");
    guiViewPort.addProcessor(niftyDisplay);
    flyCam.setDragToRotate(true);
}
[/java] 

Here is the StartScreenAppState:
[java]
//imports…
public class StartScreenAppState extends AbstractAppState implements ScreenController {
private Nifty niftyMainScreen;
private NiftyJmeDisplay niftyDisplay;
private Screen screen;
private SimpleApplication app;

private ViewPort viewPort;
private Node rootNode;
private Node guiNode;
private Node localRootNode = new Node(“Start Screen RootNode”);
private Node localGuiNode = new Node(“Start Screen GuiNode”);
private final ColorRGBA backgroundColor = ColorRGBA.Gray;
private AssetManager assetManager;
private Game game = new Game();

public StartScreenAppState(AppStateManager stateManager){
/** Your custom constructor, can accept arguments */
this.app = (SimpleApplication) stateManager.getApplication();
this.rootNode = app.getRootNode();
this.viewPort = app.getViewPort();
this.guiNode = app.getGuiNode();
this.assetManager = app.getAssetManager();
}
public void bind(Nifty nifty, Screen screen) {
this.niftyMainScreen = nifty;
this.screen = screen;
}
public void initialize(AppStateManager stateManager, Application app) {
super.initialize(stateManager, app);
this.app = (SimpleApplication) app;//just gives this object the app variable
}
public void startGame(){
niftyMainScreen.gotoScreen(“IngameUi”); // switch to another screen
}
public void quitGame() {//witch of these should I use to quit the application?
app.stop();
niftyMainScreen.exit();
}
public void stateAttached(AppStateManager stateManager) {
rootNode.attachChild(localRootNode);
guiNode.attachChild(localGuiNode);
viewPort.setBackgroundColor(backgroundColor);
}[/java]
And then here is the MainMenuScreen xml:
[java]
<screen id=“start” controller=“mygame.StartScreenAppState”>
<!-- All the different panels etc–>
<control name=“button” label=“Start” id=“StartButton” visibleToMouse=“true” >
<interact onClick=“startGame()” />
</control>
<control name=“button” label=“Quit” id=“QuitButton” visibleToMouse=“true”>
<interact onClick=“quitGame()” />
</control>
<!-- All the different panels etc–>
</screen>[/java]
Sorry for bothering you with this probably easy problem, but I really can’t find a way around it and if it’s a easy problem it seems kind of stupid of me trying to do it for hours instead of just ask here :slight_smile:
Thank you for reading and all help is appreciated!

//Ranami

You are calling bind yourself. Instead register your screen controller with nifty and then nifty will call bind for you…

1 Like

i.e, [java]nifty.fromXml(“Interface/xmls/MainMenuScreen.xml”, “start”, startScreenAppState);[/java]

1 Like

Thank you really much for the fast answer, this did the trick, didn’t expect it to be that easy ^^ Now I can get started with the game ^^