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