I just started JME3 to learn after many many years. And I know only Unity Godot.
I just love Java and JME3. I was running code and stuck in this error. Help and suggest me what to read to improve my JME3 code and skills.
package mygame;
import com.jme3.app.SimpleApplication;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.renderer.RenderManager;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;
import com.jme3.scene.Node;
import com.jme3.asset.AssetManager;
import com.jme3.scene.Spatial;
import com.jme3.math.Vector3f;
import com.jme3.light.DirectionalLight;
import com.jme3.input.controls.ActionListener;
import com.jme3.input.controls.AnalogListener;
import com.jme3.input.MouseInput;
import com.jme3.input.KeyInput;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.input.controls.MouseButtonTrigger;
/**
* This is the Main Class of your Game. You should only do initialization here.
* Move your Logic into AppStates or Controls
* @author normenhansen
*/
public class Main extends SimpleApplication {
Box b=new Box(1,1,1);
Geometry geom =new Geometry("Box",b);
AssetManager asm=this.getAssetManager(); // Still trying to get rid of null error so, i'm using it as global
private Material geoMat;
int count;
float value=0.5f;
private boolean isRunning=true;
public static void main(String[] args) {
Main app = new Main();
app.start();
}
@Override
public void simpleInitApp() {
count=1011;
Material mat = new Material(assetManager,"Common/MatDefs/Misc/Unshaded.j3md");
mat.setColor("Color", ColorRGBA.Red);
geom.setMaterial(mat);
Box b2=new Box(1,1,1);
Geometry geom2=new Geometry("Box2", b2);
Material mat2=new Material(assetManager,"Common/MatDefs/Misc/Unshaded.j3md");
mat2.setColor("Color", ColorRGBA.Green);
geom2.setMaterial(mat2);
Node n=new Node();
rootNode.attachChild(n);
Spatial terrain=assetManager.loadModel("Scenes/Terrain.j3o");
geom2.setLocalTranslation(new Vector3f(0,-2,0));
n.attachChild(geom);
DirectionalLight sun = new DirectionalLight();
sun.setDirection(new Vector3f(-0.1f, -0.7f, -1.0f).normalizeLocal());
n.addLight(sun);
terrain.setLocalTranslation(new Vector3f(0,-3,0));
n.attachChild(terrain);
InitKeys();
}
public void RotateCube( ){
this.geoMat=new Material(assetManager,"Common/MatDefs/Misc/Unshaded.j3md");
this.geoMat.setColor(("Color"), ColorRGBA.Blue);
geom.setMaterial(this.geoMat);
Node n2=new Node();
rootNode.attachChild(n2);
n2.attachChild( geom);
geom.setLocalTranslation(3,0,0);
geom.rotate(.2f, 0, 0);
value=value+0.5f;
System.out.println("This is working roate X Val: "+ geom.getLocalTranslation().x);
}
@Override
public void simpleUpdate(float tpf) {
//TODO: add update code
}
@Override
public void simpleRender(RenderManager rm) {
//TODO: add render code
}
void InitKeys(){
inputManager.addMapping("Pause", new KeyTrigger(KeyInput.KEY_P));
inputManager.addMapping("Left", new KeyTrigger(KeyInput.KEY_J));
inputManager.addMapping("Right", new KeyTrigger(KeyInput.KEY_K));
inputManager.addMapping("Rotate", new KeyTrigger(KeyInput.KEY_SPACE),
new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
MyCombinedListener mc=new MyCombinedListener();
// inputManager.addListener(mc, "Pause", "Left", "Right", "Rotate" );
inputManager.addListener(actionListener, "Pause");
inputManager.addListener(analogListener, "Left", "Right", "Rotate");
}
final private ActionListener actionListener = new ActionListener() {
@Override
public void onAction(String name, boolean keyPressed, float tpf) {
if (name.equals("Pause") && !keyPressed) {
isRunning = !isRunning;
}
}
};
final private AnalogListener analogListener = new AnalogListener() {
@Override
public void onAnalog(String name, float value, float tpf) {
if (isRunning) {
Main m=new Main();
if (name.equals("Rotate")) {
m. RotateCube();
System.out.println("Counter is: "+m.count);
}
if (name.equals("Right")) {
geom.move((new Vector3f(value, 0,0)) );
}
if (name.equals("Left")) {
geom.move(new Vector3f(-value, 0,0));
}
} else {
System.out.println("Press P to unpause.");
}
}
};
}
Uncaught exception thrown in Thread[jME3 Main,5,main]
NullPointerException: Cannot invoke “com.jme3.asset.AssetManager.loadAsset(com.jme3.asset.AssetKey)” because “contentMan” is null
Help me learn how to use AssetManager outside simpleInitApp(), When I create material in other functions. it says “contentMan” is null