Can't get collectibles to work

Hi all,
This is the first time for me working with Java and Jmonkeyengine. My goal is to make a fps with collectibles like slenderman. I’ve made 8 collectibles and spread them across the map. I want the GUI to have a text that tells tha player how many collectibles he has collected so far. The problem with my code is that it keeps displaying 0/8. Another thing is that when I want to play a sound when picking up the item it keeps repeating when my distance is smaller than 3 from the collectible that was once there.

My code concerning the collectibles and audio:

private AudioNode audio_gun;
private Spatial collectible1;
private Spatial collectible2;
private Spatial collectible3;
private Spatial collectible4;
private Spatial collectible5;
private Spatial collectible6;
private Spatial collectible7;
private Spatial collectible8;
private int collected=0;
private boolean cc1, cc2, cc3, cc4, cc5, cc6, cc7, cc8 ;
private BitmapText counter;
private float distance1, distance2, distance3, distance4,
              distance5, distance6, distance7, distance8;

@Override
    public void simpleInitApp() {
guiFont = assetManager.loadFont("Interface/Fonts/Default.fnt");
    counter = new BitmapText(guiFont, false);
    counter.setSize(guiFont.getCharSet().getRenderedSize() * 3);  
    counter.setText("0/8");
    counter.setLocalTranslation( 
    (settings.getWidth()/2+ 500) , 100  , 1);

initCollect();
initAudio();
}

private void initCollect(){
   collectible1 = assetManager.loadModel("Models/Cranio.j3o");
    collectible1.setLocalTranslation(25, 5, -109);
    collectible1.scale((float) 0.5);
    rootNode.attachChild(collectible1);
    
    collectible2 = assetManager.loadModel("Models/Cranio.j3o");
    collectible2.setLocalTranslation(435, 12, (float) -97.4);
    collectible2.scale((float) 0.5);
    rootNode.attachChild(collectible2);
    
    collectible3 = assetManager.loadModel("Models/Cranio.j3o");
    collectible3.setLocalTranslation(317, 5, -85);
    collectible3.scale((float) 0.5);
    rootNode.attachChild(collectible3);
    
    collectible4 = assetManager.loadModel("Models/Cranio.j3o");
    collectible4.setLocalTranslation(350, 5, -175);
    collectible4.scale((float) 0.5);
    rootNode.attachChild(collectible4);
    
    collectible5 = assetManager.loadModel("Models/Cranio.j3o");
    collectible5.setLocalTranslation((float)385.5, 9, (float)-246.5);
    collectible5.scale((float) 0.5);
    rootNode.attachChild(collectible5);
    
    collectible6 = assetManager.loadModel("Models/Cranio.j3o");
    collectible6.setLocalTranslation(140, 10, -148);
    collectible6.scale((float) 0.5);
    rootNode.attachChild(collectible6);
    
    collectible7 = assetManager.loadModel("Models/Cranio.j3o");
    collectible7.setLocalTranslation((float)350.4,3,-376);
    collectible7.scale((float) 0.5);
    rootNode.attachChild(collectible7);
    
    collectible8 = assetManager.loadModel("Models/Cranio.j3o");
    collectible8.setLocalTranslation((float)98.5, 5, -307);
    collectible8.scale((float) 0.5);
    rootNode.attachChild(collectible8);
    
   }

private void initAudio() {
    audio_pickup = new AudioNode(assetManager, "Sounds/pickup.ogg");
    audio_pickup.setPositional(false);
    audio_pickup.setLooping(false);
    audio_pickup.setVolume(1);
    rootNode.attachChild(audio_pickup);
  }

@Override
    public void simpleUpdate(float tpf) {
        cam.setLocation(player.getPhysicsLocation());
        
        collectible1.rotate(0f, 2*tpf, 0f);
        collectible2.rotate(0f, 2*tpf, 0f);
        collectible3.rotate(0f, 2*tpf, 0f);
        collectible4.rotate(0f, 2*tpf, 0f);
        collectible5.rotate(0f, 2*tpf, 0f);
        collectible6.rotate(0f, 2*tpf, 0f);
        collectible7.rotate(0f, 2*tpf, 0f);
        collectible8.rotate(0f, 2*tpf, 0f);
        
        
        distance1 = cam.getLocation().distance(new Vector3f (25,5,-109));
        if (distance1 < 3){
        audio_pickup.playInstance();
        collectible1.removeFromParent();
        counter.setText(Integer.toString(collected)+"/8");
        if(cc1){
        ++collected;
        cc1 = false;
        } 
      }
        distance2 = cam.getLocation().distance(new Vector3f (435, 12, (float) -97.4));
        if (distance2 < 3){
        audio_pickup.playInstance();
        collectible2.removeFromParent();
        counter.setText(Integer.toString(collected)+"/8");
        if(cc2){
        ++collected;
        cc2 = false;
        } 
      }
        distance3 = cam.getLocation().distance(new Vector3f (317, 5, -85));
        if (distance3 < 3){
        audio_pickup.playInstance();
        collectible3.removeFromParent();
        counter.setText(Integer.toString(collected)+"/8");
        if(cc3){
        ++collected;
        cc3 = false;
        } 
      }
        distance4 = cam.getLocation().distance(new Vector3f (350, 5, -175));
        if (distance4 < 3){
        audio_pickup.playInstance();
        collectible4.removeFromParent();
        counter.setText(Integer.toString(collected)+"/8");
        if(cc4){
        ++collected;
        cc4 = false;
        } 
      }
        distance5 = cam.getLocation().distance(new Vector3f ((float)385.5, 9, (float)-246.5));
        if (distance5 < 3){
        audio_pickup.playInstance();
        collectible5.removeFromParent();
        counter.setText(Integer.toString(collected)+"/8");
        if(cc5){
        ++collected;
        cc5 = false;
        } 
      }
        distance6 = cam.getLocation().distance(new Vector3f (140, 10, -148));
        if (distance6 < 3){
        audio_pickup.playInstance();
        collectible6.removeFromParent();
        counter.setText(Integer.toString(collected)+"/8");
        if(cc6){
        ++collected;
        cc6 = false;
        } 
      }
        distance7 = cam.getLocation().distance(new Vector3f ((float)350.4,3,-376));
        if (distance7 < 3){
        audio_pickup.playInstance();
        collectible7.removeFromParent();
        counter.setText(Integer.toString(collected)+"/8");
        if(cc7){
        ++collected;
        cc7 = false;
        } 
      }
        distance8 = cam.getLocation().distance(new Vector3f ((float)98.5, 5, -307));
        if (distance8 < 3){
        audio_pickup.playInstance();
        collectible8.removeFromParent();
        counter.setText(Integer.toString(collected)+"/8");
        if(cc8){
        ++collected;
        cc8 = false;
        } 
      }
        guiNode.attachChild(counter);
}

If anyone knows what I’m missing, thanks :grin:

You’re checking for picking up a collectible even if it already was removed. It also updates the text repeatedly but I’m not sure if that’s the issue with the display of “0/8”. Put the check if(cc1) around the whole distance check.
Save yourself some hassle and make an object for “Collectible” so you don’t have to define the position twice etc. or use collectible.getLocalTranslation()… And use a function or a loop to remove this kind of code duplication (the distance checks) :wink:

Well, first of all, do you know what object orientation is?

You should analyze your code yourself. Concerning the sounds: You check if the distance is closer than 3 and if so you play an instance of your sound, no matter if your item has been picked up or not.

Furthermore, your ccX values have the initial value “false”. In update you check if those values are true and if so you increment your counter and set ccX to false. The problem here: ccX never gets true, so the condition will never be true.

Maybe you should learn a little bit more about basic programming patterns, Java, object orientation (OO) before you start programming a game. I don’t want to discourage you but … start smaller … for example in the console. Then go further and program a game with a swing gui (TicTacToe as example).

jMonkeyEngine is not a drag and drop game builder, you really should bring some programing skills to the table.

Best regards
Domenic