(libgdx学习)InputProcessor InputMultiplexer

来源:互联网 发布:58到家数据分析待遇 编辑:程序博客网 时间:2024/04/20 05:09

官方文档部分解释:

1)The first three methods allow you to listen for keyboard events:以下三个方法对键盘事件提供监听

  • keyDown(): Called when a key was pressed down. Reports the key code, as found in Keys.
  • keyUp(): Called when a key was lifted. Reports the key code as above.
  • keyTyped(): Called when a Unicode character was generated by the keyboard input. This can be used to implement text fields and similar user interface elements.
2)

The next three methods report mouse/touch events:

  • touchDown(): Called when a finger went down on the screen or a mouse button was pressed. Reports the coordinates as well as the pointer index and mouse button (always Buttons.LEFTfor touch screens).当手指在屏幕中按下去的时候调用
  • touchUp(): Called when a finger was lifted from the screen or a mouse button was released. Reports the last known coordinates as well as the pointer index and mouse button (always Buttons.Left for touch screens).当手指在屏幕中弹起的时候调用
  • touchDragged(): Called when a finger is being dragged over the screen or the mouse is dragged while a button is pressed. Reports the coordinates and pointer index. The button is not reported as multiple buttons could be pressed while the mouse is being dragged. You can use Gdx.input.isButtonPressed() to check for a specific button.当手指在屏幕中滑动的时候调用
  • touchMoved(): Called when the mouse is moved over the screen without a mouse button being down. This event is only relevant on the desktop and will never occur on touch screen devices where you only get touchDragged() events.
  • scrolled(): Called when the scroll wheel of the mouse was turned. Reports either -1 or 1 depending on the direction of spin. This will never be called for touch screen devices.当你滚动滚轮的时候调用

   
   3)The InputMultiplexer will hand any new events to the first InputProcessor that was added to it. If that processor returns false from the method invoked to handle the event, this indicates the event was not handled and the multiplexer will hand the event to the next processor in the chain. Through this mechanism, the MyUiInputProcessor can handle any events that fall inside one of its widgets and pass on any other events to the MyGameInputProcessor. 使用Inputmultiplexer来处理多个inputprocessor的情况。InputMultiplexer会将事件传给第一个添加的inputprocessor的相应的方法来处理,如果该方法返回false,则会调用第二个Inputprocessor中相应的方法来进行处理。

二、应用举例
package com.example.groupactiontest;import com.badlogic.gdx.ApplicationListener;import com.badlogic.gdx.Gdx;import com.badlogic.gdx.InputMultiplexer;import com.badlogic.gdx.Input.Keys;import com.badlogic.gdx.Input.Peripheral;import com.badlogic.gdx.InputProcessor;import com.badlogic.gdx.graphics.GL10;public class MyGame implements ApplicationListener {InputProcessor inputProcessor;InputProcessor inputProcessor2;@Overridepublic void create() {inputProcessor = new InputProcessor() {@Overridepublic boolean touchUp(int arg0, int arg1, int arg2, int arg3) {System.out.println("inputProcessor--------->touchUp");return true;}@Overridepublic boolean touchDragged(int arg0, int arg1, int arg2) {System.out.println("inputProcessor--------->touchDragged");return true;}@Overridepublic boolean touchDown(int arg0, int arg1, int arg2, int arg3) {System.out.println("inputProcessor--------->touchDown");return true;}@Overridepublic boolean scrolled(int arg0) {System.out.println("inputProcessor--------->scrolled");return true;}@Overridepublic boolean mouseMoved(int arg0, int arg1) {System.out.println("inputProcessor--------->tmouseMoved");return true;}@Overridepublic boolean keyUp(int arg0) {System.out.println("inputProcessor--------->keyUp");return true;}@Overridepublic boolean keyTyped(char arg0) {System.out.println("inputProcessor--------->keyType");return true;}@Overridepublic boolean keyDown(int arg0) {System.out.println("inputProcessor--------->keyDown");return true;}};inputProcessor2 = new InputProcessor() {@Overridepublic boolean touchUp(int arg0, int arg1, int arg2, int arg3) {System.out.println("inputprocessor2---------->touchUp");return true;}@Overridepublic boolean touchDragged(int arg0, int arg1, int arg2) {// TODO Auto-generated method stubreturn false;}@Overridepublic boolean touchDown(int arg0, int arg1, int arg2, int arg3) {System.out.println("inputprocessor2------------->touchDown");return true;}@Overridepublic boolean scrolled(int arg0) {// TODO Auto-generated method stubreturn false;}@Overridepublic boolean mouseMoved(int arg0, int arg1) {// TODO Auto-generated method stubreturn false;}@Overridepublic boolean keyUp(int arg0) {// TODO Auto-generated method stubreturn false;}@Overridepublic boolean keyTyped(char arg0) {// TODO Auto-generated method stubreturn false;}@Overridepublic boolean keyDown(int arg0) {// TODO Auto-generated method stubreturn false;}};InputMultiplexer inputMultiplexer = new InputMultiplexer();//用来处理多个InputProcessor的情况inputMultiplexer.addProcessor(inputProcessor);inputMultiplexer.addProcessor(inputProcessor2);Gdx.input.setInputProcessor(inputMultiplexer);//这一句前往别漏了}@Overridepublic void dispose() {// TODO Auto-generated method stub}@Overridepublic void pause() {// TODO Auto-generated method stub}@Overridepublic void render() {Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);}@Overridepublic void resize(int arg0, int arg1) {// TODO Auto-generated method stub}@Overridepublic void resume() {// TODO Auto-generated method stub}}


三、效果图
1)当第一个InputProcessor中的touchUp和touchDown方法的返回值为false的时候,logcat中输出的结果如下:


2)当第一个InputProcessor中的touchUp和touchDown方法的返回值为true的时候,logcat中输出的结果如下:


四、源码下载
http://download.csdn.net/detail/caihongshijie6/7040691



1 1
原创粉丝点击