[英]XNA C# 2D Scrolling Background 3 or More
我正在嘗試創建具有3個背景的滾動背景,但是當第3個開始出現時,一切都應如此。 它在第3個屏幕的前面創建了一個巨大的藍屏(游戲的默認背景),而在3RD之后則不顯示任何背景。 我不知道該如何解決,而且我已經知道一些簡單的方法。 我有一個3RD可以工作,但是。
我正在嘗試使用的代碼。
public void Update(GameTime gameTime)
{
bgPos0.Y += speed;
bgPos1.Y += speed;
bgPos2.Y += speed;
if (bgPos0.Y >= 950)
{
bgPos1.Y = -950;
if (bgPos1.Y >= 950) // Doesn't go fully down.
{
bgPos2.Y = -950;
if (bgPos2.Y >= 950)
{
bgPos0.Y = 0; //(Try to change it to -950 still doesn't work. I guest it due to that bgPos0 is set to 0, 0)
}
}
}
}
pos的Vector2代碼是
bgPos0 = new Vector2(0, 0);
bgPos1 = new Vector2(0, -950);
bgPos2 = new Vector2(0, -1900); // Could be the large -1900 number that destroying the code. To make it not work.
那么我該如何解決呢? 我希望我現在可以修復它,但是由於某種原因我不能。
我認為您不希望嵌套if語句。 您發布的代碼會一直將第二個背景移至-950,直到第一個背景移至950為止。由於第一個背景不斷移回至-950,因此它永遠不會移至950以上,因此它永遠不會進入最后一個。 我認為您可能想做的更像是:
public void Update(GameTime gameTime)
{
bgPos0.Y += speed;
if(bgPos0.Y > 950) {
bgPos0.Y = -950;
}
bgPos1.Y += speed;
if(bgPos1.Y > 950) {
bgPos1.Y = -950;
}
bgPos2.Y += speed;
if(bgPos1.Y > 950) {
bgPos1.Y = -950;
}
}
[編輯]:順便說一句,所討論的數字還不足以引起問題。 XNA的Vector2類將x和y分量存儲為浮點數,而C#中的浮點數最大值在3.4e38左右,根據MSDN精確到7位。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.