问题在XNA 4.0游戏中调用Spritebatch

所以,我使用Microsoft Visual C#Studio Express 2010程序在XNA 4.0中创建了一个游戏。

我遇到的问题是:我已经完成了整个游戏,没有理由不能工作。

问题是每次我尝试去debugging我的游戏时,它都会告诉我我的代码中随机区域中缺less一个“Spritebatch.end”或“Spritebatch.begin”。 如果我放入一个“Spritebatch.end”,它告诉我我缺less一个“Spritebatch.begin”,反之亦然。 我重复了这个源代码,没有语法错误。

所以我的问题是:我正在使用的程序是否有问题,还是有其他原因,我的代码给了我这个相当圆形的错误?

这里是我现在所拥有的:我已经实施了其他的修复,但没有任何工作。

private void DrawEmptyPiece(int pixelX, int pixelY) { spriteBatch.Begin(); spriteBatch.Draw( playingPieces, new Rectangle(pixelX, pixelY, GamePiece.PieceWidth, GamePiece.PieceHeight), EmptyPiece, Color.White); spriteBatch.End(); } private void DrawStandardPiece(int x, int y, int pixelX, int pixelY) { spriteBatch.Draw( playingPieces, new Rectangle(pixelX, pixelY, GamePiece.PieceWidth, GamePiece.PieceHeight), gameBoard.GetSourceRect(x, y), Color.White); } private void DrawFallingPiece(int pixelX, int pixelY, string positionName) { spriteBatch.Draw( playingPieces, new Rectangle(pixelX, pixelY - gameBoard.fallingPieces[positionName].VerticalOffset, GamePiece.PieceWidth, GamePiece.PieceHeight), gameBoard.fallingPieces[positionName].GetSourceRect(), Color.White); } private void DrawFadingPiece(int pixelX, int pixelY, string positionName) { spriteBatch.Draw( playingPieces, new Rectangle(pixelX, pixelY, GamePiece.PieceWidth, GamePiece.PieceHeight), gameBoard.fadingPieces[positionName].GetSourceRect(), Color.White * gameBoard.fadingPieces[positionName].alphaLevel); } private void DrawRotatingPiece(int pixelX, int pixelY, string positionName) { spriteBatch.Draw( playingPieces, new Rectangle(pixelX + (GamePiece.PieceWidth / 2), pixelY + (GamePiece.PieceHeight / 2), GamePiece.PieceWidth, GamePiece.PieceHeight), gameBoard.rotatingPieces[positionName].GetSourceRect(), Color.White, gameBoard.rotatingPieces[positionName].RotationAmount, new Vector2(GamePiece.PieceWidth / 2, GamePiece.PieceHeight / 2), SpriteEffects.None, 0.0f); } 

其余的代码。

  protected override void Draw(GameTime gameTime) { foreach (ScoreZoom zoom in ScoreZooms) { spriteBatch.DrawString(pericles36Font, zoom.Text, new Vector2(this.Window.ClientBounds.Width / 2, this.Window.ClientBounds.Height / 2), zoom.DrawColor, 0.0f, new Vector2(pericles36Font.MeasureString(zoom.Text).X / 2, pericles36Font.MeasureString(zoom.Text).Y / 2), zoom.Scale, SpriteEffects.None, 0.0f); } if (gameState == GameStates.TitleScreen) { spriteBatch.Begin(); spriteBatch.Draw(titleScreen, new Rectangle(0, 0, this.Window.ClientBounds.Width, this.Window.ClientBounds.Height), Color.White); spriteBatch.End(); } for (int x = 0; x < GameBoard.GameBoardWidth; x++) for (int y = 0; y < GameBoard.GameBoardHeight; y++) { int pixelX = (int)gameBoardDisplayOrigin.X + (x * GamePiece.PieceWidth); int pixelY = (int)gameBoardDisplayOrigin.Y + (y * GamePiece.PieceHeight); DrawEmptyPiece(pixelX, pixelY); bool pieceDrawn = false; string positionName = x.ToString() + "_" + y.ToString(); if (gameBoard.rotatingPieces.ContainsKey(positionName)) { DrawRotatingPiece(pixelX, pixelY, positionName); pieceDrawn = true; } if (gameBoard.fadingPieces.ContainsKey(positionName)) { DrawFadingPiece(pixelX, pixelY, positionName); pieceDrawn = true; } if (gameBoard.fallingPieces.ContainsKey(positionName)) { DrawFallingPiece(pixelX, pixelY, positionName); pieceDrawn = true; } if (!pieceDrawn) { DrawStandardPiece(x, y, pixelX, pixelY); } } spriteBatch.DrawString(pericles36Font, playerScore.ToString(), scorePosition, Color.Black); base.Draw(gameTime); } 

当你的Draw(GameTime gameTime)是这样的

  protected override void Draw(GameTime gameTime) { spriteBatch.Begin(); foreach (ScoreZoom zoom in ScoreZooms) { spriteBatch.DrawString(pericles36Font, zoom.Text, new Vector2(this.Window.ClientBounds.Width / 2, this.Window.ClientBounds.Height / 2), zoom.DrawColor, 0.0f, new Vector2(pericles36Font.MeasureString(zoom.Text).X / 2, pericles36Font.MeasureString(zoom.Text).Y / 2), zoom.Scale, SpriteEffects.None, 0.0f); } spriteBatch.DrawString(pericles36Font, playerScore.ToString(), scorePosition, Color.Black); spriteBatch.End(); base.Draw(gameTime); } 

我建议将spriteBatch.Draw(...)代码从私有方法移动到Draw(GameTime gameTime)并注释所有这些私有方法