Extends problem

Hello all,



I am not an expert in object oriented programming and therefore i’ve want your help. I created a class “characterGame” wich inherits attributes of the class game. Like this :



characterGame → game → paramGame → initaliseGame. In the paramGame inputManager is declared as protected and now when i’ve compilate this lines it give me and error.







[java]package mygame;



import com.jme3.bullet.collision.shapes.CapsuleCollisionShape;

import com.jme3.bullet.nodes.PhysicsCharacterNode;

import com.jme3.input.KeyInput;

import com.jme3.input.controls.ActionListener;

import com.jme3.input.controls.KeyTrigger;

import com.jme3.math.Vector3f;

import com.jme3.scene.Spatial;



public class characterGame extends game implements ActionListener



{



protected Spatial obj;

protected boolean forward;

protected Vector3f camDir;

protected Vector3f camLeft;

protected Vector3f walkDirection;

protected PhysicsCharacterNode player;



public characterGame(Spatial o){



obj = o;

registerInput();

initCollision();



}



private void initCollision(){



player = new PhysicsCharacterNode(new CapsuleCollisionShape(1f, 1f, 1), .05f);

player.setFallSpeed(30);

player.setGravity(100);

player.setLocalTranslation(new Vector3f(0, 10, 0));

bulletAppState.getPhysicsSpace().add(player);

rootNode.attachChild(player);



}



private void registerInput() {



String[] inputs = {“Forward”, “Down”, “Left”, “Right”};



inputManager.addMapping(“Forward”, new KeyTrigger(KeyInput.KEY_Z));

inputManager.addMapping(“Back”, new KeyTrigger(KeyInput.KEY_S));

inputManager.addMapping(“Left”, new KeyTrigger(KeyInput.KEY_Q));

inputManager.addMapping(“Right”, new KeyTrigger(KeyInput.KEY_D));



inputManager.addListener(this, inputs);





}



@Override

public void onAction(String name, boolean isPressed, float tpf) {



if(name.equals(“Forward”)){



forward = true;



}



}



@Override

public void update() {



walkDirection.set(0, 0, 0);



camDir = cam.getDirection();

camLeft = cam.getLeft();



if(forward){



walkDirection.addLocal(camDir);



}



player.setWalkDirection(walkDirection);



}



}[/java]



GRAVE: Uncaught exception thrown in Thread[LWJGL Renderer Thread,5,main]

java.lang.NullPointerException

at mygame.characterGame.registerInput(characterGame.java:46)

at mygame.characterGame.(characterGame.java:26)

at mygame.game.simpleInitApp(game.java:59)

at mygame.paramGame.initialize(paramGame.java:180)



I do not understand InputManager is not null . It was allocated in memory in the class paramGame. Mmmm there is something I miss with the class inheritance.

It means that you are trying to access the input manager before the simplegame class is initialized. In your character game constructor you might want to make a call to super(); Super calls the constructor of the baseclass you are deriving from. Super may have parameters you need to pass in.

I totally redesigned my class to arrive to this construction :







[java]package mygame;



import com.jme3.bullet.collision.shapes.CapsuleCollisionShape;

import com.jme3.bullet.nodes.PhysicsCharacterNode;

import com.jme3.input.InputManager;

import com.jme3.input.KeyInput;

import com.jme3.input.controls.ActionListener;

import com.jme3.input.controls.KeyTrigger;

import com.jme3.math.Vector3f;

import com.jme3.renderer.Camera;

import com.jme3.scene.Node;

import com.jme3.scene.Spatial;



public class characterGame implements ActionListener



{



protected Spatial obj;

protected boolean forward;

protected Vector3f camDir;

protected Vector3f camLeft;

protected Vector3f walkDirection;

protected PhysicsCharacterNode player;

protected BulletAppState bulletAppState;

protected Node rootNode;

protected InputManager inputManager;

protected Camera cam;



public characterGame(Spatial o, Camera c, InputManager inputM, BulletAppState bull, Node rootN){



obj = o;

bulletAppState = bull;

rootNode = rootN;

cam = c;

registerInput(inputM);

initCollision();



}



private void initCollision(){



player = new PhysicsCharacterNode(new CapsuleCollisionShape(1f, 1f, 1), .05f);

player.setFallSpeed(30);

player.setGravity(5);

player.setLocalTranslation(new Vector3f(0, 10, 0));

bulletAppState.getPhysicsSpace().add(player);

rootNode.attachChild(player);

player.attachChild(obj);



}



private void registerInput(InputManager inputM) {



String[] inputs = {“Forward”, “Down”, “Left”, “Right”};



inputManager = inputM;



inputManager.addMapping(“Forward”, new KeyTrigger(KeyInput.KEY_Z));

inputManager.addMapping(“Back”, new KeyTrigger(KeyInput.KEY_S));

inputManager.addMapping(“Left”, new KeyTrigger(KeyInput.KEY_Q));

inputManager.addMapping(“Right”, new KeyTrigger(KeyInput.KEY_D));



inputManager.addListener(this, inputs);





}



@Override

public void onAction(String name, boolean isPressed, float tpf) {



if(name.equals(“Forward”)){



forward = true;



}



}



public void update() {



walkDirection.set(0, 0, 0);



camDir = cam.getDirection();

camLeft = cam.getLeft();



if(forward){



walkDirection.addLocal(camDir);



}



player.setWalkDirection(walkDirection);



}



}

[/java]



Beetwen the two solutions do you know which one is faster?