2012-05-16 55 views
0

我正在創建一個簡單的XNA遊戲,您可以在其中移動該塊。但該塊沒有按鍵動作,我不知道爲什麼。爲什麼對象不能移動鍵盤右鍵/左鍵按XNA

這裏是我的總體代碼:

public class Game1 : Microsoft.Xna.Framework.Game 
{ 
    GraphicsDeviceManager graphics; 
    SpriteBatch spriteBatch; 
    Texture2D ball; 
    Texture2D bar; 
    Vector2 ballPos; 
    Vector2 barPos; 
    public Game1() 
    { 
     graphics = new GraphicsDeviceManager(this); 
     Content.RootDirectory = "Content"; 
     // Frame rate is 30 fps by default for Windows Phone. 
     TargetElapsedTime = TimeSpan.FromTicks(333333); 

     // Extend battery life under lock. 
     InactiveSleepTime = TimeSpan.FromSeconds(1); 
    } 

    /// <summary> 
    /// Allows the game to perform any initialization it needs to before starting to run. 
    /// This is where it can query for any required services and load any non-graphic 
    /// related content. Calling base.Initialize will enumerate through any components 
    /// and initialize them as well. 
    /// </summary> 
    protected override void Initialize() 
    { 
     // TODO: Add your initialization logic here 
     ballPos = new Vector2(10, 10); 
     barPos=new Vector2(10,GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width - 20); 

     base.Initialize(); 
    } 

    /// <summary> 
    /// LoadContent will be called once per game and is the place to load 
    /// all of your content. 
    /// </summary> 
    protected override void LoadContent() 
    { 
     ball = Content.Load<Texture2D>("ball"); 
     bar = Content.Load<Texture2D>("bar"); 

     // Create a new SpriteBatch, which can be used to draw textures. 
     spriteBatch = new SpriteBatch(GraphicsDevice); 

     // TODO: use this.Content to load your game content here 
    } 

    /// <summary> 
    /// UnloadContent will be called once per game and is the place to unload 
    /// all content. 
    /// </summary> 
    protected override void UnloadContent() 
    { 
     // TODO: Unload any non ContentManager content here 
    } 

    /// <summary> 
    /// Allows the game to run logic such as updating the world, 
    /// checking for collisions, gathering input, and playing audio. 
    /// </summary> 
    /// <param name="gameTime">Provides a snapshot of timing values.</param> 
    protected override void Update(GameTime gameTime) 
    { 
     // Allows the game to exit 
     if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) 
      this.Exit(); 

     UpdateBarLocation(); 
     // TODO: Add your update logic here 

     base.Update(gameTime); 
    } 
    private void UpdateBarLocation() 
    { 
     KeyboardState newState = Keyboard.GetState(); 

     if (newState.IsKeyDown(Keys.Right) && (barPos.Y < GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height)) 
      barPos.Y = barPos.Y + 5; 
     else if (newState.IsKeyDown(Keys.Left) && (barPos.Y > 2)) 
      barPos.Y = barPos.Y - 5; 
    } 

    /// <summary> 
    /// This is called when the game should draw itself. 
    /// </summary> 
    /// <param name="gameTime">Provides a snapshot of timing values.</param> 
    protected override void Draw(GameTime gameTime) 
    { 
     GraphicsDevice.Clear(Color.CornflowerBlue); 

     // TODO: Add your drawing code here 
     spriteBatch.Begin(); 
     spriteBatch.Draw(ball,ballPos, Color.White); 
     spriteBatch.Draw(bar, barPos, Color.White); 
     spriteBatch.End(); 

     base.Draw(gameTime); 
    } 
} 

你可以看到,在updateBarLocation功能我試圖舉槓鈴,但它不工作。

回答

0

屏幕的分辨率是多少?

這是線是模糊的

barPos=new Vector2(10,GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width - 20); 

第一個參數是x軸和第二爲y軸。 所以寬度通常在x軸上處理。 你確定你正在分配什麼是好嗎? 如果是,那麼屏幕上的哪個位置最初位於酒吧?

但塊沒有在按鍵上移動我不知道爲什麼。

你甚至沒有更新塊的位置