JME基础教程代码分析5

package com.hello;


import com.jme3.app.SimpleApplication;

import com.jme3.material.Material;

import com.jme3.math.Vector3f;

import com.jme3.scene.Geometry;

import com.jme3.scene.shape.Box;

import com.jme3.math.ColorRGBA;

import com.jme3.input.KeyInput;

import com.jme3.input.MouseInput;

import com.jme3.input.controls.ActionListener;

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 */

public class HelloInput extends SimpleApplication {

 

  public static void main(String[] args) {

    HelloInput app = new HelloInput();

    app.start();

  }

  protected Geometry player;

  Boolean isRunning=true;

 

  @Override

  public void simpleInitApp() {

    Box b = new Box(Vector3f.ZERO, 1, 1, 1);

    player = new Geometry("Player", b);

    Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");

    mat.setColor("Color", ColorRGBA.Blue);

    player.setMaterial(mat);

    rootNode.attachChild(player);

    initKeys(); // load my custom keybinding

  }

 

  /** Custom Keybinding: Map named actions to inputs. */

  //这里是关键

  private void initKeys() {

    // You can map one or several inputs to one named action

 //查看看addMapping源码,Trigger可以是多个,键盘触发和鼠标触发、其他输入设备触发,映射名称可以自定义

    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.

    //根据键组可以加入监听器

    inputManager.addListener(actionListener, new String[]{"Pause"});

    inputManager.addListener(analogListener, new String[]{"Left", "Right", "Rotate"});

 

  }

 //暂停的监听操作

  private ActionListener actionListener = new ActionListener() {

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

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

        isRunning = !isRunning;

      }

    }

  };

 //旋转和位移控制监听

  private AnalogListener analogListener = new AnalogListener() {

    public void onAnalog(String name, float value, float tpf) {

      if (isRunning) {

        if (name.equals("Rotate")) {

          player.rotate(0, value*speed, 0);

        }

        if (name.equals("Right")) {

          Vector3f v = player.getLocalTranslation();

          player.setLocalTranslation(v.x + value*speed, v.y, v.z);

        }

        if (name.equals("Left")) {

          Vector3f v = player.getLocalTranslation();

          player.setLocalTranslation(v.x - value*speed, v.y, v.z);

        }

      } else {

        System.out.println("Press P to unpause.");

      }

    }

  };

}

posted on 2011-08-12 11:27  苏桓(osbert)  阅读(402)  评论(0编辑  收藏  举报

导航