【问题标题】:LibGDX + Box2D: justTouched multiplies my forcesLibGDX + Box2D:justTouched 倍增我的力量
【发布时间】:2015-03-14 20:25:43
【问题描述】:

现在我的代码如下:

if(Gdx.input.justTouched()) {
        if(cl.isPlayerOnGround()) {
            playerBody.applyForceToCenter(0, B2DVars.jumpForce, true);
        }
    }

它在大多数情况下都有效,但是如果您在桌面上同时单击两个鼠标按钮,或者在 android 上同时单击多点触控,则 jumpForce 会成倍增加。我需要忽略所有点击/点击,但首先要忽略玩家再次接触地面。

(cl 是联系人监听器。)

【问题讨论】:

    标签: java android input libgdx box2d


    【解决方案1】:

    有很多方法可以解决这个问题,但在我看来,解决这个问题的最佳方法是限制可以应用 jumpForce 的频率。这可能会避免其他问题。

    //class var
    float lastJumpForceTime = 0.5f; // start at 0.5f to enable jumping from the start
    float jumpForceDelay = 0.5f;  // delay between jumps, 0.5f = half second
    
    
    //method that calls your code
    //delta time is the time since last update LibGDX provides this but you will need to pass it
    public void yourMethod(float deltaTime, ...){
      lastJumpForceTime += deltaTime;  // update last jump time
      // check if player is on ground and if time since last jump is > delay
      if(cl.isPlayerOnGround() && lastJumpForceTime >= jumpForceDelay){
          lastJumpForceTime = 0.0f;
          playerBody.applyForceToCenter(0, B2DVars.jumpForce, true);
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-10-05
      • 2014-11-21
      • 1970-01-01
      • 1970-01-01
      • 2019-03-26
      • 1970-01-01
      • 2020-04-25
      • 2014-05-24
      相关资源
      最近更新 更多