2013-06-18 102 views
0

在我們保存代碼之前,代碼運行得非常好,並嘗試將其提交給我們的編程課程。我們相信我們有某種內存泄漏,但無法發現它。我們在保存代碼之前以及保存完好之前沒有對代碼進行任何更改,所以當我們保存它時出現了問題。以下是我們的更新方法中的所有代碼。我們有更新方法中每個困難的情況,這是我們認爲問題所在的地方。爲什麼我的XNA遊戲滯後?

//Easy game mode 
     case GameState.Easy: 
      //starts backgound music 
      MediaPlayer.Play(BackgroundMusic); 
      this.IsMouseVisible = false; 
      PlayerTwoSpeed = 5; 

      if (HitCount == PointsToWin) PlayerOneWins(); 
      if (HitCountEnemy == PointsToWin) PlayerTwoWins(); 

      //getting the keyboard state, so input can be detected 
      if (Keyboard.GetState().IsKeyDown(Keys.Down)) 
      { 
       if (POPBox.Y >= 373) 
       { 
        POPBox.Y += 0; 
       } 
       else 
       { 
        POPBox.Y += PlayersSpeed; 
       } 
      } 

      if (Keyboard.GetState().IsKeyDown(Keys.Up)) 
      { 
       if (POPBox.Y <= 0) 
       { 
        POPBox.Y += 0; 
       } 
       else 
       { 
        POPBox.Y += -PlayersSpeed; 
       } 
      } 
      // Ball limits 

      if (BallBox.Y <= 0) 
       VelocityY *= -1; 
      if (BallBox.Y >= 463) 
       VelocityY *= -1; 

      //Collision Detection (Runs this code if it hits the player one's paddle) 
      if (BallBox.Intersects(POPBox)) 
      { 
       //Used to deflect in different directions for some veriety 
       if (PlayersSpeed > 0) 
        VelocityY += 3; 
       if (PlayersSpeed < 0) 
        VelocityY -= 3; 
       VelocityX *= -1; 
       ShockerGenerator(); 

       //Stopping the no slope bug. If it wants to bounce perfectly straight, it is slightly shifty to fix that error. 

       if (VelocityY == 0) 
        VelocityY = VelocityY += 3; 
       if (VelocityX == 0) 
        VelocityX = VelocityX += 3; 

       //speed control 

       if (VelocityX > 10) 
        VelocityX = 10; 
       if (VelocityY > 10) 
        VelocityY = 10; 
      } 
      // Runs this code if the ball hits player two's paddle 
      if (BallBox.Intersects(PTPBox)) 
      { 
       VelocityX *= -1; 
       if (VelocityY == 0) 
        VelocityY = VelocityY += 3; 
       if (VelocityX == 0) 
        VelocityX = VelocityX += 3; 
      } 


      //Object a collision 

      if (BallBox.Intersects(ShocObjectARectangle)) 
      { 
       VelocityY *= -1; 
      } 
      if (BallBox.Intersects(ShocObjectBRectangle)) 
      { 
       VelocityX *= -1; 
      } 


      // If Player One Loses 

      if (BallBox.X >= 790) 
      { 
       PlayerOneLoses(); 
      } 


      if (BallBox.X <= 0) 
      { 
       PlayerTwoLoses(); 

      } 



      //Player Two's "AI" and limits 

      if ((PTPBox.Y + 50) > BallBox.Y) 
       PTPBox.Y += -PlayerTwoSpeed; 
      if ((PTPBox.Y + 50) < BallBox.Y) 
       PTPBox.Y += PlayerTwoSpeed; 



      //Object A movement code 

      ShocObjectARectangle.X += ObjectASpeed; 
      if (ShocObjectARectangle.X <= 80) 
       ObjectASpeed *= -1; 
      else if (ShocObjectARectangle.X >= 600) 
       ObjectASpeed *= -1; 


      //Object B movement code 

      ShocObjectBRectangle.Y += ObjectBSpeed; 
      if (ShocObjectBRectangle.Y <= 0) 
       ObjectBSpeed *= -1; 
      else if (ShocObjectBRectangle.Y >= 415) 
       ObjectBSpeed *= -1; 


      // Ball Velocity 

      BallBox.Y += -VelocityY; 
      BallBox.X += VelocityX; 
      break; 
+0

縮短下來的代碼只有相關的部分請... – lifetimes

+0

我不知道內存泄漏將在哪裏。我認爲這是其中一種情況。我們編輯了主要帖子,僅包含一個案例 –

+0

您是指「保存」它是什麼意思? –

回答

4

您似乎在不斷地在每次更新蜱排隊的歌曲在您的MediaPlayer:

你會想要將這個您的更新之外()方法

MediaPlayer.Play(BackgroundMusic);