Making A Class That Spawns Objects?

Hey everyone.

I coded a random number generator that spits out x and y coordinates in a nonrepeating fashion. I’ll show you the code now.
[java]public class Spawner{

private BulletAppState bulletAppState;
private RigidBodyControl landscape;
 private Spatial sceneModel;

void spawn(int max, int min, int xRange, int yRange, String modelName) {

    Random r = new Random();
    //Below is the number of random items in order (random items) + min
    int spawnNumb = r.nextInt(max) + min;
    boolean b = true;
    List<Integer> randomNumberListX = new ArrayList<Integer>();
    List<Integer> randomNumberListY = new ArrayList<Integer>();
    List<Integer> randomNumberListSum = new ArrayList<Integer>();

    for (int starter = 0; starter <= spawnNumb; starter++) {
        //Below are max coordinate values
        int xcoord = r.nextInt(xRange) + 1;
        int ycoord = r.nextInt(yRange) + 1;
        int sumCoord = (xcoord + ycoord);
        if (randomNumberListX.contains(xcoord) && randomNumberListY.contains(ycoord) && randomNumberListSum.contains(sumCoord)) {

            b = false;

        }
        if (b) {
            
            sceneModel = assetManager.loadModel(modelName);
            sceneModel.setLocalScale(25f);
            sceneModel.setLocalTranslation(xcoord, ycoord, 0);
            CollisionShape sceneShape = CollisionShapeFactory.createMeshShape((Node) sceneModel);
            rootNode.attachChild(sceneModel);
            landscape = new RigidBodyControl(sceneShape, 0);
            sceneModel.addControl(landscape);
            rootNode.attachChild(sceneModel);
            bulletAppState.getPhysicsSpace().add(landscape);
            randomNumberListX.add(xcoord);
            randomNumberListY.add(ycoord);
            randomNumberListSum.add(sumCoord);
        }
        b = true;
    }
}

}

The problem is that, because it doesn’t extend the simple application, it doesn’t know what the rootnode and asset manager are.

How should I make it so that I can call upon this class in its entirety?

Thanks a ton
-Win

You have to pass the rootnode and assetmanager as paramters through the spawn method.
Or, you could make them attributes of the class and pass them as paramters to the constructor (or any other method for initialization).

If you don’t know any one of the words above (or what they mean in a java context), I highly recommond looking it up and learning more java and object oriented programming.

2 Likes

Do I leave the class to extend simpleapplication?

Also, I’m afraid my variables bulletappstate, landscape, and sceneModel are throwing it off because they exist (necessarily) in my main class also. Anyhow, even with rootNode and assetManager as parameters, I’m still getting errors on the spawn method and Spawner as a class because it isn’t abstract. I feel like it wants me to start an app, even though this class isn’t supposed to run an app.

Here’s my code.

[package mygame;

import java.util.ArrayList;
import java.util.Random;
import java.util.List;
import com.jme3.bullet.control.RigidBodyControl;
import com.jme3.app.SimpleApplication;
import com.jme3.bullet.BulletAppState;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.bullet.collision.shapes.CollisionShape;
import com.jme3.bullet.util.CollisionShapeFactory;

public class Spawner extends SimpleApplication {

private BulletAppState bulletAppState;
private RigidBodyControl landscape;
private Spatial sceneModel;



void spawn(int max, int min, int xRange, int yRange, String modelName, assetManager, rootNode) {
    int multx;
    int multy;
    Random r = new Random();
    int negx = r.nextInt(2) - 2;
    int negy = r.nextInt(2) - 2;
    //Below is the number of random items in order (random items) + min
    if (negx == -1) {
        multx = -1;
    } else {
        multx = 1;
    }
    if (negy == -1) {
        multy = -1;
    } else {
        multy = 1;
    }
    int spawnNumb = r.nextInt(max) + min;
    boolean b = true;
    List<Integer> randomNumberListX = new ArrayList<Integer>();
    List<Integer> randomNumberListY = new ArrayList<Integer>();
    List<Integer> randomNumberListSum = new ArrayList<Integer>();

    for (int starter = 0; starter <= spawnNumb; starter++) {
        //Below are max coordinate values
        int xcoord = r.nextInt(xRange) * multx + 1;
        int ycoord = r.nextInt(yRange) * multy + 1;
        int sumCoord = (xcoord + ycoord);
        if (randomNumberListX.contains(xcoord) && randomNumberListY.contains(ycoord) && randomNumberListSum.contains(sumCoord)) {

            b = false;

        }
        if (b) {

            sceneModel = assetManager.loadModel(modelName);
            sceneModel.setLocalScale(25f);
            sceneModel.setLocalTranslation(xcoord, ycoord, 0);
            CollisionShape sceneShape = CollisionShapeFactory.createMeshShape((Node) sceneModel);
            rootNode.attachChild(sceneModel);
            landscape = new RigidBodyControl(sceneShape, 0);
            sceneModel.addControl(landscape);
            rootNode.attachChild(sceneModel);
            bulletAppState.getPhysicsSpace().add(landscape);
            randomNumberListX.add(xcoord);
            randomNumberListY.add(ycoord);
            randomNumberListSum.add(sumCoord);
        }
        b = true;
    }
}

}]

I have to ask you for how long you’ve known java.

This line:
[java]
void spawn(int max, int min, int xRange, int yRange, String modelName, assetManager, rootNode) {
[/java]
isn’t even a valid start of a method because you don’t specify the types of assetManager and rootNode. It can’t work.

Also, if this is the main class, you don’t need to pass them.
If it isn’t, you don’t extend SimpleApplication.

The errors that this class is not abstract and does not override all methods has nothing at all to do with passing rootNode and assetManager as parameter.

I’m sorry, but in my opinion it’s very apparent that you lack java skills.
While I strongly recommend you to first learn java a bit more, we cannot help you here. Maybe you should tackle some other programming task first.
Or, if you really want to make a game, switch to a game and engine that don’t require any or not very much programming.

I’d really like to help you, but without java knowledge it’d be a very bumpy road.

1 Like

I just looked through your comment history and noticed you’ve been around here for about a year.
I’ll try to be more specific:

You have your main class.
In this main class you have your spawner
[java]
Spawner spawner = new Spawner(assetManager, rootNode);
[/java]

You pass those two to the spawner (not extending SimpleApplication!!). It has private attributes and a constructor which set them:
[java]
private AssetManager assetManager;
private Node rootNode;

public Spawner(AssetManager assetManager, Node rootNode) {
this.assetManager = assetManager;
this.rootNode = rootNode;
}
[/java]

Now if you call the spawner from the main class via
[java]
spawner.spawn(…)
[/java]
it can use both the assetManager and the rootNode.

This is pretty much all the code you need to pass both the rootNode and the assetManager to the Spawner class.
However this is a basic java task.
I do not know how complicated your programs will be, but there are a lot of complicated problems and pitfalls you’ll run into.
I heavily advise learning more java outside of jMonkeyEngine if you want to continue. Make something, like an address book with GUI or anything that requires more than seven classes and get used to the concept of abstract classes and interfaces. This knowledge really matters in programming.

That being said, I wish you great success and will be happy to help if you get stuck along the way.

1 Like

Oh, and one more thing. If you’re going to paste code again, please use

[ j a v a ]

and

[ / j a v a ]

(without spaces) so the code gets formatted properly.

1 Like

Thank you so much for helping me out! It works perfectly!

-Win