preface

In the last section we learned how to use Texture and SpriteBatch to build a graphical interface. But, on the window just shows the static texture ones, without any interaction with the user, in order to respond to user input, we need to use to com. The bdalogic. Etf has. Etf has. Input.

Gdx.input User input response

LibGDX provides a set of methods in response to user input, contained in gdx.input:

  • IsTouched ()- Boolean: Returns whether the user is pressing the mouse (finger)
  • IsJustTouched ()- Boolean: Returns whether the user pressed and released (click)
  • IsKeyPressed (int key)-boolean: Returns whether the user pressed the key key, passing in the constant input.keys
  • IsJustKeyPressed (int key)- Boolean: Returns whether the user pressed and released the key key
  • GetX ()-int: returns the x coordinate of the user’s last press
  • GetY ()-int: returns the y coordinate of the user’s last press

How to use it is simple:

if(Gdx.input.isJustTouched())jump();
if(Gdx.input.isTouched())up();
if(Gdx.input.isKeyPressed(Keys.D))move();
if(Gdx.input.isJustKeyPressed(Keys.W))jump();
Copy the code

InputProcessor Input interface

More than you can use the etf has. Input response input events, also can be realized through com. Badlogic. Etf has. More InputProcessor interface in response to events. Here is a reference to Song Zhihui’s blog

The InputProcessor interface contains the following methods (all return Boolean) :

  • TouchDown (int x,int y,int pointer,int button) : Called when the mouse (finger) is pressed (only once), x and y are the coordinates of the pressed
  • TouchUp (int x,int y,int pointer,int button) : Called when released, x and y are the coordinates just pressed
  • TounchDragged (int x,int y,int pointer) : Called when dragged (pressed and moved)
  • MouseMoved (int X,int Y) : Continuously called when the mouse moves over the screen without pressing down
  • Scrolled (int amount) : continuously called when the mouse wheel rolls, amount represents the direction of the wheel rolling, -1 is front, 1 is back
  • KeyDown (int KeyCode) : Called when the keyboard is pressed (only once), keycode is the code of the key being pressed
  • KeyUp (int Keycode) : called when the keyboard is released
  • KeyTyped (int keycode) : same as keyDown() but continuously called

use

Through the use of com. Badlogic. Etf has. Etf has. Input the setInputProcessor (InputProcessor InputProcessor) to use the InputProcessor:

Gdx.input.setInputProcessor(inputProcessor);
Copy the code

InputAdapter Empty implementation class

. Com. Badlogic. Etf has InputAdapter class inherits the InputProcessor interface, and will be all empty implementation and returns false, you can use it more flexibility to respond to the input.

InputMultiplexer response input priority

Sometimes, the program will have multiple InputProcessor, but between them there will be a priority, then can use com. Badlogic. Etf has. InputMultiplexer class management priority:

// Create an InputMultiplexer object and execute it in order of addition. Return true and execute the method at the lower priority, and so on until return false
InputMultiplexer inputMultiplexer=new InputMultiplexer(inputProcessor1,inputProcessor2);
// Add an InputProcessor backwards
inputMultiplexer.addInputProcessor(inputProcessor3);
// Append the priority
inputMultiplexer.addInputProcessor(10,inputProcessor4);
/ / delete the InputProcessor
inputMultiplexer.removeInputProcessor(inputProcessor1);
/ / use inputMultiplexer
Gdx.input.setInputProcessor(inputMultiplexer);
Copy the code

try

The next example completes a small rocket program that responds to input and rises:

Download the image, name it hj.png, and copy it to the assets folder

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(a) {
		batch=new SpriteBatch();
		hj=new Texture(Gdx.files.internal("hj.png"));
                / / set the InputProcessor
		Gdx.input.setInputProcessor(new InputAdapter() {
			@Override
			public boolean keyTyped(char character) {
                                // If you press W, the y coordinate of the small rocket increases
				if(character=='w') {
                                    y+=1000*Gdx.graphics.getDeltaTime();
				}
				return false; }}); }@Override
	public void render(a) {
		Gdx.gl.glClearColor(1.1.1.1);
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
		batch.begin();
                // Specify coordinates and sizes
		batch.draw(hj, 150, y,50.50);
		batch.end();
	}
        @Override
        public void dispose(a){ hj.dispose(); batch.dispose(); }}Copy the code

Running results:

As expected, the little rocket began to stutter into the sky.