Question Regarding States

Hello, I have a question regarding States. How do I use everything that is in the Main class in another state or class? For example in main I can do “cam.setLocation” but in another state/class it gives me the error “Symbol not found”. Another example is using [java]setDisplayFps(true);[/java] and [java]setDisplayStatView(true); [/java] in main. It works there fine, but when I put the same line of code in the init section of another state/class, it gives me the error of “Cannot find symbol” What is the general fix for these types of problems? Any help is appreciated. Thanks

In java, you can’t just call instance methods of one object directly from another one. What you can do is pass a reference to that object to your other classes (usually though the constructor or a setter). Then you can call those methods by prefixing them with the instance name.

You are in for a hell of a ride if you aren’t comfortable with java before playing with jmonkey.
It’s comparable to wanting to learn to dive before learning to swim. It can only end up badly :D.
I started with “thinking in java”, a very good book for getting the bases right. It’s available free on the internet.

1 Like

Thanks for the quick reply. I actually have completed Grade 12 Computer Science at my high school and I was never taught what to do. Could you give me an example of how you should implement that code correctly? That would be very help. Thanks!

If you speak german i can suggest java ist auch eine insel, its a free webbook teaching you the basics.

1 Like
@akshaypathak1011 said: Thanks for the quick reply. I actually have completed Grade 12 Computer Science at my high school and I was never taught what to do. Could you give me an example of how you should implement that code correctly? That would be very help. Thanks!

Respectfully, I appreciate that you want to learn. Unfortunately, this is not really a “learn Java” site. There are many better sites for learning Java and the Java basics questions are sort of off topic.

Given the sheer number of people who go instantly from “playing a game” to “I want to write a game” without any prior skills, this forum would quickly be inundated with beginner Java questions. Many of us volunteer our expertise to answer the difficult and/or JME specific questions. These types questions would quickly be drowned out if we allowed these forums to become a more “Java tutorial” oriented site.

1 Like

I understand. Just telling me the line of code to get those to work would be very helpful as I can try and go on from there. Thanks!

@akshaypathak1011 said: I understand. Just telling me the line of code to get those to work would be very helpful as I can try and go on from there. Thanks!

Loopies already told you what you need to do. If that wasn’t enough, your options are to do some Java tutorials until you understand that answer, find a Java beginners forum to ask these questions, or offer contractor rates for us to write the code for you.

It’s not really a “point at a specific line” kind of answer. It’s a “you need to learn how to pass arguments to code and how to read Java code.”

Well, I found out how to do it myself. Just let me know if I am doing it right.

I set a global variable:
[java]private Camera cam;[/java]

Then in the initialize method I did this:
[java]this.cam = this.app.getCamera();[/java]

Am I doing it right? Thanks for your help.

That’s one way to do it, assuming you’ve initialized this.app.

Another way would be to base your appstate class on SimpleAppState–source code available from

Also, or Lemur’s BaseAppState which will likely be moved to core someday in some form:
http://code.google.com/p/jmonkeyplatform-contributions/source/browse/trunk/Lemur/src/com/simsilica/lemur/event/BaseAppState.java