2012-01-25 9 views
2

2dプラットフォームをJavaで作成していますが、何らかの理由でプレイヤーがプラットフォームに向かってジャンプすると、時にはスタックになってしまいます。ここでは、問題の画像は、次のとおりです。2dプラットフォームでプレイヤーがブロックに詰まることがあります

enter image description here

ご覧のように、私は、プラットフォームの上にジャンプを作っていたが、それは捕まってしまいました。

if (guy.getJumpState() == false) { 
    if (canExecuteMovement(0, 8)) { 

     ... 

     onGround = false; 
     if (guy.getY() > this.getParent().getHeight()/2 - 100) { 
      // if you are in the middle, move the platforms. 
      for (int i = 0; i < platformCount; i++) { 
       if (platform[i].getVisibility() == true) { 
        platform[i].setY(platform[i].getY() - 8); 
       } 
      } 
     } else { 
      // or just move the guy if not. 
      guy.moveY(8); 
     } 
    } else { 
     onGround = true; 
    } 
} else { 
    if (canExecuteMovement(0, -12)) { 

     if (guy.getY() < this.getParent().getHeight()/2 - 100) { 
      // if you are in the middle, move the platforms. 
      for (int i = 0; i < platformCount; i++) { 
       if (platform[i].getVisibility() == true) { 
        platform[i].setY(platform[i].getY() + 12); 
       } 
      } 
     } else { 
      // or just move the guy if not. 
      guy.moveY(-12); 
     } 
     jumpCount++; 
     if (jumpCount >= 20) { 
      jumpCount = 0; 
      guy.setJumpState(false); 
     } 
    } else { 
     jumpCount = 0; 
     guy.setJumpState(false); 
    } 
} 

コード左右に移動させる:

if (guy.getDirection() == "left") { 
      if (canExecuteMovement(-4, 0)) { 
       if (guy.getX() < this.getParent().getWidth()/2) { 
        // if you are in the middle, move the platforms. 
        for (int i = 0; i < platformCount; i++) { 
         if (platform[i].getVisibility() == true) { 
          platform[i].setX(platform[i].getX() + 4); 
         } 
        } 
       } else { 
        // or just move the guy if not. 
        guy.moveX(-4); 
       } 
      } 
     } else if (guy.getDirection() == "right") { 
      if (canExecuteMovement(4, 0)) { 
       if (guy.getX() > this.getParent().getWidth()/2) { 
        // if you are in the middle, move the platforms. 
        for (int i = 0; i < platformCount; i++) { 
         if (platform[i].getVisibility() == true) { 
          platform[i].setX(platform[i].getX() - 4); 
         } 
        } 
       } else { 
        // or just move the guy if not. 
        guy.moveX(4); 
       } 
      } 
     } 

そして、ここではcanExecuteMovement機能です:

private boolean canExecuteMovement(int xChange, int yChange) { 
    int projectedX = guy.getX() + xChange; 
    int projectedY = guy.getY() + yChange; 
    Rectangle projectedBounds = new Rectangle(projectedX, projectedY, guy.getWidth(), guy.getHeight()); 
    for (int i = 0; i < platformCount; i++) { 
     if (projectedBounds.intersects(platform[i].getBounds()) && platform[i].getVisibility() == true) { 
      return false; 
     } 
    } 
    return true; 
} 

私は本当にここで

が落下/ジャンプのための私のコリソンコードですこの問題にどのように取り組むべきかわからない、うまくいけば私はいくつかの啓発を得ることができます。

答えて

5

xyの許可/拒否の移動コードを個別に追跡する必要があります。起こっていることは、ユーザがそれ以上の権利を移すことができない場合、コードが彼を完全に止めるということです。しかし、彼はまだ上または下に移動することができるはずです。

は、新しい動きベクトルを割り当てているとき、それは個別にxyに移動する能力をチェックし、個別にxyを設定するように、あなたのコード内で条件文を分割を検討してください。

+0

私はcanMoveYとcanMoveXの2つの機能を持っているはずですか?そして、これらの関数はYとXをそれぞれチェックするだけです。 – Qasim

+0

である。あなたのコードは次のようになります: 'if(canMoveY){moveY(); } if(canMoveX){moveX();} } '(しかし、もっとコメントしたいと思いますが) – Tim

+0

私はそれを取ることができますか? – Tim

関連する問題