Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# XNA-使精灵无法走出边界_C#_Xna - Fatal编程技术网

C# XNA-使精灵无法走出边界

C# XNA-使精灵无法走出边界,c#,xna,C#,Xna,我一直在和MinX和MinY玩,但我就是不能让它工作。我有一个播放器(一个盒子),我用鼠标来引导它。但是我可以越过边界,这样就看不到球员了。我想解决这个问题,但不知道如何解决 这是我的代码(我给你完整的,因为我不确定你需要它的哪一部分): 使用系统; 使用System.Collections.Generic; 使用System.Linq; 使用Microsoft.Xna.Framework; 使用Microsoft.Xna.Framework.Audio; 使用Microsoft.Xna.Fra

我一直在和MinX和MinY玩,但我就是不能让它工作。我有一个播放器(一个盒子),我用鼠标来引导它。但是我可以越过边界,这样就看不到球员了。我想解决这个问题,但不知道如何解决

这是我的代码(我给你完整的,因为我不确定你需要它的哪一部分):

使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用Microsoft.Xna.Framework;
使用Microsoft.Xna.Framework.Audio;
使用Microsoft.Xna.Framework.Content;
使用Microsoft.Xna.Framework.GamerServices;
使用Microsoft.Xna.Framework.Graphics;
使用Microsoft.Xna.Framework.Input;
使用Microsoft.Xna.Framework.Media;
名称空间平方游戏
{
/// 
///这是游戏的主要类型
/// 
公共类游戏1:Microsoft.Xna.Framework.Game
{
图形管理器图形;
SpriteBatch SpriteBatch;
公共游戏1()
{
graphics=新的GraphicsDeviceManager(此);
Content.RootDirectory=“Content”;
}
/// 
///允许游戏在开始运行之前执行任何需要的初始化。
///在这里,它可以查询任何必需的服务,并加载任何非图形化的服务
///相关内容。调用base.Initialize将枚举所有组件
///并对它们进行初始化。
/// 
受保护的覆盖无效初始化()
{
//TODO:在此处添加初始化逻辑
base.Initialize();
}
/// 
///LoadContent将在每个游戏中调用一次,并且是加载的地方
///你所有的内容。
/// 
/// 
纹理2D播放器;
Vector2 spritePosition=Vector2.0;
私有向量2起源;
私人矢量2屏幕POS;
受保护的覆盖void LoadContent()
{
//创建一个新的SpriteBatch,可用于绘制纹理。
spriteBatch=新spriteBatch(图形设备);
Player=Content.Load(“Models\\Player”);
视口=graphics.graphicsdevelope.Viewport;
origin.X=Player.Width/2;
原点.Y=玩家高度/2;
screenpos.X=视口宽度/2;
屏幕位置Y=视口高度/2;
//TODO:使用此.Content在此处加载游戏内容
}
/// 
///UnloadContent将在每个游戏中调用一次,并且是卸载的地方
///所有内容。
/// 
受保护的覆盖无效UnloadContent()
{
//TODO:在此卸载任何非ContentManager内容
}
/// 
///允许游戏运行逻辑,例如更新世界,
///检查碰撞、收集输入和播放音频。
/// 
///提供计时值的快照。
私人浮动旋转角;
受保护覆盖无效更新(游戏时间游戏时间)
{
//允许游戏退出
if(GamePad.GetState(PlayerIndex.One).Buttons.Back==ButtonState.Pressed)
这是Exit();
已用浮点=(浮点)gameTime.ElapsedGameTime.TotalSeconds*2;
//TODO:在此处添加更新逻辑
旋转角度+=经过的时间;
浮动圆=MathHelper.Pi*2;
RotationAngle=RotationAngle%圆;;
spritePosition.X=Mouse.GetState().X;
spritePosition.Y=Mouse.GetState().Y;
更新(游戏时间);
}
/// 
///这就是所谓的比赛应该平局的时候。
/// 
///提供计时值的快照。
///
受保护覆盖无效绘制(游戏时间游戏时间)
{
graphics.GraphicsDevice.Clear(颜色:浅黄花);
//TODO:在此处添加图形代码
spriteBatch.Begin();
spriteBatch.Draw(播放器、精灵位置、空值、颜色、白色、旋转角度、原点、1.0f、精灵效果、无、0f);
spriteBatch.End();
基础。抽签(游戏时间);
}
}
}
还有我的球员在边境的照片:


先谢谢你!:)

在更新方法中说:

if(Mouse.GetState().X > 0 && Mouse.GetState().X < yourWindowWidth)            
    spritePosition.X = Mouse.GetState().X;
else if (Mouse.GetState().X < 0)
    spritePosition.X = 0;
else                   //The mouse is out from the right side of the window.
    spritePosition.X = yourWindowWidth;
if(Mouse.GetState().Y > 0 && Mouse.GetState().Y < yourWindowHeight)
    spritePosition.Y = Mouse.GetState().Y;
else if (Mouse.GetState().Y < 0)
    spritePosition.Y = 0;
else                   //The mouse is out from the bottom side of the window.
    spritePosition.Y = yourWindowHeight;
if(Mouse.GetState().X>0&&Mouse.GetState().X0&&Mouse.GetState().Y

希望这有帮助。

在移动到鼠标之前,请检查mouse.GetState().X是否大于零且小于窗口的宽度。在y轴上做同样的事情。我在哪里检查它是否大于零?在这行spritePosition.X之前=Mouse.GetState().X;小菜鸟很抱歉地说,我不太明白这是一种魅力。有没有可能让玩家从精灵中心而不是边缘与波尔多相撞?编辑:有时候它看起来很笨拙。好像它不会一直走到边缘。然后在它到达边缘之前,我必须来回拿几次。我想如果我让鼠标无法走出游戏窗口,它可能会被修复,不确定:oWorked!谢谢!:)
if(Mouse.GetState().X > 0 && Mouse.GetState().X < yourWindowWidth)            
    spritePosition.X = Mouse.GetState().X;
else if (Mouse.GetState().X < 0)
    spritePosition.X = 0;
else                   //The mouse is out from the right side of the window.
    spritePosition.X = yourWindowWidth;
if(Mouse.GetState().Y > 0 && Mouse.GetState().Y < yourWindowHeight)
    spritePosition.Y = Mouse.GetState().Y;
else if (Mouse.GetState().Y < 0)
    spritePosition.Y = 0;
else                   //The mouse is out from the bottom side of the window.
    spritePosition.Y = yourWindowHeight;