[SOLVED] Objects and arrays

hi all,

I’m trying to make a breakout style game as a project to learn JME with.

I’m trying to make the blocks with an array, but I really cant seem to get it to work.

My array code so far is:
(Gbox is a box geometry)

[java]
int[] topline = new int[10];
int i;

    for (i=0; i < topline.length; i++){
        Gbox.clone();
        Gbox.move(i, i, i);
        
        
    }

[/java]

Im sure I’m missing something pretty obvious but I’ve tried everything I can think of, so if someone could point me in the right direction I’d really appreciate it.

Thanks!

You haven’t told us what’s not working.

One thing you should be aware of is that geometries are not visible unless they’ve been added to the
scene graph.

Make sure you attach Gbox.clone() to the rootnode either directly or to another node which is attached to the rootnode. Here is an example of creating multiple spatials with an array:

[java]
gold = assetManager.loadModel(“Models/Items/Gold/Cylinder.mesh.j3o”);
gold.setMaterial(assetManager.loadMaterial(“Models/Items/Gold/gold.j3m”));

        goldCoins = new Node();

        amountOfGold = new Spatial[amount];

        for (int i = 1; i < amount; i++) {
            amountOfGold[i] = gold.clone();
            amountOfGold[i].rotate(300f, 20f, 300f);
            amountOfGold[i].setMaterial(assetManager.loadMaterial("Models/Items/Gold/gold.j3m"));
            amountOfGold[i].setLocalScale(0.25f, 0.25f, 0.25f);
            amountOfGold[i].setName("Gold");
            goldCoins.attachChild(amountOfGold[i]);
            for (float r = 0; r < 0.25; r += 0.01) {
                amountOfGold[i].setLocalTranslation(location.add(new Vector3f(random.nextInt(4), r, random.nextInt(4))));
            }
        }
        game.getApp().getRootNode().attachChild(goldCoins);

        GeometryBatchFactory.optimize(goldCoins, true);

[/java]

It may be worth picking up the JME beginner’s guide. I still don’t have it =( But… that’s what I get for being poor. From all accounts it brings you from not knowing how to make a game in JME to having a pretty solid foundation to making your own games. Not to mention, it actually helps support the dev/hosting/etc efforts that are out-of-pocket expenses atm (I believe).

Anyways, I’m really looking forward to getting a copy as soon as I can afford it. Maybe it will help?

Thanks a lot for all the help everyone! Its been really helpful and Ive nearly got it working now, but I’ve run into a second problem.
I get a line of boxes, which is what I want, and I’m trying to assign them random colors. So I have 5 different materials which I defined before the for loop, created a random number for each iteration in the for loop and have an if/else condition in the loop to decide which color the box should be depending on the random number. What happens however is I get a line of white boxes (my default cube material) and a few blue ones in the line which occupy the same space as some of the white ones so I get a kind of Z-fighting effect. I think the problem is somewhere in my if conditions, please could you take a quick look?
[java]
TLB = new Node();

    toplineboxes = new Spatial[10];
    
    for (int i=0; i<=9;i++){
    
    int rn = FastMath.rand.nextInt(10) + 1;
            
    System.out.println("array number:");
    System.out.println(i);
    System.out.println("random number:");
    System.out.println(rn);
    
    toplineboxes[i] = Gbox.clone();
    if(rn <2){
        toplineboxes[i].setMaterial(BGboxmat);    
    }else{
        if(rn<4){
            toplineboxes[i].setMaterial(CGboxmat);
        }else{
            if(rn<6){
                toplineboxes[i].setMaterial(YGboxmat);
            }else{
                if(rn<8){
                    toplineboxes[i].setMaterial(GGboxmat);
                }else{
                    if(rn<10){
                        toplineboxes[i].setMaterial(RGboxmat);
                    }
                }
            }
        }
    }
    TLB.attachChild(toplineboxes[i]);
    
    toplineboxes[i].setLocalTranslation((new Vector3f(i*0.3f,0f,0f)));
    rootNode.attachChild(TLB);

}
[/java]

Instead of doing else{if}}, try else if(rn<2){}else if(rn<4){}

1 Like

If your boxes overlap then you will have z fighting. It has nothing to do with the materials.

But why have them overlap?

Thanks a lot for all the help! and taking the time to look at my code :slight_smile:
got it working now :slight_smile: