2014-01-28 18 views
3

我想創建一個移動應用程序,允許兩個玩家在同一臺設備上玩乒乓球。每個玩家將抓住設備的一端,並能夠在y軸上來回移動守門員。我正在開發使用Java w/LibGDX的遊戲,並且無法讓多個輸入工作。我可以這樣做,以便平臺註冊輸入是在左側還是右側(確定哪個球員正在移動),我可以用它來單獨移動每個球員,但我不能讓他們在同時。LIbGDX多人乒乓球有多個觸摸輸入

這是怎樣我現在有我的運動設置:

 PlayerPaddle playerOnePaddle = ((GameScreen) currentScreen).getPlayerOnePaddle(); 
     PlayerPaddle playerTwoPaddle = ((GameScreen) currentScreen).getPlayerTwoPaddle(); 
     Vector2 touchPos = new Vector2(Gdx.input.getX(), Gdx.input.getY() + playerOnePaddle.height/2); 

     if (Gdx.input.getX() < Gdx.graphics.getWidth()/2) 
     { 
      playerOnePaddle.pos.y = Gdx.graphics.getHeight() - touchPos.y; 
     } 
     if (Gdx.input.getX() > Gdx.graphics.getWidth()/2) 
     { 
      playerTwoPaddle.pos.y = Gdx.graphics.getHeight() - touchPos.y; 
     } 

這適用於單獨的輸入,這意味着我可以點擊屏幕的左側,移動左邊的球員,我可以挖掘的權屏幕並移動正確的玩家,但我無法同時移動每個玩家,這會打敗遊戲的重點。我需要具體的例子來說明如何實現這一點,因爲我對LibGDX輸入的經驗非常有限,在搜索後我無法找到任何正確的方法來做到這一點。我想過多線程的第二個輸入,但這隻會使代碼混亂,使邏輯不對稱

回答

3

在屏幕上的每一個觸摸INT指針給出。第一次觸摸將得到指針0,第二次將得到指針1.如果你觸摸,指針被釋放,它會被賦予下一次觸摸(它將始終佔用第一個空閒指針)。我建議你檢查的第5個球,以確保:

for (int i=0; i<5; i++){ 
    if (!Gdx.input.isTouched(i)) continue; 
    Vector2 touchPos = new Vector2(Gdx.input.getX(i), Gdx.input.getY(i) + playerOnePaddle.height/2); 
    if (Gdx.input.getX(i) < Gdx.graphics.getWidth()/2){ 
     playerOnePaddle.pos.y = Gdx.graphics.getHeight() - touchPos.y; 
    } 
    if (Gdx.input.getX(i) > Gdx.graphics.getWidth()/2){ 
     playerTwoPaddle.pos.y = Gdx.graphics.getHeight() - touchPos.y; 
    } 
} 

順便說一句,你應該使用一個攝像頭和unproject您的觸摸:

camera.unproject(touchPos.set(Gdx.input.getX(i), Gdx.input.getY(i), 0)); 

而且使用touchPos.xtouchPos.y爲你的觸摸。這樣它就可以在每個屏幕分辨率下工作。

+0

我很欣賞有關未投影相機的其他建議。謝謝。 – hasherr