2013-04-03 24 views
-3

我最近問了一個問題,但還是不明白,我的意思是答案很好,但我只是不明白。我試圖讓球員跳起來的時間越長。還需要在高度上有一個最大值。新浪微博 - 持有更長時間跳高(示例附)

我有一個球員類的例子,我試圖添加這樣的功能。如果有人可以看看這個課程,請添加該功能,然後添加一些評論來幫助我,我非常感謝。 - 謝謝。

Player.cs

namespace Jumping 

{

class Player 
{ 
    public Texture2D Texture; 

    public Vector2 Velocity; 
    public Vector2 Position; 
    public float ground; 
    private float Speed; 

    private Rectangle screenBound; 

    public bool isJumping; //are we jumping or not 
    public bool goingUp; //if going up or not 

    public float u; //initial velocity 
    private float jumpU; 
    private float g; //gravity 
    public float t; //time 

    private KeyboardState prevKB; 

    public Player(Texture2D Texture, Vector2 Position, float Speed, Rectangle screenBound) 
    { 
     this.Texture = Texture; 
     this.Position = Position; 
     ground = Position.Y; 
     this.Speed = Speed; 
     this.screenBound = screenBound; 
     Velocity = Vector2.Zero; 
     isJumping = goingUp = false; 
     jumpU = 2.5f; 
     g = -9.8f; 
     t = 0; 
    } 

    public void Update(GameTime gameTime) 
    { 

     Position.X += (Velocity.X * Speed); 
     //Set the Y position to be subtracted so that the upward movement would be done by decreasing the Y value 
     Position.Y -= (Velocity.Y * Speed); 

     goingUp = (Velocity.Y > 0); 

     // TODO: Add your update logic here 
     if (isJumping == true) 
     { 
      //motion equation using velocity: v = u + at 
      Velocity.Y = (float)(u + (g * t)); 
      //Increase the timer 
      t += (float)gameTime.ElapsedGameTime.TotalSeconds; 
     } 
     if (isJumping == true && Position.Y > screenBound.Height - Texture.Height) 
     { 
      Position.Y = ground = screenBound.Height - Texture.Height; 
      Velocity.Y = 0; 
      isJumping = false; 
      t = 0; 
     } 

     if (Position.X < 0) 
     { 
      //if Texture touches left side of the screen, set the position to zero and the velocity to zero. 
      Position.X = 0; 
      Velocity.X = 0; 
     } 
     else if (Position.X + Texture.Width > screenBound.Width) 
     { 
      //if Texture touches left side of the screen, set the position to zero and the velocity to zero. 
      Position.X = screenBound.Width - Texture.Width; 
      Velocity.X = 0; 
     } 
     if (Position.Y < 0) 
     { 
      //if the Texture touches the top of the screen, reset the timer and set the initial velocity to zero. 
      Position.Y = 0; 
      t = 0; 
      u = 0; 
     } 
    } 

    public void Input(KeyboardState keyState) 
    { 
     if (keyState.IsKeyDown(Keys.Space) && (isJumping == false || Position.Y == ground)) 
     { 
      isJumping = true; 
      u = jumpU; 
     } 
     if (keyState.IsKeyDown(Keys.Left) && !keyState.IsKeyDown(Keys.Right)) 
     { 
      if (keyState.IsKeyDown(Keys.A)) 
      { 
       if (Velocity.X > -2.0f) 
       Velocity.X -= (1.0f/10); 
      } 
      else if (Velocity.X > -1.0f) 
      { 
       Velocity.X -= (1.0f/10); 
      } 
      else 
      { 
       Velocity.X = -1.0f; 
      } 
     } 
     else if (!keyState.IsKeyDown(Keys.Left) && keyState.IsKeyDown(Keys.Right)) 
     { 
      if (keyState.IsKeyDown(Keys.A)) 
      { 
       if (Velocity.X < 2.0f) 
       Velocity.X += (1.0f/10); 
      } 
      else if (Velocity.X < 1.0f) 
      { 
       Velocity.X += (1.0f/10); 
      } 
      else 
      { 
       Velocity.X = 1.0f; 
      } 
     } 
     else 
     { 
      if (Velocity.X > 0.05 || Velocity.X < -0.05) 
       Velocity.X *= 0.90f; 
      else 
       Velocity.X = 0; 
     } 

     prevKB = keyState; 
    } 

    public void Fall() 
    { 
     t = 0; 
     u = 0; 
    } 

    public void Draw(SpriteBatch spriteBatch) 
    { 
     spriteBatch.Draw(Texture, new Rectangle((int)Position.X, (int)Position.Y, Texture.Width, Texture.Height), Color.White); 
    } 
} 

}

+0

同意,不要開始另一個問題。 – Clint

+0

所以我決定花時間幫助你解決最後一個問題,但是你只是重複一下你最後的回答沒有幫助嗎?這將如何讓你到達任何地方?如果您對我最後一個問題的回答發表評論,我可能會幫助解決您的問題。 – Cyral

回答

2

爲了幫助你,不要混淆你,我會建議的做法,但它必須在稍後重構。

在你的輸入法,將其添加:

public void Input(KeyboardState keyState) 
{ 
    newIsJumping = keyState.IsKeyDown(Keys.Space); 

    ... 

而在你的更新方法,使用它:

// TODO: Add your update logic here 
if (isJumping == true) 
{ 
    if (newIsJumping) u += 0.5f; // use some constant here 

    ... 

我希望它可以解決你的問題。

+0

Thankyou的幫助,但我已經將此添加到player.cs中,並且出現錯誤: 'newIsJumping'的名稱在當前上下文中不存在。我該如何聲明newIsJumping爲? – user1631026

+1

我認爲它和isJumping是一樣的。所以一個布爾 – phadaphunk

+0

謝謝robson roase | PhaDaPhunk top lads。 – user1631026