(libgdx学习)InputProcessor InputMultiplexer

官方文档部分解释:

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;
	
	@Override
	public void create() {
		inputProcessor = new InputProcessor() {
			
			@Override
			public boolean touchUp(int arg0, int arg1, int arg2, int arg3) {
				System.out.println("inputProcessor--------->touchUp");
				
				return true;
			}
			
			@Override
			public boolean touchDragged(int arg0, int arg1, int arg2) {
				System.out.println("inputProcessor--------->touchDragged");
				
				return true;
			}
			
			@Override
			public boolean touchDown(int arg0, int arg1, int arg2, int arg3) {
				System.out.println("inputProcessor--------->touchDown");
				
				return true;
			}
			
			@Override
			public boolean scrolled(int arg0) {
				System.out.println("inputProcessor--------->scrolled");
				
				return true;
			}
			
			@Override
			public boolean mouseMoved(int arg0, int arg1) {
				System.out.println("inputProcessor--------->tmouseMoved");
				
				return true;
			}
			
			@Override
			public boolean keyUp(int arg0) {
				System.out.println("inputProcessor--------->keyUp");
				return true;
			}
			
			@Override
			public boolean keyTyped(char arg0) {
				
				System.out.println("inputProcessor--------->keyType");
				
				return true;
			}
			
			@Override
			public boolean keyDown(int arg0) {
				System.out.println("inputProcessor--------->keyDown");
				
				return true;
			}
		};
		
		inputProcessor2 = new InputProcessor() {
			
			@Override
			public boolean touchUp(int arg0, int arg1, int arg2, int arg3) {
				System.out.println("inputprocessor2---------->touchUp");
				
				return true;
			}
			
			@Override
			public boolean touchDragged(int arg0, int arg1, int arg2) {
				// TODO Auto-generated method stub
				return false;
			}
			
			@Override
			public boolean touchDown(int arg0, int arg1, int arg2, int arg3) {
				System.out.println("inputprocessor2------------->touchDown");
				
				return true;
			}
			
			@Override
			public boolean scrolled(int arg0) {
				// TODO Auto-generated method stub
				return false;
			}
			
			@Override
			public boolean mouseMoved(int arg0, int arg1) {
				// TODO Auto-generated method stub
				return false;
			}
			
			@Override
			public boolean keyUp(int arg0) {
				// TODO Auto-generated method stub
				return false;
			}
			
			@Override
			public boolean keyTyped(char arg0) {
				// TODO Auto-generated method stub
				return false;
			}
			
			@Override
			public boolean keyDown(int arg0) {
				// TODO Auto-generated method stub
				return false;
			}
		};
		
		InputMultiplexer inputMultiplexer = new InputMultiplexer();//用来处理多个InputProcessor的情况
		inputMultiplexer.addProcessor(inputProcessor);
		inputMultiplexer.addProcessor(inputProcessor2);
		
		Gdx.input.setInputProcessor(inputMultiplexer);//这一句前往别漏了
	}

	@Override
	public void dispose() {
		// TODO Auto-generated method stub

	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub

	}

	@Override
	public void render() {
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		
		
	}

	@Override
	public void resize(int arg0, int arg1) {
		// TODO Auto-generated method stub

	}

	@Override
	public 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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

帅气的东哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值