2015-08-27 122 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; 
}