[英]Position reset when minimizing
When I start my application, the object spawns at the given position (given vector). 当我启动我的应用程序时,对象在给定位置(给定的矢量)产生。 But when I minimize the monogame window and reopen it, then the object is in the upper-left corner.
但是,当我最小化monogame窗口并重新打开它时,该对象位于左上角。
Why is this happening? 为什么会这样呢?
NOTE: this is my Draw
method: 注意:这是我的
Draw
方法:
public virtual void Draw(GameTime gameTime, SpriteBatch spriteBatch)
{
// Position is the object position
spriteBatch.Draw(textureImage, position, new Rectangle(
(currentFrame.X * frameSize.X),
(currentFrame.Y * frameSize.Y),
frameSize.X, frameSize.Y),
Color.White, 0, Vector2.Zero, 2, SpriteEffects.None, 0);
}
How the starting position is calculated: 起始位置的计算方式:
// 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>();
}
I use Vector2 direction
to know which direction the sprite is facing: 我使用
Vector2 direction
来了解精灵所面向的方向:
public abstract Vector2 direction
{
get;
}
I use the get
in my PlayerMovement
class and return inputDirection * speed
我在
PlayerMovement
类中使用get
并返回inputDirection * speed
( inputDirection
is a Vector2
) (
inputDirection
是一个Vector2
)
Finally in my Update
method, I do position += direction
and I also check if the player isn't touching the borders of the screen(he can't move out the screen.). 最后,在
Update
方法中,我position += direction
,还检查播放器是否未触摸屏幕的边界(他不能移出屏幕)。
From my own experience, using Game.Window.ClientBounds
in an Update
call has caused problems when the window is minimized. 根据我的经验,在最小化窗口时,在
Update
调用中使用Game.Window.ClientBounds
会引起问题。 Here is some sample code from my project: 这是我的项目中的一些示例代码:
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);
The problem I had when minimizing was that Game.Window.ClientBounds was returning some width/height around -32000
. 最小化时我遇到的问题是Game.Window.ClientBounds返回的宽度/高度约为
-32000
。 This would always reset my game objects to some default location when restoring the window. 恢复窗口时,这总是将我的游戏对象重置到某个默认位置。 I fixed it by first checking that the ClientBounds
Width
and Height
were both greater than zero: 我通过首先检查ClientBounds
Width
和Height
都大于零来解决此问题:
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);
}
For reference, here is a diff of changes that fixed the minimize problem for my own project. 作为参考,这里有一些变化 ,这些变化解决了我自己项目的最小化问题。
A separate bug I was having involved interaction with the game still happening when the game was not the primary, active window. 当游戏不是主要的活动窗口时,我仍在与游戏互动发生一个单独的错误。 You can also add a check for
Game.IsActive
at the beginning of your Update
and Draw
calls: 您还可以在
Update
和Draw
调用开始时添加对Game.IsActive
的检查:
public override void Update(GameTime gt)
{
if(!IsActive) return;
//etc...
}
Or if using Game Components, your component update/draw would look like: 或者,如果使用游戏组件,则组件的更新/绘制如下所示:
public override void Update(GameTime gt)
{
if(!Game.IsActive) return;
//etc...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.