2015-08-27 233 views
-2

今天早上我開始研究這個遊戲,直到遇到這個問題時我才確定:每當我按下空間時我都會下去。請幫忙,我已經在這裏一個小時了。我已經嘗試將>切換到<,但隨後它剛剛啓動。我是否與Y軸混淆或者有什麼問題?謝謝,這是我的代碼:似乎是跳過代碼?

using Microsoft.Xna.Framework; 
using Microsoft.Xna.Framework.Graphics; 
using Microsoft.Xna.Framework.Input; 

namespace Game1 
{ 

public class Game1 : Game 
{ 
    GraphicsDeviceManager graphics; 
    SpriteBatch spriteBatch; 
    Texture2D player; 
    int playerX = 0; 
    int playerY = 459; 
    int ground = 480; 
    int jump = 2; 
    int jumplimit = 450; 

    public Game1() 
    { 
     graphics = new GraphicsDeviceManager(this); 
     Content.RootDirectory = "Content";    
    } 

    protected override void Initialize() 
    { 
     graphics.IsFullScreen = false; 
     graphics.PreferredBackBufferWidth = 640; 
     graphics.PreferredBackBufferHeight = 480; 
     graphics.ApplyChanges(); 

     base.Initialize(); 
    } 

    protected override void LoadContent() 
    { 
     KeyboardState newState = Keyboard.GetState(); 
     spriteBatch = new SpriteBatch(GraphicsDevice); 
     player = Content.Load<Texture2D>("astronaut_right"); 
     //ALWAYS PUT THE ABOVE FUNCTION IN THE LOADCONTENT FUNCTION!!!!^^^^ 
     //THAT'S WHAT I SPENT AN HOUR TRYING TO FIX 

    } 

    protected override void UnloadContent() 
    { 
     Content.Unload(); 
    } 

    protected override void Update(GameTime gameTime) 
    { 
     if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape)) 
      Exit(); 

      KeyboardState newState = Keyboard.GetState(); 

      if (newState.IsKeyDown(Keys.Left)) 
      { 
       playerX -= 1; 
       player = Content.Load<Texture2D>("astronaut_left"); 
      } 

      if (newState.IsKeyDown(Keys.Right)) 
      { 
       playerX += 1; 
       player = Content.Load<Texture2D>("astronaut_right"); 
      } 

      if (newState.IsKeyDown(Keys.Up)) 
      { 
       playerY -= 0; 
      } 

      if (newState.IsKeyDown(Keys.Down)) 
      { 
       playerY += 0; 
      } 

      if (newState.IsKeyDown(Keys.Space)) 
      { 
       if (player.Bounds.Bottom > jumplimit) 
       { 
        playerY -= jump; 
       } 
       else 
       { 
        playerY += jump 
       } 
      } 

     base.Update(gameTime); 
    } 

    protected override void Draw(GameTime gameTime) 
    { 
     GraphicsDevice.Clear(Color.CornflowerBlue); 
     spriteBatch.Begin(); 

     spriteBatch.Draw(player, new Vector2(playerX, playerY), Color.White); 

     spriteBatch.End(); 
     base.Draw(gameTime); 
    } 
} 
} 
+0

爲什麼比較'jumplimit'與'player.Bounds.Bottom'而不是'playerY'? –

+0

_「我遇到了這個問題:每當我按下空間時我都會下去。」_ - 嗯?什麼? – MickyD

+0

我做到了這一點,當我按空間時,只要精靈的底部低於極限,我的球員就會上升 – Christos

回答

0

你檢查上player.Bounds.Bottomjumplimit但在更新playerY

也許你打算這樣做?

if (playerY > jumplimit) 
{ 
    playerY -= jump; 
} 
else 
{ 
    playerY += jump; 
}