2016-03-08 72 views
1

我正在與LibGDX一起玩遊戲,我正試圖弄清楚如何限制TouchEvent跳躍。LibGDX - 如何控制多次跳躍的觸摸事件

我嘗試:

if(player.b2body.getLinearVelocity().y > 3.5f){ 
      float currentVelocityY = player.b2body.getLinearVelocity().y; 
      player.b2body.applyLinearImpulse(new Vector2(0, (- currentVelocityY)), player.b2body.getWorldCenter(), true); 
    } 

我想減少對Y軸的速度,如果它弄巧成拙一定的價值。但這不起作用,就像我一直在觸摸屏幕一樣,角色會飛得更高。

我想在短時間內將touchEvent限制爲只跳兩次。

您的任何想法?

謝謝。

回答

5

解決方案1(限制每秒跳躍):

所以,你的角色跳躍上的觸下事件。你定義一個變量來存儲以毫秒爲單位的最後兩次:

long lastTap = System.currentTimeMillis(); 

而且水龍頭事件:

public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { 
    if(System.currentTimeMillis() - lastTap < 1000) 
    return true; 

    // your character jump code 

    lastTap = System.currentTimeMillis(); 
    return true; 

這應該打電話給你跳躍的代碼只有每秒一次(因爲如果在1000毫秒)不管你點擊多快。只需測試一下數字(我們每秒鐘抽2次,250 - 4秒等等)。

解決方案2(限制跳躍計數):

你定義一個變量來存儲多少次進行跳躍和最大跳數:

int jumps = 0; 
int maxJumps = 3; 

而且水龍頭事件:

public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { 
    // the limit is reached 
    if(jumps == maxJumps) 
    return true; 

    // your character jump code 

    jumps++; 
    return true; 

而你重置了你的render()方法或box2d交互偵聽器中的跳轉var時E身體落在:

if(player.b2body.getLinearVelocity().y == 0) 
    jumps = 0; 

現在,用戶將能夠做一個3次快速跳躍,然後他將不得不等待字符落在地上再跳一次。

溶液3(檢查抽頭上的力)

public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) { 
    if(player.b2body.getLinearVelocity().y > 3.5f) 
    return true; 

    // your character jump code 

    return true; 
} 

現在,用戶將能夠只要在y速度下3.5F跳。

+0

你好雅森,謝謝你的回答。只是回覆我注意到它,並會在今天晚些時候對它進行測試 - 目前我在辦公室。一旦測試就會接受你的答案! –

+0

我使用了maxJumps選項,它適用於我。我將它設置爲2,這是我想達到的。回答已接受並已提交。謝謝@Yasen! –