I believe i am having a gui issue although at this point I’m not entirely sure. it seems that clicking on the pictures in my nifty gui only works about 10% of the time and when it does work it looks like it doesn’t call the correct methods. I have quite a bit of code for it and i apologize in advance. I will try to put only what is necessary.
code from main class:
niftyDisplay = new NiftyJmeDisplay(
assetManager, inputManager, audioRenderer, guiViewPort);
actives = new Node(“actives”);
nifty = niftyDisplay.getNifty();
guiViewPort.addProcessor(niftyDisplay);
for(int i = 0; i < 20; i++)
{
inventoryImages = “Models/Images/empty.png”;
}
player = new Node();
ammo[9] = 100;
bullet = assetManager.loadModel(“Models/guns/bullet.j3o”);
bullet.scale(0.1F);
initCrossHairs();
nifty.loadStyleFile(“nifty-default-styles.xml”);
nifty.loadControlFile(“nifty-default-controls.xml”);
final ScreenController ref = this;
//
nifty.addScreen(“inventory”, new ScreenBuilder(“nifty inventory”){{
controller(ref); // Screen properties
//
layer(new LayerBuilder(“item layer”) {{
childLayoutHorizontal(); // layer properties, add more…
backgroundColor("#000f");
//
panel(new PanelBuilder(“item panel_1”) {{
childLayoutVertical(); // panel properties, add more…
height(“100%”);
width(“16%”);
panel(new PanelBuilder(“item_1”) {{
height(“25%”);
width(“100%”);
childLayoutCenter();
image( new ImageBuilder() {{
id(“c1r1”);
filename(inventoryImages[0]);
visibleToMouse(true);
interactOnClick(“attachGun1ToPlayer()”);
//interactOnMouseOver(“getInfo()”);
}});
}});
panel(new PanelBuilder(“item_2”) {{
height(“25%”);
width(“100%”);
childLayoutCenter();
image( new ImageBuilder() {{
id(“c1r2”);
filename(inventoryImages[1]);
visibleToMouse(true);
interactOnClick(“equipItem(spot2)”);
//interactOnMouseOver(“getInfo()”);
}});
}});
panel(new PanelBuilder(“item_3”) {{
height(“25%”);
width(“100%”);
childLayoutCenter();
image( new ImageBuilder() {{
id(“c1r3”);
filename(inventoryImages[2]);
visibleToMouse(true);
interactOnClick(“equipItem(spot3)”);
//interactOnMouseOver("");
}});
}});
panel(new PanelBuilder(“item_4”) {{
height(“25%”);
width(“100%”);
childLayoutCenter();
image( new ImageBuilder() {{
id(“c1r4”);
filename(inventoryImages[3]);
visibleToMouse(true);
interactOnClick(“equipItem(spot4)”);
//interactOnMouseOver("");
}});
}});
}});
panel(new PanelBuilder(“item panel_2”){{
childLayoutVertical();
height(“100%”);
width(“16%”);
panel(new PanelBuilder(“item_1a”) {{
height(“25%”);
width(“100%”);
childLayoutCenter();
image( new ImageBuilder() {{
id(“c2r1”);
filename(inventoryImages[4]);
visibleToMouse(true);
interactOnClick(“equipItem(spot5)”);
// interactOnMouseOver("");
}});
}});
panel(new PanelBuilder(“item_2a”) {{
height(“25%”);
width(“100%”);
childLayoutCenter();
image( new ImageBuilder() {{
id(“c2r2”);
filename(inventoryImages[5]);
visibleToMouse(true);
interactOnClick(“equipItem(spot6)”);
//interactOnMouseOver("");
}});
}});
panel(new PanelBuilder(“item_3a”) {{
height(“25%”);
width(“100%”);
childLayoutCenter();
image( new ImageBuilder() {{
id(“c2r3”);
filename(inventoryImages[6]);
visibleToMouse(true);
interactOnClick(“equipItem(spot7)”);
//interactOnMouseOver("");
}});
}});
panel(new PanelBuilder(“item_4a”) {{
height(“25%”);
width(“100%”);
childLayoutCenter();
image( new ImageBuilder() {{
id(“c2r4”);
filename(inventoryImages[7]);
visibleToMouse(true);
interactOnClick(“equipItem(spot8)”);
//interactOnMouseOver("");
}});
}});
}});
panel(new PanelBuilder(“item panel_3”){{
childLayoutVertical();
height(“100%”);
width(“16%”);
panel(new PanelBuilder(“item_1b”) {{
height(“25%”);
width(“100%”);
childLayoutCenter();
image( new ImageBuilder() {{
id(“c3r1”);
filename(inventoryImages[8]);
visibleToMouse(true);
interactOnClick(“equipItem(spot9)”);
//interactOnMouseOver("");
}});
}});
panel(new PanelBuilder(“item_2b”) {{
height(“25%”);
width(“100%”);
childLayoutCenter();
image( new ImageBuilder() {{
id(“c3r2”);
filename(inventoryImages[9]);
visibleToMouse(true);
interactOnClick(“equipItem(spot10)”);
//interactOnMouseOver("");
}});
}});
panel(new PanelBuilder(“item_3b”) {{
height(“25%”);
width(“100%”);
childLayoutCenter();
image( new ImageBuilder() {{
id(“c3r3”);
filename(inventoryImages[10]);
visibleToMouse(true);
interactOnClick(“equipItem(spot11)”);
//interactOnMouseOver("");
}});
}});
panel(new PanelBuilder(“item_4b”) {{
height(“25%”);
width(“100%”);
childLayoutCenter();
image( new ImageBuilder() {{
id(“c3r4”);
filename(inventoryImages[11]);
visibleToMouse(true);
interactOnClick(“equipItem(spot12)”);
//interactOnMouseOver("");
}});
}});
}});
panel(new PanelBuilder(“item panel_4”){{
childLayoutVertical();
height(“100%”);
width(“16%”);
panel(new PanelBuilder(“item_1c”) {{
height(“25%”);
width(“100%”);
childLayoutCenter();
image( new ImageBuilder() {{
id(“c4r1”);
filename(inventoryImages[12]);
visibleToMouse(true);
interactOnClick(“equipItem(spot13)”);
//interactOnMouseOver("");
}});
}});
panel(new PanelBuilder(“item_2c”) {{
height(“25%”);
width(“100%”);
childLayoutCenter();
image( new ImageBuilder() {{
id(“c4r2”);
filename(inventoryImages[13]);
visibleToMouse(true);
interactOnClick(“equipItem(spot14)”);
//interactOnMouseOver("");
}});
}});
panel(new PanelBuilder(“item_3c”) {{
height(“25%”);
width(“100%”);
childLayoutCenter();
image( new ImageBuilder() {{
id(“c4r3”);
filename(inventoryImages[14]);
visibleToMouse(true);
interactOnClick(“equipItem(spot15)”);
//interactOnMouseOver("");
}});
}});
panel(new PanelBuilder(“item_4c”) {{
height(“25%”);
width(“100%”);
childLayoutCenter();
image( new ImageBuilder() {{
id(“c4r4”);
filename(inventoryImages[15]);
visibleToMouse(true);
interactOnClick(“equipItem(spot16)”);
//interactOnMouseOver("");
}});
}});
}});
panel(new PanelBuilder(“item panel_5”){{
childLayoutVertical();
height(“100%”);
width(“16%”);
panel(new PanelBuilder(“item_1d”) {{
height(“25%”);
width(“100%”);
childLayoutCenter();
image( new ImageBuilder() {{
id(“c5r1”);
filename(inventoryImages[16]);
visibleToMouse(true);
interactOnClick(“equipItem(spot17)”);
//interactOnMouseOver("");
}});
}});
panel(new PanelBuilder(“item_2d”) {{
height(“25%”);
width(“100%”);
childLayoutCenter();
image( new ImageBuilder() {{
id(“c5r2”);
filename(inventoryImages[17]);
visibleToMouse(true);
interactOnClick(“equipItem(spot18)”);
//interactOnMouseOver("");
}});
}});
panel(new PanelBuilder(“item_3d”) {{
height(“25%”);
width(“100%”);
childLayoutCenter();
image( new ImageBuilder() {{
id(“c5r3”);
filename(inventoryImages[18]);
visibleToMouse(true);
interactOnClick(“equipItem(spot19)”);
//interactOnMouseOver("");
}});
}});
panel(new PanelBuilder(“item_4d”) {{
height(“25%”);
width(“100%”);
childLayoutCenter();
image( new ImageBuilder() {{
id(“c5r4”);
filename(inventoryImages[19]);
visibleToMouse(true);
interactOnClick(“equipItem(spot20)”);
//interactOnMouseOver("");
}});
}});
}});
//
}});
//
}}.build(nifty));
this is in my simpleInitApp() method. it is the menu im concerned with really the only one that im trying to fix is the first one. if i fix it i can fix all of them. i did have a screen controller on it but i figured it would be better to make my main class implement screencontroller and just go straight to the method in it for equiping weapons.
here is the method:
public int gunDamage;
public int ammoType;
public int fireRate;
public Spatial gunU;
public void attachGun1ToPlayer()
{
System.out.println(“got into attach gun”);
Spatial gun = assetManager.loadModel(“Models/guns/” + inventoryImages[0] + “.j3o”);
rootNode.attachChild(gun);
gun.setLocalTranslation(cam.getLocation());
gunU = gun;
clipSize = invData[0][0];
ammoType = invData[0][1];
gunDamage = invData[0][2];
}
then in my update loop i have this for keeping the gun at the camera:
if(gunU != null)
{
gunU.setLocalTranslation(cam.getLocation());
}
the issue im having is i cant get the gun to attach to the screen and also i have issues with shooting (it acts like there has been no current variables set for clipsize and such) here is relevant code for that issue:
public void shoot()
{
Spatial newbullet = bullet.clone();
rootNode.attachChild(newbullet);
newbullet.setLocalTranslation(cam.getLocation());
bullet_phy = new RigidBodyControl(2F);
newbullet.addControl(bullet_phy);
bulletAppState.getPhysicsSpace().add(bullet_phy);
bullet_phy.setLinearVelocity(cam.getDirection().mult(25));
}
(this next one im wondering if the mouse thing there doesnt work i set it up with key T to get it to at least register because it doesnt seem to even enter the shoot method with the mouse set up like this)
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.addMapping(“Menu”, new KeyTrigger(KeyInput.KEY_TAB));
inputManager.addMapping(“Activate”, new KeyTrigger(KeyInput.KEY_E));
inputManager.addMapping(“Shoot”, new KeyTrigger(MouseInput.BUTTON_LEFT));
inputManager.addMapping(“Reload”, new KeyTrigger(KeyInput.KEY_R));
inputManager.addListener(this, “Menu”);
inputManager.addListener(this, “Activate”);
inputManager.addListener(this, “Left”);
inputManager.addListener(this, “Right”);
inputManager.addListener(this, “Up”);
inputManager.addListener(this, “Down”);
inputManager.addListener(this, “Jump”);
inputManager.addListener(this, “Shoot”);
inputManager.addListener(this, “Reload”);
}
this next one has all of my actions in it they seem to work but im worried about the one that sets the data to invData[][] it seems to work but when i recall the gun and try to get the data im not getting anything i think its a problem with the method not calling though (attachGun1ToPlayer() that is)
so this probably doesn’t matter at all
int b;
public void onAction(String binding, boolean value, float tpf) {
if (binding.equals(“Left”)) {
if(!invFlag)
{
left = value;
}
} else if (binding.equals(“Right”)) {
if(!invFlag)
{
right = value;
}
} else if (binding.equals(“Up”)) {
if(!invFlag)
{
up = value;
}
} else if (binding.equals(“Down”)) {
if(!invFlag)
{
down = value;
}
} else if (binding.equals(“Jump”)) {
if(!invFlag)
{
playerControl.jump();
}
}else if (binding.equals(“Menu”) && !value){
if(invFlag)
{
startHUD();
}else{
startInven();
}
invFlag = !invFlag;
}else if (binding.equals(“Activate”)){
// 1. Reset results list.
CollisionResults results = new CollisionResults();
// 2. Aim the ray from cam loc to cam direction.
Ray ray = new Ray(cam.getLocation(), cam.getDirection());
// 3. Collect intersections between Ray and Shootables in results list.
actives.collideWith(ray, results);
// 4. Print the results
System.out.println("
Collisions? " + results.size() + "
");
for (int i = 0; i < results.size(); i++) {
// For each hit, we know distance, impact point, name of geometry.
float dist = results.getCollision(i).getDistance();
Vector3f pt = results.getCollision(i).getContactPoint();
}
// 5. Use the results (we mark the hit object)
if (results.size() > 0) {
// The closest collision point is what was truly hit:
CollisionResult closest = results.getClosestCollision();
if (closest.getDistance() <= 100)
{
String toAdd = (String)closest.getGeometry().getName(); // <
WORK ON THIS IT ISN'T WORKING FOR SOME GOD AWFUL IMPOSSIBLE TO FIGURE OUT REASON
System.out.println(toAdd);
int spot= 0;
boolean flag = true;
//int data1 = (Integer) closest.getGeometry().getUserData("data"); // <
THIS WORKS TIME TO ATTACTH THESE TO AN OFFSCREEN NODE TO SAVE ALL THEIR STATS AND STUFF
//System.out.println(data1);
for(int i = 0; i<20; i++)
{
if(inventoryImages.equals("Models/Images/empty.png"))
{
inventoryImages= toAdd;
String picReplace = "";
System.out.println(i);
if(i == 0)
{
picReplace = "c1r1";
}else if (i == 1)
{
picReplace = "c1r2";
}else if (i == 2)
{
picReplace = "c1r3";
}else if (i == 3)
{
picReplace = "c1r4";
}else if (i == 4)
{
picReplace = "c2r1";
}else if (i == 5)
{
picReplace = "c2r2";
}else if (i == 6)
{
picReplace = "c2r3";
}else if (i == 7)
{
picReplace = "c2r4";
}else if (i == 8)
{
picReplace = "c3r1";
}else if (i == 9)
{
picReplace = "c3r2";
}else if (i == 10)
{
picReplace = "c3r3";
}else if (i == 11)
{
picReplace = "c3r4";
}else if (i == 12)
{
picReplace = "c4r1";
}else if (i == 13)
{
picReplace = "c4r2";
}else if (i == 14)
{
picReplace = "c4r3";
}else if (i == 15)
{
picReplace = "c4r4";
}else if (i == 16)
{
picReplace = "c5r1";
}else if (i == 17)
{
picReplace = "c5r2";
}else if (i == 18)
{
picReplace = "c5r3";
}else if (i == 19)
{
picReplace = "c5r4";
}
System.out.println(picReplace);
System.out.println(inventoryImages);
// load or create new image
NiftyImage img = nifty.getRenderEngine().createImage("Models/Images/" +inventoryImages + ".jpg", false);
// find old image
Element niftyElement = nifty.getScreen("inventory").findElementByName(picReplace);
// swap old with new image
niftyElement.getRenderer(ImageRenderer.class).setImage(img);
flag = false;
b = i;
break;
}
}
if(flag)
{
System.out.println("inventory full");
}else{
Geometry g = closest.getGeometry();
Spatial t = g;
t.removeFromParent();
bulletAppState.getPhysicsSpace().remove(t);
invData[0] = t.getUserData("clipSize");
invData[1] = t.getUserData("ammoType");
invData[2] = t.getUserData("damage");
System.out.println(invData[0]);
System.out.println(b);
}
}
}
}else if(binding.equals("Shoot"))
{
if(clipAmount > 0)
{
shoot();
clipAmount = clipAmount - 1;
}else{
System.out.println("clip out reload");
}
}else if(binding.equals("Reload"))
{
System.out.println("reloading");
if(ammo[ammoType] > 0)
{
if(ammo[ammoType] > clipSize)
{
clipAmount = clipAmount + ammo[ammoType];
ammo[ammoType] = 0;
}else{
ammo[ammoType] = ammo[ammoType] - (clipSize - clipAmount);
clipAmount = clipSize;
}
}
}
}
what's wrong here? it seems to be a problem with Nifty and im so sick and tired of nifty being an issue for me. It is a buggy api that seems to rarely work compared to something like swing or AWT. Im at the end of my rope. I cant solve it. Maybe one of you can. PLEASE HELP
other info: it seemed to have the same issue with clicking on a picture even with the other class as its controller BUT i DID notice that it only had an issue when i added code into the method to call a seperate method in the main class. When it was just System.out.println() stuff it seemed to work fine 100% of the time.
ps. is there another option from nifty? i dont care if its harder as long as it ACTUALLY WORKS
pps. i fixed my mouse input issue figured out i had the keytrigger thing wrong (woops)
ppps. after fixing mouse input it seems to let me shoot a couple times after reloading the gun after i take it out of inventory (gun starts with no bullets in clip) but after i run out reload no longer works O.o it still isnt attaching the model to the camera though D:< still not entirely sure the screen is working
pppps. also my mouse input (and now key input) all register twice!!!!! why is this happening it is shooting twice for each click (noticed its once for click once for release) and reload does twice too (key R)
ppppps fixed the problem with one reload it was one operator gone wrong (a > instead of a <) now it works fine so HOW DO I ATTACH THE GUN TO THE SCREEN?