1

スプライトをジャンプさせるのに非常に苦労しています。これまでのところ、「W」を 1 回タップすると、スプライトが一定の速度で上向きに送信されるコードのセクションがあります。ジャンプを開始してから、スプライトを特定の時間または高さで地面に戻せるようにする必要があります。ある種の重力をシミュレートするために、スプライトには一定の速度 2 のプルもあります。

// Walking = true when there is collision detected between the sprite and ground

if (Walking == true)
    if (keystate.IsKeyDown(Keys.W))
        {
            Jumping = true;
        }

if (Jumping == true)
    {
        spritePosition.Y -= 10;
    }

アイデアや助けをいただければ幸いですが、可能であれば、投稿されたコードの修正版を希望します。

4

3 に答える 3

2

あなたがやっているように 10 の一定速度とは対照的に、インパルスをスプライトに適用する必要があります。あなたがやろうとしていることについては、ここに良いチュートリアルがあります。

于 2012-12-04T11:03:08.347 に答える
1
    public class Player
    {
      private Vector2 Velocity, Position;
      private bool is_jumping; 
      private const float LandPosition = 500f; 

      public Player(Vector2 Position)
      {
         this.Position = new Vector2(Position.X, LandPosition); 
         Velocity = Vector2.Zero;
         is_jumping = false; 
      }
      public void UpdatePlayer(Gametime gametime, KeyboardState keystate, KeyboardState previousKeyBoardState)
      {
       if (!is_jumping)
         if (keystate.isKeyDown(Keys.Space) && previousKeyBoardState.isKeyUp(Keys.Space))
          do_jump(10f); 
       else
       {
        Velocity.Y++; 
        if (Position.Y >= LandPosition)
           is_jumping = false; 
       } 
       Position += Velocity; 

     }

     private void do_jump(float speed)
     {
            is_jumping = true; 
            Velocity = new Vector2(Velocity.X, -speed); 
     }
   }

疑似コードといくつかの実際のコードを少し組み合わせた楽しいものです。最初に含めなかった変数を追加するだけです。

また、Stack Overflow Physics もチェックしてください ;) ゲームの成功を祈っています。

編集:これで完了です。どうなるか教えてください。

于 2012-12-04T11:19:18.403 に答える
1

私はこのようなことをします...

const float jumpHeight = 60F; //arbitrary jump height, change this as needed
const float jumpHeightChangePerFrame = 10F; //again, change this as desired
const float gravity = 2F;
float jumpCeiling;
bool jumping = false;

if (Walking == true)
{
    if (keystate.IsKeyDown(Keys.W))
    {
        jumping = true;
        jumpCeiling = (spritePosition - jumpHeight);
    }
}

if (jumping == true)
{
    spritePosition -= jumpHeightChangePerFrame;
}

//constant gravity of 2 when in the air
if (Walking == false)
{
    spritePosition += gravity;
}

if (spritePosition < jumpCeiling)
{
    spritePosition = jumpCeiling; //we don't want the jump to go any further than the maximum jump height
    jumping = false; //once they have reached the jump height we want to stop them going up and let gravity take over
}
于 2012-12-04T11:05:01.543 に答える