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