Programming Games for Android

Programming Games for Android 11: Input events InputAdapter

42 videos

238 minutes

To work with input events, libgdx has a class called InputProcessor. This class is controlled by libgdx at the entrance and It works like ApplicationAdapter: it is a class event. We write the code and let events libgdx the call when it deems timely. We do not check if any key is pressed, let whoever libgdx notify us that you have pressed a key.

This differentiates the system that we have seen in the previous video, polling, that if you are not pressing that key is not that code is executed and the game runs faster. In the case of InputProcessor what is that events will be unleashed in separate threads to not disturb the main code. The first thing I will do is create the input processor, and this is something that should be in a separate class,

Processor for example. My input processor must implement InputProcessor. It is an interface, so I obliged to implement all its methods. There are a lot of methods, we will see them gradually. Some are down and others are up. The down used when we begin to tap or touch the screen, and they are used up when we fail to do so. For example, keyDown is the event that triggers when we started pressing a key, for example when we 'K'

and leave the 'K' down but not release the finger. When you release your finger keyUp shooting. So we can detect when we started pressing and pressing. This can serve to make a game where you want the character forward while pressing the Right button and when the stop is released forward. Here we tell you to move around, and here we tell it to end. Similarly with touchdown and touchup we can do the same but with your finger.

Touchdown is triggered when you press your finger, and when he got up touchup. We have other interesting methods as mouseMoved, which tells us when it is moving the mouse. With this we can comproabr if the mouse is moving. This does not fire in mobile phones because you do not know where your finger is safe who we play. We have other as touchDragged to detect when we are dragging the mouse or finger on the screen, so that we can follow a

movement or make a gesture, or whatever. Or keyTyped that detects when we clicked a character, such as A or B. so that if we are writing letters we can detect faster. As you can see have a lot of parameters, so I do not want to get hit with it because there are many, but let's start for those who have to do with the touch, which is what interests us. First of all I will eliminate this, and as many will interest me change

InputProcessor by InputAdapter. Why? For as ApplicationAdapter, InputAdapter and any adapter it does is it implements interface for us, that is, it remains a Processor, but not implement the methods that we will not implement, and this is very convenient. We want to use TouchDown, then we use it. We want to use touchup, use it. We will not use KeyDown, leave it there and not used.

An important feature that perhaps you are wondering about the methods of InputProcessor is why all return boolean. This is because Boolean says if we have done something in this method, so that if we return false what we say is that you have not done anything. And that's okay because libgdx if another processor has to attend to, you can determine whether, if we have not done anything, use your own code to process the event, or to drop it

or call another processor ... that we will always make code on the touch, or the key or wherever they are going to do, we must return true for libgdx indicate that we have processed the event. TouchDown and touchup. They have four parameters here. Both the same well. screenY screenX and tell you where we played. pointer, finger indicates how we played. You will say with what finger? Yes, because the phones are all

multitouch day today, and you can click with several fingers at once, for example to make the zoom gesture. Or to make any other gesture, but usually they accept 4 or 5 fingers without problem. When we press the screen with a finger, that finger will be 0, and if while holding down the 0 press with another finger, that is the finger 1, and so until it reaches the limit, each screen has its own, because not all good screens.

In the desktop case, if we have a mouse button brings us back what we've mouse button clicked, as the left or the right. For test this I'm going to do is say 'you played at the position (screenX, screenY)' and 'have used the finger (pointer) and (button) button'. As I tried my event in a way, I return true just so you know I've done things. The only thing left is to connect it with the main game.

To find new courses, subscribe. There will be no spam, promised :)

About the Author

foto de jotajotavm
José Javier Villena

Biography: Analyst-Programmer in different languages. Tutor PREMIUM platforms recognized worldwide as CodigoFacilito. Editor Cristalab articles. My YouTube Channel is sponsored by the ANAYA and LaTostadora publisher. I like to explain in detail and give several examples for the avoidance of doubt.