ActionListener problem

[java]
package mygame;

import com.jme3.app.SimpleApplication;

import com.jme3.scene.Geometry;
import com.jme3.scene.shape.Box;
import com.jme3.light.AmbientLight;
import com.jme3.math.Vector3f;
import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector2f;
import com.jme3.texture.Texture;
import com.jme3.scene.control.AbstractControl;

import kunysz.customcontrollers.RotationController;
import com.jme3.scene.control.AbstractControl;
import com.jme3.input.controls.ActionListener;

import com.jme3.input.controls.InputListener;
import com.jme3.input.controls.MouseButtonTrigger;
import com.jme3.input.controls.KeyTrigger;
import com.jme3.input.MouseInput;

/**
*

  • @author K.K
    */
    public class ModelBrowser extends SimpleApplication {

    private boolean isKeyPressed=false;
    private Geometry myGeom;
    public static void main(String args[]){

     ModelBrowser app=new ModelBrowser();
     app.start();
    

    }

    @Override
    public void simpleInitApp(){

     AmbientLight myambientLight= new AmbientLight();
     rootNode.addLight(myambientLight);
     
     
     Box mybox= new Box(100f,0.1f,100f);
     Geometry Ziemia_holder=new Geometry("Box",mybox);
    Material mat1 = new Material(assetManager,"Common/MatDefs/Misc/Unshaded.j3md");
     
    Texture ziemiatexture =assetManager.loadTexture("Textures/grass01.jpg");
    ziemiatexture.setWrap(Texture.WrapMode.Repeat);
    mat1.setTexture("ColorMap", ziemiatexture);
     Ziemia_holder.setMaterial(mat1);
     Ziemia_holder.getMesh().scaleTextureCoordinates(new Vector2f(30f,30f));
     rootNode.attachChild(Ziemia_holder);
     
     
    
     
     
     Box myObject=new Box(3f,3f,3f);
     Geometry myGeom=new Geometry("Box",myObject);
     
     Material matgeom= new Material(assetManager,"Common/MatDefs/Misc/Unshaded.j3md");
     Texture myGeomTex=assetManager.loadTexture("Textures/wood10.jpg");
     myGeomTex.setWrap(Texture.WrapMode.Repeat);
     matgeom.setTexture("ColorMap",myGeomTex);
     myGeom.getMesh().scaleTextureCoordinates(new Vector2f(1f,1f));
     myGeom.setMaterial(matgeom);
     myGeom.setLocalTranslation(new Vector3f(0f,3f,0f));
     
     //conroler for a object //
    // RotationController mycontroll=new RotationController();
     myGeom.addControl(new RotationController());
     //new RotationController().cloneForSpatial(myGeom);
     
     //myGeom.getControl(RotationController.class).setSpeed(10);
     
     
     
     rootNode.attachChild(myGeom);
     
     cam.setLocation(new Vector3f(0f,10f,5f));
     flyCam.setMoveSpeed(100f);
     
     initKeys();
    

    }

    //Map for keys don’t forget to implement;
    private void initKeys(){
    inputManager.addMapping(“Rotate”, new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
    inputManager.addListener(actionListener,“Rotate”);
    }

    private ActionListener actionListener = new ActionListener(){
    public void onAction(String name, boolean keyPressed, float tpf ){

         if (name.equals("Rotate") && !keyPressed){
    

    myGeom.getControl(RotationController.class).setSpeed(10);

     ;
     
        
         }
         
     }
    

    };

    @Override
    public void simpleUpdate(float tpf){

    }

    //public void onAnalog(String name, float value, float tpf) {
    // System.out.println(“on analog fired”);
    //}

}
[/java]

myGeom.getControl(RotationController.class).setSpeed(10); - This line in ActionListener returns void instad of RotationController . I don’t get it! but executed form SipleInitApp function work like charm - maybe I am missing a reference of this controller at rootNode level ?

The SDK is giving you a warning on this line:
Geometry myGeom=new Geometry(“Box”,myObject);

Address the warning and you will fix your problem.

1 Like

There was a warnnig about the scope of the viariable. I have tried with variable of class level and the same variable in SipleInitApp function - and did not work , still with no acess to controller.

But it wasn’t the couse of the problem . I did read some othe treads in this forum about how to get an access of Geometry elements and I have found that i some cases . There is a need of explicit use (Geometry) before myGeom.getControl(RotationController.class).setSpeed(10);
In the case above ActionListener woks fine but called myGeom.getControl(RotationController.class returns void instead of contoller (very confusing);

((Geometry)myGeom).getControl(RotationController.class).setSpeed(10); // Works! cube changes speed of rotation Z axis to 10;

And problem solved / the geometry was the right tip - Thank you for a help :sunglasses:

@Kwasula said: There was a warnnig about the scope of the viariable. I have tried with variable of class level and the same variable in SipleInitApp function - and did not work , still with no acess to controller.

But it wasn’t the couse of the problem . I did read some othe treads in this forum about how to get an access of Geometry elements and I have found that i some cases . There is a need of explicit use (Geometry) before myGeom.getControl(RotationController.class).setSpeed(10);
In the case above ActionListener woks fine but called myGeom.getControl(RotationController.class returns void instead of contoller (very confusing);

((Geometry)myGeom).getControl(RotationController.class).setSpeed(10); // Works! cube changes speed of rotation Z axis to 10;

And problem solved / the geometry was the right tip - Thank you for a help :sunglasses:

No, something else fixed it. You should never have to cast a Geometry to a Geometry as the compiler will happily remove it for you.

When you do this as in your original code:

[java]
public class MyClass {
private Geometery myGeometry;

public void someMethod() {
    Geometry myGeometry = foo;
    //  this 'myGeometry' is local to this method only.
}

public void someOtherMethod() {
    // myGeometry will still be null here  because the class-level myGeometry was never set to anything... thus
    // you will get an NPE if you try to access it.
}

}
[/java]

fixed by

private Spatial myGeom;

[java]myGeom=new Geometry(“Box”,myObject);[/java]

And in ActionListener i have used this:

java.getControl(RotationController.class).setSpeed(10f); [/java]

mybe this is not right? but works!

Change myGeom to Geometry instead of Spatial and you won’t need the cast.

Yes - it goes to Spatial and works - thanks - problem solved

Marked the topic as resolved for you.