前言
上节我们已经学会使用 Texture 和 SpriteBatch 来构建图形界面。但是,在窗口上只是显示了一张张静态纹理而已,没有任何与用户的互动,为了响应用户输入,我们需要使用到com.bdalogic.gdx.Gdx.input
。
Gdx.input 用户输入响应
libGDX提供了响应用户输入的一系列方法,包含在 Gdx.input 中:
- isTouched()-boolean : 返回用户是否鼠标(手指)按下
- isJustTouched()-boolean : 返回用户是否按下并松开(点击)
- isKeyPressed(int key)-boolean : 返回用户是否按下key键,传入Input.Keys的常量
- isJustKeyPressed(int key)-boolean : 返回用户是否按下并松开(点击)key键
- getX()-int : 返回用户最后一次按下的x坐标
- getY()-int : 返回用户最后一次按下的y坐标
使用方法很简单:
if(Gdx.input.isJustTouched())jump();
if(Gdx.input.isTouched())up();
if(Gdx.input.isKeyPressed(Keys.D))move();
if(Gdx.input.isJustKeyPressed(Keys.W))jump();
InputProcessor 输入接口
不止可以使用 Gdx.input 响应输入事件,还可以通过实现com.badlogic.gdx.InputProcessor
接口来响应更多事件。这里参考了宋志辉的博客点此
InputProcessor 接口包含以下方法(都返回 boolean ):
- touchDown(int x,int y,int pointer,int button) : 当鼠标(手指)按下时调用(只调用一次), x 和 y 是按下的坐标
- touchUp(int x,int y,int pointer,int button) : 当松开时调用, x 和 y 是刚刚按下的坐标
- tounchDragged(int x,int y,int pointer) : 当拖动(按下并移动)时调用
- mouseMoved(int x,int y) : 当鼠标在屏幕上移动而不按下时持续调用
- scrolled(int amount) : 当鼠标滚轮滚动时持续调用, amount 表示滚轮滚动的方向,-1为前,1为后
- keyDown(int keycode) : 当键盘按下时调用(只调用一次),keycode 是按下的键的 code
- keyUp(int keycode) : 当键盘松开时调用
- keyTyped(int keycode) : 同keyDown() 但持续调用
使用
通过使用com.badlogic.gdx.Gdx.input.setInputProcessor(InputProcessor inputProcessor)
来使用 InputProcessor :
Gdx.input.setInputProcessor(inputProcessor);
InputAdapter 空实现类
com.badlogic.gdx.InputAdapter
类继承了 InputProcessor 接口,并将所有方法空实现并返回 false ,你可以使用它更灵活地响应输入。
InputMultiplexer 响应输入优先级
有时候,程序会有多个 InputProcessor ,而它们之间都会有一个优先级,这时可以使用com.badlogic.gdx.InputMultiplexer
类来管理优先级:
//创建一个 InputMultiplexer 对象,以添加的顺序执行,优先级高的先执行,返回true则执行优先级下一级的方法,因此类推,直到返回false
InputMultiplexer inputMultiplexer=new InputMultiplexer(inputProcessor1,inputProcessor2);
//向后添加一个 InputProcessor
inputMultiplexer.addInputProcessor(inputProcessor3);
//追加优先级
inputMultiplexer.addInputProcessor(10,inputProcessor4);
//删除 InputProcessor
inputMultiplexer.removeInputProcessor(inputProcessor1);
//使用 inputMultiplexer
Gdx.input.setInputProcessor(inputMultiplexer);
尝试
接下来的实例将完成一个响应输入并上升的小火箭程序:
下载图片,命名为hj.png,并复制到assets文件夹
package com.libGDX.test;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputAdapter;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
public class Test extends ApplicationAdapter {
private SpriteBatch batch;
private Texture hj;
private int y;
@Override
public void create() {
batch=new SpriteBatch();
hj=new Texture(Gdx.files.internal("hj.png"));
//设置 InputProcessor
Gdx.input.setInputProcessor(new InputAdapter() {
@Override
public boolean keyTyped(char character) {
//如果按下w,小火箭的y坐标增加
if(character=='w') {
y+=1000*Gdx.graphics.getDeltaTime();
}
return false;
}
});
}
@Override
public void render() {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
batch.begin();
//指定坐标和大小
batch.draw(hj, 150, y,50,50);
batch.end();
}
@Override
public void dispose(){
hj.dispose();
batch.dispose();
}
}
运行结果:
不出所料,小火箭开始断断续续飞向天空。