2015-11-03 71 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

相关问题