Input module
The input module can be accessed either through Gdx.getInput() or by using the
shortcut variable Gdx.input.
In order to receive and handle input properly, you should always implement the
InputProcessor interface and set it as the global handler for input in Libgdx by
calling Gdx.input.setInputProcessor().
Reading the keyboard/touch/mouse input
Query the system for the last x or y coordinate in the screen coordinates where the
screen origin is at the top-left corner by calling either Gdx.input.getX() or Gdx.
input.getY().
• To find out if the screen is touched either by a finger or by mouse, call Gdx.
input.isTouched()
• To find out if the mouse button is pressed, call Gdx.input.
isButtonPressed()
• To find out if the keyboard is pressed, call Gdx.input.isKeyPressed()
Reading the accelerometer
Query the accelerometer for its value on the x axis by calling Gdx.input.
getAccelerometerX(). Replace the X in the method's name with Y or Z to query the
other two axes. Be aware that there will be no accelerometer present on a desktop,
so Libgdx always returns 0.
Starting and canceling vibrator
On Android, you can let the device vibrate by calling Gdx.input.vibrate().
A running vibration can be cancelled by calling Gdx.input.cancelVibrate().
Catching Android soft keys
You might want to catch Android's soft keys to add an extra handling code for them.
If you want to catch the back button, call Gdx.input.setCatchBackKey(true).
If you want to catch the menu button, call Gdx.input.setCatchMenuKey(true).
On a desktop where you have a mouse pointer, you can tell Libgdx to catch
it so that you get a permanent mouse input without having the mouse ever
leave the application window. To catch the mouse cursor, call Gdx.input.
setCursorCatched(true).
The files module
The files module can be accessed either through Gdx.getFiles() or by using the
shortcut variable Gdx.files.
Getting an internal file handle
You can get a file handle for an internal file by calling Gdx.files.internal().
An internal file is relative to the assets folder on the Android and WebGL
platforms. On a desktop, it is relative to the root folder of the application.
Getting an external file handle
You can get a file handle for an external file by calling Gdx.files.external().
An external file is relative to the SD card on the Android platform. On a
desktop, it is relative to the user's home folder. Note that this is not available
for WebGL applications.
The network module
The network module can be accessed either through Gdx.getNet() or by using the
shortcut variable Gdx.net.
HTTP GET and HTTP POST
You can make HTTP GET and POST requests by calling either Gdx.net.httpGet()
or Gdx.net.httpPost().
Cross-platform Development – Build Once, Deploy Anywhere
[ 58 ]
Client/server sockets
You can create client/server sockets by calling either Gdx.net.newClientSocket()
or Gdx.net.newServerSocket().
Opening a URI in a web browser
To open a Uniform Resource Identifier (URI) in the default web browser, call Gdx.
net.openURI(URI).
The input module can be accessed either through Gdx.getInput() or by using the
shortcut variable Gdx.input.
In order to receive and handle input properly, you should always implement the
InputProcessor interface and set it as the global handler for input in Libgdx by
calling Gdx.input.setInputProcessor().
Reading the keyboard/touch/mouse input
Query the system for the last x or y coordinate in the screen coordinates where the
screen origin is at the top-left corner by calling either Gdx.input.getX() or Gdx.
input.getY().
• To find out if the screen is touched either by a finger or by mouse, call Gdx.
input.isTouched()
• To find out if the mouse button is pressed, call Gdx.input.
isButtonPressed()
• To find out if the keyboard is pressed, call Gdx.input.isKeyPressed()
Reading the accelerometer
Query the accelerometer for its value on the x axis by calling Gdx.input.
getAccelerometerX(). Replace the X in the method's name with Y or Z to query the
other two axes. Be aware that there will be no accelerometer present on a desktop,
so Libgdx always returns 0.
Starting and canceling vibrator
On Android, you can let the device vibrate by calling Gdx.input.vibrate().
A running vibration can be cancelled by calling Gdx.input.cancelVibrate().
Catching Android soft keys
You might want to catch Android's soft keys to add an extra handling code for them.
If you want to catch the back button, call Gdx.input.setCatchBackKey(true).
If you want to catch the menu button, call Gdx.input.setCatchMenuKey(true).
On a desktop where you have a mouse pointer, you can tell Libgdx to catch
it so that you get a permanent mouse input without having the mouse ever
leave the application window. To catch the mouse cursor, call Gdx.input.
setCursorCatched(true).
The files module
The files module can be accessed either through Gdx.getFiles() or by using the
shortcut variable Gdx.files.
Getting an internal file handle
You can get a file handle for an internal file by calling Gdx.files.internal().
An internal file is relative to the assets folder on the Android and WebGL
platforms. On a desktop, it is relative to the root folder of the application.
Getting an external file handle
You can get a file handle for an external file by calling Gdx.files.external().
An external file is relative to the SD card on the Android platform. On a
desktop, it is relative to the user's home folder. Note that this is not available
for WebGL applications.
The network module
The network module can be accessed either through Gdx.getNet() or by using the
shortcut variable Gdx.net.
HTTP GET and HTTP POST
You can make HTTP GET and POST requests by calling either Gdx.net.httpGet()
or Gdx.net.httpPost().
Cross-platform Development – Build Once, Deploy Anywhere
[ 58 ]
Client/server sockets
You can create client/server sockets by calling either Gdx.net.newClientSocket()
or Gdx.net.newServerSocket().
Opening a URI in a web browser
To open a Uniform Resource Identifier (URI) in the default web browser, call Gdx.
net.openURI(URI).
No comments:
Post a Comment