2015-11-03 72 views
0

當我開始我的應用程序時,對象在給定位置(給定矢量)產生。但是,當我將monogame窗口最小化並重新打開它時,則該對象位於左上角。當最小化時位置重置

這究竟是爲什麼?

注:這是我Draw方法:

​​

如何起始位置的計算方法:

// Vector2 position is the starting position for the object 

public PlayerMovement(Texture2D textureImage, Vector2 position, Point frameSize, int collisionOffSet, Point currentFrame, Point startFrame, Point sheetSize, float speed, float speedMultiplier, float millisecondsPerFrame) 
     : base(textureImage, position, frameSize, collisionOffSet, currentFrame, startFrame, sheetSize, speed, speedMultiplier, millisecondsPerFrame) 
{ 
     children = new List<Sprite>(); 
} 

我用Vector2 direction知道精靈面對的方向:

public abstract Vector2 direction 
    { 
     get; 
    } 

我在我的中使用類和返回inputDirection * speed

inputDirectionVector2

最後,在我的Update方法,我做position += direction,我也檢查,如果玩家沒有觸摸屏的邊界(他不能動出屏幕)。

+0

如何設置currentFrame和frameSize計算? –

+0

'currentFrame'是動畫中的當前幀。我爲遊戲在動畫中顯示下一個精靈時等待的時間分配了一個變量。 「frameSize」是動畫中一個精靈的大小(高度和像素有多少像素)。但這不是我想的問題,因爲動畫效果很好。 – Jelle

+0

我在主遊戲類中檢查「IsActive」嗎? – Jelle

回答

1

根據我自己的經驗,在窗口最小化時,在Update調用中使用Game.Window.ClientBounds會導致問題。這裏是我的項目的一些示例代碼:

Rectangle gdm = Game.Window.ClientBounds; 
if (DrawLocation.X < 0) DrawLocation = new Vector2(0, DrawLocation.Y); 
if (DrawLocation.Y < 0) DrawLocation = new Vector2(DrawLocation.X, 0); 
if (DrawLocation.X > gdm.Width - DrawAreaWithOffset.Width) DrawLocation = new Vector2(gdm.Width - DrawAreaWithOffset.Width, DrawLocation.Y); 
if (DrawLocation.Y > gdm.Height - DrawAreaWithOffset.Height) DrawLocation = new Vector2(DrawLocation.X, gdm.Height - DrawAreaWithOffset.Height); 

減少是Game.Window.ClientBounds圍繞-32000返回一些寬/高,當我有問題。在恢復窗口時,這總是會將我的遊戲對象重置爲某個默認位置。我固定它首先檢查該ClientBounds WidthHeight均大於零:

Rectangle gdm = Game.Window.ClientBounds; 
if (gdm.Width > 0 && gdm.Height > 0) //protect when window is minimized 
{ 
    if (DrawLocation.X < 0) 
     DrawLocation = new Vector2(0, DrawLocation.Y); 
    if (DrawLocation.Y < 0) 
     DrawLocation = new Vector2(DrawLocation.X, 0); 
    if (DrawLocation.X > gdm.Width - DrawAreaWithOffset.Width) 
     DrawLocation = new Vector2(gdm.Width - DrawAreaWithOffset.Width, DrawLocation.Y); 
    if (DrawLocation.Y > gdm.Height - DrawAreaWithOffset.Height) 
     DrawLocation = new Vector2(DrawLocation.X, gdm.Height - DrawAreaWithOffset.Height); 
} 

僅供參考,這裏是一個diff of changes是固定的爲自己的項目減少的問題。

當遊戲不是主要的活動窗口時,我曾經參與過一個單獨的錯誤,它與遊戲的交互仍在發生。你也可以在你的UpdateDraw電話的開頭添加一張支票Game.IsActive

public override void Update(GameTime gt) 
{ 
    if(!IsActive) return; 
    //etc... 
} 

或者如果使用遊戲組件,您的組件更新/平局會是這樣的:

public override void Update(GameTime gt) 
{ 
    if(!Game.IsActive) return; 
    //etc... 
} 
+0

它的工作,謝謝! – Jelle

相關問題