Mouse.SetPosition() 赋值变量被视为无效
本文关键字:无效 变量 赋值 SetPosition Mouse | 更新日期: 2023-09-27 18:34:25
我正在尝试将鼠标位置设置为等于变量"位置",但收到错误"无法将类型'void'隐式转换为'Microsoft.Xna.Framework.Input.MouseState"。据我所知,"位置"并非无效。这是代码,由于试图使这项工作有点混乱:
class Player
{
private Texture2D texture;
private static int textureSize = 20;
private static int screenWidth = Game1.Instance.GraphicsDevice.Viewport.Width;
private static int screenHeight = Game1.Instance.GraphicsDevice.Viewport.Height;
private static int halfTexture = (int)(textureSize * (screenHeight / (double)textureSize)) / 2;
private Vector2 location;
private Rectangle destination;
private float speed;
private MouseState mouse;
public Player(Texture2D texture)
{
this.texture = texture;
this.location = new Vector2(screenWidth / 2 - halfTexture, screenHeight * 3 / 4 - halfTexture);
this.destination = new Rectangle((int)location.X, (int)location.Y, textureSize, textureSize);
this.mouse = Mouse.SetPosition((int)location.X, (int)location.Y); // the error is here
}
那么这是怎么回事,我该如何解决呢?
如果不查看 XNA 文档,我仍然可以肯定地说 Mouse.SetPosition
-方法 返回void
,并且您正在尝试将其返回值分配给类型 MouseState
的this.mouse
。删除该作业,您将没事的。