C#XNA鼠标位置 [英] C# XNA Mouse Position

查看:199
本文介绍了C#XNA鼠标位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我的鼠标的一些问题坐标XNA - 0x0的是任意接近(但不是在)我的屏幕的左上角。



我运行在窗口模式下游戏的权利,但该坐标基于关闭屏幕,而不是游戏窗口(尽管XNA文档告诉我,它应该是其他方式)



在此先感谢



下面的代码:

 命名空间TheGame 
{
类像老鼠
{
静态公共Vector2 POS;
静态专用的Texture2D TEX;
静态公共MouseState mouseState;
静态公共MouseState previousState;

//静态公共像老鼠()
// {
//}

静态公共无效更新()
{
previousState = mouseState;
mouseState = Mouse.GetState(); //需要找到最新的鼠标状态。
pos.X = mouseState.X; //将x更改为POS mouseX
pos.Y = mouseState.Y; //改变Y位置为mouseY的
}

//绘图功能在正选赛函数被调用。
静态公共无效LoadContent(ContentManager thecontent)
{
TEX = thecontent.Load<&的Texture2D GT(像老鼠);
}

静态公共无效抽奖(SpriteBatch批)// SpriteBatch使用。
{
batch.Draw(TEX,POS,Color.White); //使用批处理绘制它。
}

静态公共布尔LBP()
{
如果(mouseState.LeftButton == ButtonState.Pressed&放大器;&安培; previousState.LeftButton == ButtonState.Released )
{
返回真;
}
,否则
{
返回FALSE;
}
}
}
}


解决方案

在game.cs:

  //设置窗口鼠标手柄客户范围手柄

Mouse.WindowHandle = Window.Handle;


I am having some issues with my mouse coordinates in XNA - the 0x0 is arbitrarily near (but not in) the top left corner of my screen.

I am running the game in a windowed mode right now, but the coordinates are based off the screen, not the game window (even though the XNA documentation tells me it should be otherwise)

Thanks in advance!

Here's the code:

namespace TheGame
{
   class Mousey
   {
      static public Vector2 pos;
      static private Texture2D tex;
      static public MouseState mouseState;
      static public MouseState previousState;

      //static public Mousey()
      //{
      //}

      static public void Update()
      {
         previousState = mouseState;
         mouseState = Mouse.GetState(); //Needed to find the most current mouse states.
         pos.X = mouseState.X; //Change x pos to mouseX
         pos.Y = mouseState.Y; //Change y pos to mouseY
      }

      //Drawing function to be called in the main Draw function.
      static public void LoadContent(ContentManager thecontent)
      {
         tex = thecontent.Load<Texture2D>("mousey");
      }

      static public void Draw(SpriteBatch batch) //SpriteBatch to use.
      {
         batch.Draw(tex, pos, Color.White); //Draw it using the batch.
      }

      static public bool LBP()
      {
          if (mouseState.LeftButton == ButtonState.Pressed && previousState.LeftButton ==                      ButtonState.Released)
          {
              return true; 
          }
          else 
          { 
              return false; 
          }
      }   
   }
}

解决方案

In game.cs:

//sets the windows mouse handle to client bounds handle

Mouse.WindowHandle = Window.Handle;

这篇关于C#XNA鼠标位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆