I am working on a 3d fps, and before I end up like last time with one class and 1500+ lines of code I would like to learn how to instantiate outer classes that load spatials, create animations etc. into the main class in which the game is run. I have just started by trying to add the level to the scene from another outer class, but i keep getting a null pointer exception.
package ClassFiles;
import com.jme3.light.AmbientLight;
import com.jme3.math.ColorRGBA;
public class MainThread extends Maps{
public static void main(String[] args) {
MainThread thread = new MainThread();
thread.start();
}
@Override
public void simpleInitApp() {
Maps object = new Maps();
object.loadMaps();
AmbientLight al = new AmbientLight();
al.setColor(ColorRGBA.Blue);
rootNode.addLight(al);
}
}
package ClassFiles;
import com.jme3.app.SimpleApplication;
import com.jme3.scene.Spatial;
public class Maps extends SimpleApplication {
Spatial map;
public Maps() {
}
public void loadMaps() {
map = assetManager.loadModel("dgame/maps/main.scene");
map.setLocalScale(2f);
rootNode.attachChild(map);
}
@Override
public void simpleInitApp() {
}
}
Any help would be great! :)
normen
June 28, 2011, 11:29pm
2
1 Like
you can have a look at other peopleās source code for guidance. Sometimes people post it in these forums: http://hub.jmonkeyengine.org/groups/user-code-projects/forum/ and http://hub.jmonkeyengine.org/groups/free-announcements/forum/ . There is also a JME project created by the very own JME developers. It is called āMonkeyZoneā and full source is available, the wiki is here: https://wiki.jmonkeyengine.org/legacy/doku.php/jme3:advanced:monkey_zone .
i found your problem :
Do this :
MainThread extends SimpleApplication
remove inheritance from Maps. IT NOW WORKS !!!
Advice : Dont use inheritance. All code can be written with delegate design pattern instead of inheritance. E.g āHas a variableā instead of āisA variableā. Use inheritance only once every 1000 years when the stars align and subclass should inherit 100% of all methods/variables/behavior.
Why does a subclass initialize a superclass ?
@ tralala
i understand why you want me to extends SimplaApplication, but removing the āMaps object = new Maps();ā did not make it work.
Ops you are right, you also need to make main class āsingletonā : I renamed that class to scene.
[java]
import com.jme3.app.SimpleApplication;
import com.jme3.light.AmbientLight;
import com.jme3.math.ColorRGBA;
public class Scene extends SimpleApplication
{
private static Scene scene ;
public static void main(String[] args)
{
Scene scene = Scene.get();
scene.start();
}
private Scene()
{
}
public static Scene get()
{
if (scene == null) scene=new Scene();
return scene;
}
@Override
public void simpleInitApp()
{
Maps object = new Maps();
object.loadMaps();
AmbientLight al = new AmbientLight();
al.setColor(ColorRGBA.Blue);
rootNode.addLight(al);
}
}[/java]
[java]
import com.jme3.app.SimpleApplication;
import com.jme3.scene.Spatial;
public class Maps {
Spatial map;
public Maps() {
}
public void loadMaps() {
Scene scene = Scene.get();
map = scene.getAssetManager().loadModel(ādgame/maps/main.sceneā);
map.setLocalScale(2f);
scene.getRootNode().attachChild(map);
}
public void simpleInitApp() {
}
}[/java]
1 Like
Thanks soooo much! it worked perfectly. It was written very well too, I understand every line of it. Thanks again man.
Looking back at this topic embarrasses me lol. I was such a n00b way in over my head By the way I have a much stronger grasp on OOP