Help using multiple class files

I am trying to have one Main.java file with a Map.java file and a Player.java file. The map file will generate the terrain and the player file will initiate the cross hairs and other player aspects.

I want to know how to use the main method in the Main.java file to run methods in the Map and Player files that would normally work in the simpleInitApp() method without opening different application windows.

I know this sounds very confusing so I can clarify if nobody understands what I am getting at.

Check out some java and object oriented programming tutorials, this is dire basics that you should learn before attempting to write a game.

@norman I know how to call methods from other files. What I can’t figure out is how to call them the correct way with jMonkey.

I don’t want my Main file to be SUPER long so I want to call a map generation method from my Map file to run in the Main file.

Basically, I used the terrain generation tutorial and renamed the simpleInitApp() method to run() and called run() in the Main file. This did not work and I’m not 100% sure why.

You main class must implement simpleInitApp() because simpleInitApp() is a virtual method of the virtual class SimpleApplication.

After main() calls start(), the startup code executes, which eventually passes control to simpleInitApp().

The upshot is that your Map and Player methods should be invoked from simpleInitApp(), not main().

@Connor14 said: @norman I know how to call methods from other files. What I can't figure out is how to call them the correct way with jMonkey.

I don’t want my Main file to be SUPER long so I want to call a map generation method from my Map file to run in the Main file.

Basically, I used the terrain generation tutorial and renamed the simpleInitApp() method to run() and called run() in the Main file. This did not work and I’m not 100% sure why.

Theres hundreds of ways to use multiple classes but you don’t seem to have an actual reason to create a new class. You don’t make a new class when the current one is “full”, you make a new class to have a new type of object that you can then use. I can’t possibly say what kinds of objects you need for your application.

So my statement from before is still valid. Or you ignore it, don’t learn object-oriented programming and keep doing stuff in main. Thats then more like in old programming languages or in script languages, simply calling methods in one big namespace. Probably easier for now.

@Connor14 said: @norman I know how to call methods from other files. What I can't figure out is how to call them the correct way with jMonkey.

I don’t want my Main file to be SUPER long so I want to call a map generation method from my Map file to run in the Main file.

Basically, I used the terrain generation tutorial and renamed the simpleInitApp() method to run() and called run() in the Main file. This did not work and I’m not 100% sure why.

Because you can’t just randomly rename callback methods and expect your app to still work. I echo Normen’s suggestion that you learn Java first before attempting a 3D game. It’s clear from this thread that you barely know Object Oriented Programming or you’d have sorted this out in 4 seconds.

If this is what’s tripping you up now, there are a hundred more complicated (for you) things waiting just around the corner. And that’s completely ignoring the 1000 things that are complicated for decent Java developers already.

@norman I have looked at documentation per your suggestion and I have a general understanding of using methods; however, I am still running into problems. I hope you or another person can help me without getting too annoyed with me as a newbie. Please let me know if what I am trying to do is even possible.

Again, I was trying to pull the run() method from my Map file into the simpleInitApp() method in Main.java so that I can run the map from a different file without opening a new application window. I am obviously doing ALOT of things wrong right now that I would like some assistance with. Basically, my overall question is how can I generate the map for my game from a different file?

Here is my Main.java file
[java]package mygame;

import com.jme3.app.SimpleApplication;
import com.jme3.renderer.RenderManager;

/**

  • test

  • @author normenhansen
    */
    public class Main extends SimpleApplication {

    public static void main(String[] args) {
    Main app = new Main();
    app.start();
    }

    @Override
    public void simpleInitApp() {
    Map map = new Map();
    map.run();
    }

    @Override
    public void simpleUpdate(float tpf) {
    //TODO: add update code
    }

    @Override
    public void simpleRender(RenderManager rm) {
    //TODO: add render code
    }
    }
    [/java]

And my Map file that I was trying to pull methods from:

[java]public class Map extends SimpleApplication
implements ActionListener {

private BulletAppState bulletAppState;
private RigidBodyControl landscape;
private CharacterControl player;
private Vector3f walkDirection = new Vector3f();
private boolean left = false, right = false, up = false, down = false;
private TerrainQuad terrain;
private Material mat_terrain;

@Override
public void simpleInitApp() {

}

public void run() {
/** Set up Physics */
bulletAppState = new BulletAppState();
stateManager.attach(bulletAppState);
//bulletAppState.getPhysicsSpace().enableDebug(assetManager);

flyCam.setMoveSpeed(100);
setUpKeys();

/** 1. Create terrain material and load four textures into it. */
mat_terrain = new Material(assetManager, 
        "Common/MatDefs/Terrain/Terrain.j3md");

/** 1.1) Add ALPHA map (for red-blue-green coded splat textures) */
mat_terrain.setTexture("Alpha", assetManager.loadTexture(
        "Textures/alphamap.png"));

/** 1.2) Add GRASS texture into the red layer (Tex1). */
Texture grass = assetManager.loadTexture(
        "Textures/grass.jpg");
grass.setWrap(WrapMode.Repeat);
mat_terrain.setTexture("Tex1", grass);
mat_terrain.setFloat("Tex1Scale", 64f);

/** 1.3) Add DIRT texture into the green layer (Tex2) */
Texture dirt = assetManager.loadTexture(
        "Textures/dirt.jpg");
dirt.setWrap(WrapMode.Repeat);
mat_terrain.setTexture("Tex2", dirt);
mat_terrain.setFloat("Tex2Scale", 32f);

/** 1.4) Add ROAD texture into the blue layer (Tex3) */
Texture rock = assetManager.loadTexture(
        "Textures/road.jpg");
rock.setWrap(WrapMode.Repeat);
mat_terrain.setTexture("Tex3", rock);
mat_terrain.setFloat("Tex3Scale", 128f);

/** 2. Create the height map */
AbstractHeightMap heightmap = null;
Texture heightMapImage = assetManager.loadTexture(
        "Textures/mountains512.png");
heightmap = new ImageBasedHeightMap(heightMapImage.getImage());
heightmap.load();

/** 3. We have prepared material and heightmap. 
 * Now we create the actual terrain:
 * 3.1) Create a TerrainQuad and name it "my terrain".
 * 3.2) A good value for terrain tiles is 64x64 -- so we supply 64+1=65.
 * 3.3) We prepared a heightmap of size 512x512 -- so we supply 512+1=513.
 * 3.4) As LOD step scale we supply Vector3f(1,1,1).
 * 3.5) We supply the prepared heightmap itself.
 */
terrain = new TerrainQuad("my terrain", 65, 513, heightmap.getHeightMap());

/** 4. We give the terrain its material, position & scale it, and attach it. */
terrain.setMaterial(mat_terrain);
terrain.setLocalTranslation(0, -100, 0);
terrain.setLocalScale(2f, 1f, 2f);
rootNode.attachChild(terrain);

/** 5. The LOD (level of detail) depends on were the camera is: */
List<Camera> cameras = new ArrayList<Camera>();
cameras.add(getCamera());
TerrainLodControl control = new TerrainLodControl(terrain, cameras);
terrain.addControl(control);

/** 6. Add physics: */ 
// We set up collision detection for the scene by creating a static 
terrain.addControl(new RigidBodyControl(0));

// We set up collision detection for the player by creating
// a capsule collision shape and a CharacterControl.
// The CharacterControl offers extra settings for
// size, stepheight, jumping, falling, and gravity.
// We also put the player in its starting position.
CapsuleCollisionShape capsuleShape = new CapsuleCollisionShape(1.5f, 6f, 1);
player = new CharacterControl(capsuleShape, 0.05f);
player.setJumpSpeed(20);
player.setFallSpeed(30);
player.setGravity(30);
player.setPhysicsLocation(new Vector3f(-10, 10, 10));

// We attach the scene and the player to the rootnode and the physics space,
// to make them appear in the game world.
bulletAppState.getPhysicsSpace().add(terrain);
bulletAppState.getPhysicsSpace().add(player);

}
/** We over-write some navigational key mappings here, so we can

  • add physics-controlled walking and jumping: */
    private void setUpKeys() {
    inputManager.addMapping(“Left”, new KeyTrigger(KeyInput.KEY_A));
    inputManager.addMapping(“Right”, new KeyTrigger(KeyInput.KEY_D));
    inputManager.addMapping(“Up”, new KeyTrigger(KeyInput.KEY_W));
    inputManager.addMapping(“Down”, new KeyTrigger(KeyInput.KEY_S));
    inputManager.addMapping(“Jump”, new KeyTrigger(KeyInput.KEY_SPACE));
    inputManager.addListener(this, “Left”);
    inputManager.addListener(this, “Right”);
    inputManager.addListener(this, “Up”);
    inputManager.addListener(this, “Down”);
    inputManager.addListener(this, “Jump”);
    }

/** These are our custom actions triggered by key presses.

  • We do not walk yet, we just keep track of the direction the user pressed. */
    public void onAction(String binding, boolean value, float tpf) {
    if (binding.equals(“Left”)) {
    if (value) { left = true; } else { left = false; }
    } else if (binding.equals(“Right”)) {
    if (value) { right = true; } else { right = false; }
    } else if (binding.equals(“Up”)) {
    if (value) { up = true; } else { up = false; }
    } else if (binding.equals(“Down”)) {
    if (value) { down = true; } else { down = false; }
    } else if (binding.equals(“Jump”)) {
    player.jump();
    }
    }

/**

  • This is the main event loop–walking happens here.
  • We check in which direction the player is walking by interpreting
  • the camera direction forward (camDir) and to the side (camLeft).
  • The setWalkDirection() command is what lets a physics-controlled player walk.
  • We also make sure here that the camera moves with player.
    */
    @Override
    public void simpleUpdate(float tpf) {
    Vector3f camDir = cam.getDirection().clone().multLocal(0.6f);
    Vector3f camLeft = cam.getLeft().clone().multLocal(0.4f);
    walkDirection.set(0, 0, 0);
    if (left) { walkDirection.addLocal(camLeft); }
    if (right) { walkDirection.addLocal(camLeft.negate()); }
    if (up) { walkDirection.addLocal(camDir); }
    if (down) { walkDirection.addLocal(camDir.negate()); }
    player.setWalkDirection(walkDirection);
    cam.setLocation(player.getPhysicsLocation());
    }
    }[/java]

I know this will sound obvious once I’ve stated it… but only one application per application.

@pspeed So what you’re saying is that it is not possible to generate my map from another file? Even if I managed to pull in the methods the correct way in my Main.java file?

@Connor14 said: @pspeed So what you're saying is that it is not possible to generate my map from another file? Even if I managed to pull in the methods the correct way in my Main.java file?

It’s 100% possible but trying to shove more than one application into your application is like trying to shove more than one house into your house just because you want another room.

Then again, if I set out to build a house without knowing how to build a house that is what I might try to do and then wonder why my inside house doesn’t have power and water and stuff like my outside house does.

Seriously, create a separate class that is NOT an application. Pass to it the stuff you need. If any of that doesn’t instantly click then go back and do Java tutorials until it does.

Learning to program in Java = really hard.
Learning to write a game = really hard.
Learning to write a 3D game = super hard.

Trying to do all of them at once it like trying to learn to swim in the middle of the ocean with sharks circling without ever having seen water before.

If this rudimentary stuff is tripping you up then the next things will be a struggle every step of the way.