事件處理允許您獲得關(guān)于用戶輸入的更細粒度和序列信息。事件處理提供了一種方法來實現(xiàn)與用戶界面的交互,其中非常重要的特定輸入序列,例如用戶單擊按鈕產(chǎn)生的按下/松開。這樣的交互很難用輪詢實現(xiàn)。
輸入處理器
事件處理是使用觀察者模式完成的。首先我們要實現(xiàn)一個監(jiān)聽器接口稱為InputProcessor:
public class MyInputProcessor implements InputProcessor {
public boolean keyDown (int keycode) {
return false;
}
public boolean keyUp (int keycode) {
return false;
}
public boolean keyTyped (char character) {
return false;
}
public boolean touchDown (int x, int y, int pointer, int button) {
return false;
}
public boolean touchUp (int x, int y, int pointer, int button) {
return false;
}
public boolean touchDragged (int x, int y, int pointer) {
return false;
}
public boolean mouseMoved (int x, int y) {
return false;
}
public boolean scrolled (int amount) {
return false;
}
}
前三個方法允許你去監(jiān)聽鍵盤事件:
keyDown(): 當(dāng)一個物理按鍵被按下時被調(diào)用,并返回按鍵代碼,你可以在Keys發(fā)現(xiàn)更多的常量.
keyUp(): 當(dāng)一個物理按鍵被釋放,并返回按鍵代碼.
keyTyped(): 有鍵盤輸入產(chǎn)生的Unicode字符,這個可以用來實現(xiàn)文本輸入等用戶界面操作相關(guān)的內(nèi)容。
接下來的五個方法報告了鼠標事件:
touchDown(): 當(dāng)觸摸屏幕或者鼠標被按下時調(diào)用,報告當(dāng)前按下的坐標位置,以及鼠標按鍵,鼠標按鍵默認總是返回左鍵(Buttons.LEFT).
touchUp():當(dāng)觸摸屏幕或者鼠標被釋放時調(diào)用,報告當(dāng)前按下的坐標位置,以及鼠標按鍵,鼠標按鍵默認總是返回左鍵 (Buttons.Left).
touchDragged(): 當(dāng)手指/鼠標在屏幕拖動時調(diào)用.并報告坐標及指針索引.當(dāng)鼠標/手指在屏幕拖動時,此時無法通過該方法知道當(dāng)前按下了哪個鍵.你可以使用useGdx.input.isButtonPressed()方法來判斷是非按下了特定的鍵.
mouseMoved(): 當(dāng)沒有鼠標按鍵被按下,但是移動鼠標時調(diào)用.這種情形只會在桌面應(yīng)用上出現(xiàn).
events.
scrolled(): 當(dāng)鼠標的滾輪轉(zhuǎn)動,通過-1或1來報告滾輪的方向,這種情形只會在桌面應(yīng)用上出現(xiàn).
通過下面的InputMultiplexer,我們來了解為什么每個方法都返回一個Boolean類型的值.
一旦你實現(xiàn)了你的InputProcessor ,你必須告訴LibGDX你的InputProcessor 實現(xiàn),這樣,當(dāng)新的輸入事件來臨時,LibGDX才可以調(diào)用你的InputProcessor 處理用戶的輸入.
MyInputProcessor inputProcessor = new MyInputProcessor();
Gdx.input.setInputProcessor(inputProcessor);
這樣,所有的新的輸入事件都會被提交到MyInputProcessor中進行處理,在渲染線程調(diào)用ApplicationListener.render()之前,所有的事件都會被分發(fā)處理。
InputAdapter
InputAdapter 實現(xiàn)了InputProcessor 所有的方法,并且每個方法都返回false,你可以選擇繼承InputAdapter ,這樣你就可以只實現(xiàn)你所關(guān)心的方法.你也可以使用匿名內(nèi)部類:
Gdx.input.setInputProcessor(new InputAdapter () {
@Override
public boolean touchDown (int x, int y, int pointer, int button) {
// your touch down code here
return true; // return true to indicate the event was handled
}
@Override
public boolean touchUp (int x, int y, int pointer, int button) {
// your touch up code here
return true; // return true to indicate the event was handled
}
});
InputMultiplexer
有時候,你可能需要有多個InputProcessor 實例,以應(yīng)付不同的處理需求.比如你需要使用一個InputProcessor 來處理Welcome頁面,使用另外一個InputProcessor 來處理游戲邏輯。這種情況下你可以使用inputmultiplexer類來實現(xiàn)這個需求:
InputMultiplexer multiplexer = new InputMultiplexer();
multiplexer.addProcessor(new MyUiInputProcessor());
multiplexer.addProcessor(new MyGameInputProcessor());
Gdx.input.setInputProcessor(multiplexer);
任何新的輸入事件都會被InputMultiplexer 的MyUiInputProcessor優(yōu)先接收并處理。但是當(dāng)MyUiInputProcessor的方法返回False,那么InputMultiplexer 就會把事件接著往下傳給第二個InputProcessor ,也就是MyGameInputProcessor,通過這種機制,MyUiInputProcessor可以攔截它可以處理的。并傳遞不能處理的事件給MyGameInputProcessor進行處理.
連續(xù)輸入句柄示例
如果你想使用 InputProcessor 來移動你的演員類,you will notice that it will move only when the key is typed (or pressed with keydown).如果你想移動一個精靈,你可以為它添加一個標記:
public class Bob
{
boolean leftMove;
boolean rightMove;
...
updateMotion()
{
if (leftMove)
{
x -= 5 * Gdx.graphics.getDeltaTime();
}
if (rightMove)
{
x += 5 * Gdx.graphics.getDeltaTime();
}
}
...
public void setLeftMove(boolean t)
{
if(rightMove && t) rightMove = false;
leftMove = t;
}
public void setRightMove(boolean t)
{
if(leftMove && t) leftMove = false;
rightMove = t;
}
然后在您的處理器上:
...
@Override
public boolean keyDown(int keycode)
{
switch (keycode)
{
case Keys.LEFT:
bob.setLeftMove(true);
break;
case Keys.RIGHT:
bob.setRightMove(true);
break;
}
return true;
}
@Override
public boolean keyUp(int keycode)
{
switch (keycode)
{
case Keys.LEFT:
bob.setLeftMove(false);
break;
case Keys.RIGHT:
bob.setRightMove(false);
break;
}
return true;
}