0

I am working on my final project for my game design and development class. I have a method that creates both a class object for a character and a spatial geometry to represent an enemy character.

The enemy character is created but I cannot access the object created in the method just the geometry.

Here is the method that creates the enemy:

/*
Method to create an enemy model.  
*/
protected Spatial makeEnemy(String t, float x, float y, float z) {

    Character emy = new Character(t);

    // load a character from jme3test-test-data
    Spatial enemy = assetManager.loadModel(emy.getLocation());
    if (t=="ogre") {
        enemy.scale(4.0f);
    } else {
        enemy.scale(1.0f);
    }

    enemy.setLocalTranslation(x, y, z);

    //add physics
    enemy_phy = new RigidBodyControl(2f);
    enemy.addControl(enemy_phy);
    bulletAppState.getPhysicsSpace().add(enemy_phy);

    //add a light to make the model visible
    DirectionalLight sun = new DirectionalLight();
    sun.setDirection(new Vector3f(-0.1f, -0.7f, 1.0f));
    enemy.addLight(sun);
    enemy.rotate(0.0f, 3.0f, 0.0f);
    return enemy;
}//end makeEnemy

The character class is as follows:

public class Character {

    private String modelLocation; //the location of the model
    private int hitPoints;
    private int atk;
    private int score;

    Character() {
        modelLocation = "Models/Ninja/Ninja.mesh.xml";//makes the ninja the default character
        hitPoints = 10;
        atk = 1;
        score = 0;
    }//end no argument constructor

    /*
    This will be the constructor used primarily for 
    creating enemy characters.
    */
    Character(String s){
        if(s=="ogre"){
            modelLocation = "Models/Sinbad.j3o";
            hitPoints = 5;
            atk = 1;
            score= 0;
        } else {
            modelLocation = "Models/Oto/Oto.mesh.xml";
            hitPoints = 10;
            atk = 1;
            score = 0;
        }
    }//end constructor

    /*
    This constructor will be used for 
    creating the player character.
    */
    Character(String s, int h, int a){
        modelLocation = s;
        hitPoints = h;
        atk = a;
        score= 0;//score always starts at zero
    }//end constructor

    /*
    Getter Methods
    */
    public String getLocation() {
        return modelLocation;
    }//end getLocation
    public int getHitPoints() {
        return hitPoints;
    }//end getHitPoints
    public int getAtk() {
        return atk;
    }//end getAtk
    public int getScore() {
        return score;
    }//end getScore

    /*
    Setter methods
    */
    public void setHitPoints(int n) {
        hitPoints = n;
    }//end setHitPoints
    public void setAtk(int n) {
        atk = n;
    }//end setAtk
    public void setScore(int n) {
        score = n;
    }//end setScore

    //method to deal damage to character
    public void takeDamage(int a) {
        hitPoints = hitPoints - a;
    }//end takeDamage

    //mehtod to add to character score
    public void addScore() {
        if(modelLocation == "Models/Sinbad.j3o") {
            score = score + 1; // 1 point for a ogre
        } else {
            score = score + 2; //2 points for golems
        }//end if else
    }//end addScore
}//end character

In another method I am able to access the spatial created in this method and make changes to it, but not the emy character object created. I just need to know how to access its hitPoints variable and such from outside the method.

Anil
  • 655
  • 1
  • 11
  • 25
Desmond
  • 9
  • 3

2 Answers2

1

Local variables are accessible only "locally", meaning only within its scope which is usually defined by curly brackets ({}) they are created (only the method know where they are).

To solve the mentioned issue and make the variable with the Character available outside the method you have a few options:

  • Implement the Character variable as an instance (maybe even class variable) and initialise it inside the method.
  • The better approach would be to return the Character variable by your method.

Assuming that the Spatial object describes the position of the Character, you could return the Character and the Spatial as a key value pair. In this situation, I would go with another approach and would define the Spatial for each Character within the Character object itself (I do not know whether your architecture allows this).

Felix Seifert
  • 552
  • 1
  • 9
  • 19
0

Since it's jMonkeyEngine, consider this: Make Character extend AbstractControl. Then:

enemy.addControl(emy);

You will now be able to access it through:

Character char = enemy.getControl(Character.class);
char.setHitPoints(10000);

Controls are your friends: https://wiki.jmonkeyengine.org/jme3/advanced/custom_controls.html

reden
  • 968
  • 7
  • 14