Hello, I have recently downloaded jMonkeyEngine and I have stumbled into a slight problem that I cannot solve. My player class seems to cause it when I attach it to the rootnode, it is probably a rookie mistake and I would appreciate some help. Here is the code for the 2 classes starting with my player class.
player.java
[java]
package com.destroyerGames.aberration.characters;
import com.jme3.app.SimpleApplication;
import com.jme3.bullet.control.CharacterControl;
import com.jme3.input.;
import com.jme3.input.controls.;
import com.jme3.math.Vector3f;
import com.jme3.scene.CameraNode;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
/**
@author Destroyer Games
/
public class Player {
/*
creates instances of classes to use in the code.
*/
private FlyByCamera flyCam;
private CharacterControl player;
private InputManager inputManager;
private SimpleApplication application;
private Node rootNode;
private CameraNode camNode;
/**
The states of the player. Defined by strings, and represented to the game
as decimal data values.
*/
private int state;
private static final int STATE_ALIVE = 0;
private static final int STATE_DEAD = 1;
private static final int STATE_ATTACKING = 2;
private static final int STATE_PUSHED = 3;
private static final int STATE_ATTACKED = 4;
private static final int STATE_SWINGING_SWORD = 5;
private static final int STATE_FIRING_SEMIAUTOMATIC_GUN = 6;
private static final int STATE_FIRING_AUTOMATIC_GUN = 7;
private static final int STATE_FIRING_BOW = 8;
private static final int STATE_RELOADING = 9;
private static final int STATE_WALKING = 10;
/**
An idTag is a special identifier for a class that specifies which package
The UpdateManager will put the class in. In this case the
com.destroyerGames.aberration.characters' special idTag is the number 2.
*/
public static final int idTag = 2;
/**
The actions that the player will take. To be mapped with a certain key.
*/
public boolean Left = false, Right = false, Forward = false,
Backward = false, Jump = false, Attack = false, Block = false,
Sneak = false, Action = false, Reload = false;
/**
Strings
*/
public String name;
/**
Makes it compatible with SimpleApplication
*/
public Player(SimpleApplication application, String name) {
this.application = application;
}
/**
Pulls everything together
*/
public void initPlayer() {
fpCamera();
receiveDigitalInput();
receiveAnalogInput();
}
/**
Configures the player
*/
public void playerConfig() {
player.setUpAxis(1);
player.setMaxSlope(2.0f);
player.setGravity(2.5f);
}
/**
Set up the first-person camera
*/
public void fpCamera() {
flyCam.setEnabled(true);
flyCam.setMoveSpeed(2);
application.getRootNode().attachChild(camNode);
}
/**
Set up the digital, on button-press, actions
*/
public void receiveDigitalInput() {
inputManager.addMapping("Jump", new KeyTrigger(KeyInput.KEY_SPACE));
inputManager.addMapping("Action", new KeyTrigger(KeyInput.KEY_E));
inputManager.addMapping("Reload", new KeyTrigger(KeyInput.KEY_R));
inputManager.addListener(action, new String[] {"Jump"});
inputManager.addListener(action, new String[] {"Action"});
inputManager.addListener(action, new String[] {"Reload"});
}
/**
Receives the analog, held down button, actions
*/
public void receiveAnalogInput() {
inputManager.addMapping("Forward", new KeyTrigger(KeyInput.KEY_W));
inputManager.addMapping("Backward", new KeyTrigger(KeyInput.KEY_S));
inputManager.addMapping("Left", new KeyTrigger(KeyInput.KEY_A));
inputManager.addMapping("Right", new KeyTrigger(KeyInput.KEY_D));
inputManager.addMapping("AutoFire", new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
inputManager.addMapping("Aim", new MouseButtonTrigger(MouseInput.BUTTON_RIGHT));
inputManager.addListener(analog, new String[] {"Forward"});
inputManager.addListener(analog, new String[] {"Backward"});
inputManager.addListener(analog, new String[] {"Left"});
inputManager.addListener(analog, new String[] {"Right"});
inputManager.addListener(analog, new String[] {"AutoFire"});
inputManager.addListener(analog, new String[] {"Aim"});
}
private ActionListener action = new ActionListener() {
public void onAction(String name, boolean isPressed, float tpf) {
if (name.equals("Reload")) {
state = STATE_RELOADING;
}
}
};
private AnalogListener analog = new AnalogListener() {
public void onAnalog(String name, float value, float tpf) {
if (name.equals("Forward")) {
Forward = true;
player.setWalkDirection(new Vector3f(0f,0f,-2f));
state = STATE_WALKING;
}
else {
Forward = false;
player.setWalkDirection(new Vector3f(0f,0f,0f));
}
if (name.equals("Backward")) {
Backward = true;
player.setWalkDirection(new Vector3f(0f,0f,2f));
state = STATE_WALKING;
}
else {
Backward = false;
player.setWalkDirection(new Vector3f(0f,0f,0f));
}
if (name.equals("Left")) {
Left = true;
player.setWalkDirection(new Vector3f(-2f, 0f, 0f));
state = STATE_WALKING;
}
else {
Left = false;
player.setWalkDirection(new Vector3f(0f, 0f, 0f));
}
if (name.equals("Right")) {
Right = true;
player.setWalkDirection(new Vector3f(2f, 0f, 0f));
state = STATE_WALKING;
}
else {
Right = false;
player.setWalkDirection(new Vector3f(0f, 0f, 0f));
}
if (name.equals("Forward") && name.equals("Left")) {
player.setWalkDirection(new Vector3f (-2f, 0f, -2f));
state = STATE_WALKING;
}
else {
player.setWalkDirection(new Vector3f(0f, 0f, 0f));
}
if (name.equals("Forward") && name.equals("Right")) {
player.setWalkDirection(new Vector3f(2f, 0f, -2f));
state = STATE_WALKING;
}
else {
player.setWalkDirection(new Vector3f(0f, 0f, 0f));
}
if (name.equals("Backward") && name.equals("Left")) {
player.setWalkDirection(new Vector3f(-2f, 0f, 2f));
state = STATE_WALKING;
}
else {
player.setWalkDirection(new Vector3f(0f, 0f, 0f));
}
if (name.equals("Backward") && name.equals("Right")) {
player.setWalkDirection(new Vector3f(2f, 0f, 2f));
state = STATE_WALKING;
}
}
};
}
[/java]
Here is the main class:
main.java
[java]
package com.destroyerGames.aberration;
import com.jme3.app.SimpleApplication;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;
import com.destroyerGames.aberration.graphics.OpenWorldLoader;
import com.jme3.scene.Spatial;
import com.destroyerGames.aberration.characters.Player;
import com.jme3.scene.CameraNode;
public class Main extends SimpleApplication {
private OpenWorldLoader openWorld;
private Player player;
public static void main(String[] args) {
Main app = new Main();
app.start();
}
@Override
public void simpleInitApp() {
Spatial scene = assetManager.loadModel("Scenes/somePlace.j3o");
player.initPlayer();
rootNode.attachChild(scene);
}
@Override
public void simpleUpdate(float tpf) {
//TODO: add update code
}
@Override
public void simpleRender(RenderManager rm) {
//TODO: add render code
}
}
[/java]
The following code gives me this stack trace:
SEVERE: Uncaught exception thrown in Thread[LWJGL Renderer Thread,5,main]
java.lang.NullPointerException
at com.destroyerGames.aberration.Main.simpleInitApp(Main.java:29)
at com.jme3.app.SimpleApplication.initialize(SimpleApplication.java:225)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.initInThread(LwjglAbstractDisplay.java:130)
at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:207)
at java.lang.Thread.run(Thread.java:722)
again help would be appreciated and sorry if my programming is hard to read. I am also sorry if this exact question has been asked before. Thanks in advance. Also, are there any books out there to help people with development in jMonkeyEngine to help me avoid problems of this nature again.