Changing a picture with an if statement

Hi all,

I am currently in the process of learning java and jmonkey, I have 2 images of guns that I want to use in my game. One with a muzzle flash and the other without. I have got the first gun drawn in the correct place with:

[java]Picture gun = new Picture("weapon");

gun.setImage(assetManager, "Interface/ak1.png", true);

gun.setWidth(settings.getWidth()/2);

gun.setHeight(settings.getHeight()/2);

gun.setPosition(settings.getWidth()-settings.getWidth()/2, 2);

guiNode.attachChild(gun);[/java]



Now I want the second image of the gun to appear (the one with the muzzle flash) when the left mouse button is clicked. I have set up the key mappings and the listener etc… I need to know how to set up an if statement that will change the image of the gun on click (among other things.) Could anybody more experienced help me?

Thanks in advance.

Theres so many ways to do this. Heres an example, keep a reference to the current gun shown:

[java]private Picture currentGun;[/java]



Inside your listener put:

[java]if(currentGun.getName().equals("weapon")) {



guiNode.detachChild(gun);

guiNode.attachChild(muzzleFlashGun);

currentGun = muzzleFlashGun;



} else {

guiNode.detachChild(muzzleFlashFun);

guiNode.attachChild(gun);

currentGun = gun;



}[/java]

Cleaning a little bit more :slight_smile:



[java]

String currentName = currentGun.getName();

guiNode.detachChild(currentName.equals(“weapon”)?gun:muzzleFlashFun);

guiNode.attachChild(currentName.equals(“weapon”)?(currentGun=muzzleFlashFun):(currentGun=gun));

[/java]



But I think it will confuse you more than help xD. Do not listen to me if you can’t understand this.

Wait, hangabouts, does the method private Picture currentGun; contain anything? Where do i put my

[java]Picture gun = new Picture(“weapon”);

gun.setImage(assetManager, “Interface/ak1.png”, true);

gun.setWidth(settings.getWidth()/2);

gun.setHeight(settings.getHeight()/2);

gun.setPosition(settings.getWidth()-settings.getWidth()/2, 2);[/java]

And my

[java]Picture gun = new Picture(“MuzzleFlashGun”);

gun.setImage(assetManager, “Interface/ak2.png”, true);

gun.setWidth(settings.getWidth()/2);

gun.setHeight(settings.getHeight()/2);

gun.setPosition(settings.getWidth()-settings.getWidth()/2, 2);[/java]

if I put the 2 pictures anywhere but inside the actual if statement i get red wiggly lines underneath the “gun” and “muzzlelflashgun” identifiers. Oh no! I don’t know what i’m doing. The guy who is making models for me i going to facepalm!

does the method private Picture currentGun; contain anything?


That's not a method at all!!!! That's a Picture object to store the *current* gun like the wezrule's example!

if I put the 2 pictures anywhere but inside the actual if statement i get red wiggly lines underneath the “gun” and “muzzlelflashgun” identifiers


Of course. Your are giving the same object declaration twice *gun*. And you are programming structured, it's kinda *delphi* :P. Learn to code *object oriented* man, you are coding in java ;).
Also, create a method for creating your pictures :

[java]
private Picture createPicture(String name, String filename) {
Picture picture = new Picture(name);
picture.setImage(assetManager, filename, true);
picture.setWidth(settings.getWidth()/2);
picture.setHeight(settings.getHeight()/2);
picture.setPosition(settings.getWidth()-settings.getWidth()/2, 2);
}
[/java]

Then create your pictures so:

[java]
Picture gun = createPicture("gun", "Interface/ak1.png");
Picture muzzleFlashGun = createPicture("gun", "Interface/ak1.png");
[/java]

:).



-.-. I just wanted to be efficient. So be free to code structured as you are now if you are not able to understand that, so:

http://www.javabeginner.com/

I am very sorry, but I am fairly new to java and perhaps my biggest mistake has been jumping straight into jmonkey after reading this http://www.java-made-easy.com/ and not having developed any sort of application apart from a small program designed to put a Scottish surname on the end of your first name. (And before you ask, I have gone through all the beginner tutorials and they mention nothing like this). I thank you for your patience but I still need help.

I have my method for creating the pictures set up like so:

[java]

private Picture currentGun;

public void pictures(){

Picture gun = new Picture(“gun”);

gun.setImage(assetManager, “Interface/ak1.png”, true);

gun.setWidth(settings.getWidth()/2);

gun.setHeight(settings.getHeight()/2);

gun.setPosition(settings.getWidth()-settings.getWidth()/2, 2);



public void pictures(){

Picture gun = new Picture(“gun2”);

gun2.setImage(assetManager, “Interface/ak2.png”, true);

gun2.setWidth(settings.getWidth()/2);

gun2.setHeight(settings.getHeight()/2);

gun2.setPosition(settings.getWidth()-settings.getWidth()/2, 2);

}[/java]



And am now puzzled as to where to put the actual if statement. Is it here? Like this?

[java] private ActionListener actionListener = new ActionListener() {

public void onAction(String name, boolean keyPressed, float tpf) {

if (name.equals(“Shoot”) && !keyPressed) {

if(currentGun.getName().equals(“weapon”)) {



guiNode.detachChild(gun);

guiNode.attachChild(gun2);

currentGun = gun2;



} else {

guiNode.detachChild(gun2);

guiNode.attachChild(gun);

currentGun = gun;



}

}



}



};[/java]

I have tried putting “Picture gun, Picture gun2” in the parenthesis in public void onAction() which gets rid of the red wiggly lines underneath gun and gun2 but puts a bigger one under [java]private ActionListener actionListener = new ActionListener() {[/java]. perhaps it is something to do with “abstract methods” which is to me and “abstract topic”.

Perhaps one day, with sufficient help, I will have the ability to give back to this forum and community all that I have taken from it in learning.

Live long, and prosper.

gun and gun2 are local to the pictures() method(s???) and can’t be accessed outside of those methods… versus currentGun which is a field of the object and can be accessed anywhere inside the object.



All of your problems are Java-learning problems and not really JME related. You will have a much easier time if you spend a few days (weeks?) going through Java tutorials until you understand the language basics. In this case, some rudimentary knowledge on “variable scope” would have come in handy.

maybe you should read about the stack and heap. Your creating references to Pictures in picture(), but those will be lost when the variables containing the references to them are popped off the stack. So you should create instance variables of them, which will be retained for the duration of the object’s existence.

[java]

private Picture gun1;

private Picture gun2;

private Picture currentGun;



public void pictures(){

gun1 = new Picture(“gun”);

gun1.setImage(assetManager, “Interface/ak1.png”, true);

gun1.setWidth(settings.getWidth()/2);

gun1.setHeight(settings.getHeight()/2);

gun1.setPosition(settings.getWidth()-settings.getWidth()/2, 2);



gun2 = new Picture(“gun2”);

gun2.setImage(assetManager, “Interface/ak2.png”, true);

gun2.setWidth(settings.getWidth()/2);

gun2.setHeight(settings.getHeight()/2);

gun2.setPosition(settings.getWidth()-settings.getWidth()/2, 2);

}[/java]



your if statement is fine where it is



and if your feeling adventurous you do as @glaucomardano suggests, which would slim it down some:



[java]

private Picture gun1 = createPicture(“gun1”, “Interface/ak1.png” )

private Picture gun2 = createPicture(“gun2”, “Interface/ak2.png”);

private Picture currentGun;



public ClassName() { //class constructor

guiNode.attachChild(gun1);

currentGun = gun1;

}



private Picture createPicture(String name, String filename) {

Picture picture = new Picture(name);

picture.setImage(assetManager, filename, true);

picture.setWidth(settings.getWidth()/2);

picture.setHeight(settings.getHeight()/2);

picture.setPosition(settings.getWidth()-settings.getWidth()/2, 2);

return picture;

} [/java]

You are absolutely correct. I must walk before I can run. I thought that the level of knowledge I had was sufficient and the JME tutorials would take me the rest of the way, apparently not. Some more time in eclipse would do me good, albeit it’s not nearly as fun as trying to use JME :slight_smile:

Ok, I know I’ve just said i’ll spend some time reviewing the tutorials, but could you please explain something first:

I have all the code code pieced together like so (with no red wiggly lines!!)



[java]private Picture gun1;

private Picture gun2;

private Picture currentGun;



public void pictures(){

gun1 = new Picture(“gun1”);

gun1.setImage(assetManager, “Interface/ak1.png”, true);

gun1.setWidth(settings.getWidth()/2);

gun1.setHeight(settings.getHeight()/2);

gun1.setPosition(settings.getWidth()-settings.getWidth()/2, 2);



gun2 = new Picture(“gun2”);

gun2.setImage(assetManager, “Interface/ak2.png”, true);

gun2.setWidth(settings.getWidth()/2);

gun2.setHeight(settings.getHeight()/2);

gun2.setPosition(settings.getWidth()-settings.getWidth()/2, 2);

}

private ActionListener actionListener = new ActionListener() {

public void onAction(String name, boolean keyPressed, float tpf) {

if (name.equals(“Shoot”) && !keyPressed) {

if(currentGun.getName().equals(“weapon”)) {



guiNode.detachChild(gun1);

guiNode.attachChild(gun2);

currentGun = gun2;



} else {

guiNode.detachChild(gun2);

guiNode.attachChild(gun1);

currentGun = gun1;



}

}



}

};

[/java]



And when run, I get the horrible block of red text at the bottom that I have seen so many times:

[java]29-Dec-2011 10:20:48 com.jme3.app.Application handleError

SEVERE: Uncaught exception thrown in Thread[LWJGL Renderer Thread,5,main]

java.lang.NullPointerException

at mygame.Main$1.onAction(Main.java:133)

at com.jme3.input.InputManager.invokeActions(InputManager.java:183)

at com.jme3.input.InputManager.onMouseButtonEventQueued(InputManager.java:418)

at com.jme3.input.InputManager.processQueue(InputManager.java:791)

at com.jme3.input.InputManager.update(InputManager.java:841)

at com.jme3.app.Application.update(Application.java:567)

at com.jme3.app.SimpleApplication.update(SimpleApplication.java:235)

at com.jme3.system.lwjgl.LwjglAbstractDisplay.runLoop(LwjglAbstractDisplay.java:149)

at com.jme3.system.lwjgl.LwjglDisplay.runLoop(LwjglDisplay.java:185)

at com.jme3.system.lwjgl.LwjglAbstractDisplay.run(LwjglAbstractDisplay.java:223)

at java.lang.Thread.run(Thread.java:662)[/java]



If you could do one last thing for me, could it be to tell me where I’m going wrong?

Live long, and prosper.

do you ever call pictures()?



create a constructor in your class and put the code you want to be called (i.e pictures()), constructors are called every time you create an object of this class.



[java]

class Main {

public Main() {

pictures();

}

}

[/java]



please review all java concepts, we are not here to teach you java! :stuck_out_tongue:

Please, go to the java concepts, and then go to the java practise. After that you can start coding in jME. I advise you to read some java books too. It’s fundamental!



http://docs.oracle.com/javase/tutorial/java/concepts/



You are simply programming structured. I’ve noticed that your main difficulty is with objects. But do not confuse primary variables with objects. When I started coding in java, I had the same questions like you. I couldn’t understand how objects works, and polymorphism was very difficulty for me. And what more taught me how code object oriented in java, was some 2D / 3D game books, like “Killer Programming in java”. Btw you learn the things with the time, and in a few months you’ll be a nice java programmer and kicking asses ;).

1 Like