Selaa lähdekoodia

Wiki refresh (Hello Input) (#155)

I am going through the beginner wiki pages and updating them so they are the same as the source code files in jme3test/helloworld.

This edit tweaks player geometry and private boolean lines, adds a few comments, and then updates a few rotate and move methods to reflect the source code. 

jme3test/helloworld source code:
https://github.com/jMonkeyEngine/jmonkeyengine/blob/master/jme3-examples/src/main/java/jme3test/helloworld/HelloInput.java
woodx319 3 vuotta sitten
vanhempi
commit
0e9004f2c6
1 muutettua tiedostoa jossa 26 lisäystä ja 32 poistoa
  1. 26 32
      docs/modules/tutorials/pages/beginner/hello_input_system.adoc

+ 26 - 32
docs/modules/tutorials/pages/beginner/hello_input_system.adoc

@@ -29,9 +29,8 @@ import com.jme3.input.controls.AnalogListener;
 import com.jme3.input.controls.KeyTrigger;
 import com.jme3.input.controls.MouseButtonTrigger;
 
-/**
- * Sample 5 - how to map keys and mousebuttons to actions
- */
+/** Sample 5 - how to map keys and mouse buttons to actions */
+
 public class HelloInput extends SimpleApplication {
 
     public static void main(String[] args) {
@@ -39,8 +38,8 @@ public class HelloInput extends SimpleApplication {
         app.start();
     }
 
-    protected Geometry player;
-    private boolean isRunning = true;
+    private Geometry player;
+    private Boolean isRunning = true;
 
     @Override
     public void simpleInitApp() {
@@ -53,23 +52,22 @@ public class HelloInput extends SimpleApplication {
         initKeys(); // load my custom keybinding
     }
 
-    /**
-     * Custom Keybinding: Map named actions to inputs.
-     */
+    /** Custom Keybinding: Map named actions to inputs. */ 
     private void initKeys() {
-        // You can map one or several inputs to one named action
+        /* You can map one or several inputs to one named mapping. */ 
         inputManager.addMapping("Pause",  new KeyTrigger(KeyInput.KEY_P));
         inputManager.addMapping("Left",   new KeyTrigger(KeyInput.KEY_J));
         inputManager.addMapping("Right",  new KeyTrigger(KeyInput.KEY_K));
         inputManager.addMapping("Rotate", new KeyTrigger(KeyInput.KEY_SPACE),
                                           new MouseButtonTrigger(MouseInput.BUTTON_LEFT));
-        // Add the names to the action listener.
+        /* Add the named mappings to the action listeners. */ 
         inputManager.addListener(actionListener, "Pause");
         inputManager.addListener(analogListener, "Left", "Right", "Rotate");
 
     }
 
-    private final ActionListener actionListener = new ActionListener() {
+  /** Use this listener for KeyDown/KeyUp events */ 
+    final private ActionListener actionListener = new ActionListener() {
         @Override
         public void onAction(String name, boolean keyPressed, float tpf) {
             if (name.equals("Pause") && !keyPressed) {
@@ -78,20 +76,19 @@ public class HelloInput extends SimpleApplication {
         }
     };
 
-    private final AnalogListener analogListener = new AnalogListener() {
+  /** Use this listener for continuous events */ 
+    final private AnalogListener analogListener = new AnalogListener() {
         @Override
         public void onAnalog(String name, float value, float tpf) {
             if (isRunning) {
                 if (name.equals("Rotate")) {
-                    player.rotate(0, value * speed, 0);
+                    player.rotate(0, value, 0);
                 }
                 if (name.equals("Right")) {
-                    Vector3f v = player.getLocalTranslation();
-                    player.setLocalTranslation(v.x + value * speed, v.y, v.z);
+                    player.move((new Vector3f(value, 0,0)) ); 
                 }
                 if (name.equals("Left")) {
-                    Vector3f v = player.getLocalTranslation();
-                    player.setLocalTranslation(v.x - value * speed, v.y, v.z);
+                    player.move(new Vector3f(-value, 0,0)); 
                 }
             } else {
                 System.out.println("Press P to unpause.");
@@ -131,7 +128,7 @@ Have a look at the code:
 [source,java]
 ----
 
-    // You can map one or several inputs to one named action
+    /* You can map one or several inputs to one named mapping. */ 
     inputManager.addMapping("Pause",  new KeyTrigger(KeyInput.KEY_P));
     inputManager.addMapping("Left",   new KeyTrigger(KeyInput.KEY_J));
     inputManager.addMapping("Right",  new KeyTrigger(KeyInput.KEY_K));
@@ -148,7 +145,7 @@ Now you need to register your trigger mappings.
 [source,java]
 ----
 
-    // Add the names to the action listener.
+    /* Add the named mappings to the action listeners. */ 
     inputManager.addListener(actionListener,"Pause");
     inputManager.addListener(analogListener,"Left", "Right", "Rotate");
 
@@ -173,8 +170,8 @@ In this example, we trigger the following actions:
 [source,java]
 ----
 
-
-    private final ActionListener actionListener = new ActionListener() {
+  /** Use this listener for KeyDown/KeyUp events */ 
+    final private ActionListener actionListener = new ActionListener() {
         @Override
         public void onAction(String name, boolean keyPressed, float tpf) {
             if (name.equals("Pause") && !keyPressed) {
@@ -183,20 +180,19 @@ In this example, we trigger the following actions:
         }
     };
 
-    private final AnalogListener analogListener = new AnalogListener() {
+  /** Use this listener for continuous events */ 
+    final private AnalogListener analogListener = new AnalogListener() {
         @Override
         public void onAnalog(String name, float value, float tpf) {
             if (isRunning) {
                 if (name.equals("Rotate")) {
-                    player.rotate(0, value * speed, 0);
+                    player.rotate(0, value, 0);
                 }
                 if (name.equals("Right")) {
-                    Vector3f v = player.getLocalTranslation();
-                    player.setLocalTranslation(v.x + value * speed, v.y, v.z);
+                    player.move((new Vector3f(value, 0,0)) ); 
                 }
                 if (name.equals("Left")) {
-                    Vector3f v = player.getLocalTranslation();
-                    player.setLocalTranslation(v.x - value * speed, v.y, v.z);
+                    player.move(new Vector3f(-value, 0,0)); 
                 }
             } else {
                 System.out.println("Press P to unpause.");
@@ -225,15 +221,13 @@ private class MyCombinedListener implements AnalogListener, ActionListener {
     public void onAnalog(String name, float value, float tpf) {
         if (isRunning) {
             if (name.equals("Rotate")) {
-                player.rotate(0, value * speed, 0);
+                player.rotate(0, value, 0);
             }
             if (name.equals("Right")) {
-                Vector3f v = player.getLocalTranslation();
-                player.setLocalTranslation(v.x + value * speed, v.y, v.z);
+                player.move((new Vector3f(value, 0,0)) ); 
             }
             if (name.equals("Left")) {
-                Vector3f v = player.getLocalTranslation();
-                player.setLocalTranslation(v.x - value * speed, v.y, v.z);
+                player.move(new Vector3f(-value, 0,0)); 
             }
         } else {
             System.out.println("Press P to unpause.");