2012-11-21 69 views
6

所以我啓動了當前的項目,我做的第一件事就是運行它,它給了我上面的例外。昨晚運行良好。這是我的Draw事件中的所有代碼。 spriteBatch.Begin不會出現在項目的其他任何地方。這裏刪除開始會導致spriteBatch.Draw引發異常,在開始之前放置spriteBatch.End也會引發異常。我不知道什麼是錯的,以及如何解決這個問題。SpriteBatch:「在成功調用End之前,不能再次調用Begin。」

GraphicsDevice.Clear(Color.CornflowerBlue); 

spriteBatch.Begin(); 
spriteBatch.Draw(background, Vector2.Zero, Color.White); 

player.Draw(spriteBatch); 
level1.Draw(spriteBatch); 

spriteBatch.End(); 

base.Draw(gameTime); 
+1

對於一個完整的故事,你能提供玩家和level1的繪製方法嗎?因爲這些問題可能是由這些方法引起的。 –

+1

2可能性:1 |或者在player.draw或者level1.draw中調用spritebatch.begin()2 | spirteBatch.End()不會被調用,因爲你沒有達到它。 – Svexo

回答

1

嗯,我需要看到你的levek1.Draw和player.Draw爲我的信息,但我期待這一點。在你的其中一種方法中,你打電話給spriteBatch.Begin(),你必須在打電話前致電End()

所以,如果您的播放器類的Draw方法看起來像這樣

Draw(Spritebatch spriteBatch) 
{ 
spriteBatch.Begin(); 
spriteBatch.Draw(PlayerSprite,PlayerPosition, etc); 
spriteBatch.End(); 
} 

您需要刪除BeginEnd電話,因爲他們已經被父方法調用。

編輯:沒讀清楚,我看你相信的,這是沒有問題的,但我們需要看到其他的方法來找出問題。

0

ctrl+F並選擇Entire Solution,查找spriteBatch.Begin()並將其刪除不屬於它的地方。我懷疑你可能在你的playerlevel平局中有額外的副本。

還要確保你使用正確的變量爲你的精靈批次,它可以被稱爲SpriteBatch,與第一個字母大寫。

無論如何,你可以嘗試刪除(有//評論他們)所有spritebatch從您的項目需要,看看它是否仍然會拋出異常。

1

您的一個繪圖調用有可能拋出異常。這會讓你不用撥打電話spriteBatch.End(),然後下一次,你會在spriteBatch.Begin()上得到一個例外。 (雖然我不知道爲什麼第一個異常並沒有結束你的程序,但第二個做到了。)

如果是這樣的問題,一個解決辦法是在try/finally塊包裹抽獎電話:

spriteBatch.Begin(); 
spriteBatch.Draw(background, Vector2.Zero, Color.White); 

try { 
    player.Draw(spriteBatch); 
    level1.Draw(spriteBatch); 
} finally { 
    spriteBatch.End(); 
} 

另一種可能性是,您實際上意外地撥打了spriteBatch.Begin()兩次。我個人通過將SpriteBatch對象封裝在不同的類中來避免這樣做。

例:

internal sealed class DrawParams 
{ 
    private SpriteBatch mSpriteBatch; 
    private bool mBegin; 

    /// <summary>Calls SpriteBatch.Begin if the begin value is true. Always call this in a draw method; use the return value to determine whether you should call EndDraw.</summary> 
    /// <returns>A value indicating whether or not begin was called, and thus whether or not you should call end.</returns> 
    public bool BeginDraw() 
    { 
     bool rBegin = mBegin; 

     if (mBegin) 
     { 
     mSpriteBatch.Begin(); 
     mBegin = false; 
     } 

     return rBegin; 
    } 

    /// <summary>Always calls SpriteBatch.End. Use the return value of BeginDraw to determine if you should call this method after drawing.</summary> 
    public void EndDraw() 
    { 
     mSpriteBatch.End(); 
    } 
} 
0

有了這個確切的問題我自己,沒有任何的建議,在這裏工作的。最後,我意識到在我的開始/結束部分中,我是一個異步方法。

已經意識到,這將可能再重新啓動完成前的方法,我只是增加了一個bool isDrawing變量和繪圖前檢查它。

HTH

相關問題