XNA鼠标&x27;相对于'的位置;更改每个构建

XNA鼠标&x27;相对于'的位置;更改每个构建,xna,Xna,好的,我开始为一个小型flash游戏制作一个主菜单,要做到这一点,我想用鼠标点击按钮等。我有一个按钮类,我在其中创建了两个矩形:一个用于按钮的矩形和一个基于X和Y的鼠标矩形,1像素乘以1像素。我使用矩形。相交来检查它们是否接触,然后再看鼠标左键是否按下。问题是,鼠标位置每次都会发生变化,所以无论鼠标按钮在屏幕上的什么位置,它的坐标永远不会与在同一位置的不同构建中的坐标相同。我现在真的需要想法,因为我快用完了。如果我没有很好地解释,或者你需要更多的细节来帮助,请询问-我将非常感激 如果我找到答案

好的,我开始为一个小型flash游戏制作一个主菜单,要做到这一点,我想用鼠标点击按钮等。我有一个按钮类,我在其中创建了两个矩形:一个用于按钮的矩形和一个基于X和Y的鼠标矩形,1像素乘以1像素。我使用矩形。相交来检查它们是否接触,然后再看鼠标左键是否按下。问题是,鼠标位置每次都会发生变化,所以无论鼠标按钮在屏幕上的什么位置,它的坐标永远不会与在同一位置的不同构建中的坐标相同。我现在真的需要想法,因为我快用完了。如果我没有很好地解释,或者你需要更多的细节来帮助,请询问-我将非常感激

  • 如果我找到答案,会发回吗
更新-好的,这是按钮类

    class OnScreenButton
    {
    public Texture2D texture;
    Vector2 position;
    Rectangle rectangle;

    Color colour = new Color(255, 255, 255, 255);

    public Vector2 size;

    public OnScreenButton(Texture2D newtexture, GraphicsDevice graphics)
    {
        texture = newtexture;

        // ScreenW = 500, ScreenH = 600
        // Img W = 80, Img H = 20
        size = new Vector2(graphics.Viewport.Width / 10, graphics.Viewport.Height / 30);
        size = new Vector2(texture.Width, texture.Height);

    }

    bool down;
    public bool isClicked;

    public void Update(MouseState mouseState)
    {
        rectangle = new Rectangle((int)position.X, (int)position.Y, (int)size.X, (int)size.Y);

        Rectangle mouseRectangle = new Rectangle(mouseState.X, mouseState.Y, 1, 1);

        if (mouseRectangle.Intersects(rectangle))
        {
            if (colour.A == 255)
            {
                down = false;
            }
            if (colour.A == 0)
            {
                down = true;
            }
            if (down)
            {
                colour.A += 3;
            }
            else
            {
                colour.A -= 3;
            }
            if (mouseState.LeftButton == ButtonState.Pressed)
            {
                isClicked = true;
            }

        }
        else if (colour.A < 255)
        {
            colour.A += 3;
            isClicked = false;
            colour.A = (255);
        }
    }

    public void SetPosition(Vector2 newPos)
    {
        position = newPos;
    }

    public void Draw(SpriteBatch spriteBatch)
    {
        spriteBatch.Draw(texture, rectangle, colour);
    }
    }
    }
主菜单更新程序

private void UpdateMainMenu(GameTime gameTime)
    {
        // Button options
        if (buttonPlay.isClicked == true)
        {
            CreateNewGame();
            currentGameState = GameState.playing;
        }
        buttonPlay.Update(mouseState);

        if (buttonExit.isClicked == true)
        {
            this.Exit();
        }
        buttonExit.Update(mouseState);

        // Press enter to play
        if (keyboardState.IsKeyDown(Keys.Enter))
        {
            CreateNewGame();
            currentGameState = GameState.playing;
        }
    }
这是主菜单的绘图程序

public void DrawMainMenu()
    {
        spriteBatch.Draw(mainMenuBackground, new Vector2(0, 0), Color.White);
        buttonPlay.Draw(spriteBatch);
        buttonExit.Draw(spriteBatch);
        spriteBatch.DrawString(playerAmmoFont, String.Format("{0}", mouseState), new Vector2(0, 0), Color.White);
    }
好吧,这就是我能想到的

更新-好的,所以我知道一些不是问题的事情。。。 整个button类都很好,我做了一个新项目,并将所有相关代码插入其中,它工作得非常完美,所以我开始认为这与代码定位和图形设备有关,尽管我仍然不知道如何修复它

  • 窗口每次都出现在同一个位置
  • 坐标的变化根本没有规律可循
  • 这真烦人
更新-好的。我花了很长时间写下每次运行代码时得到的坐标,并将光标固定在屏幕右上角。这是我得到的

  • (-203,-225)
  • (-253,-275)
  • (-53,-75)
  • (-103,-125)
  • (-153,-175)
  • (-203,-225)
  • (-253,-275)
  • (-53,-75)
  • (-103,-125)
  • (-153,-175)
  • (-203,-225)
  • (-253,-275)
  • (-53,-75)
  • (-103,-125)
  • (-153,-175)
  • (-203,-225)
  • (-253,-275)
  • (-53,-75)
  • (-103,-125)
  • (-153,-175)
  • (-203,-225)
  • (-78,-100)
  • (-128,-150)
  • (-178,-200)
  • (-228,-250)
  • (-28,-50)
  • (-53,-75)
  • (-103,-125)
  • (-153,-175)<从这里开始,图案循环
我只是不明白相同的代码如何在不同的执行上执行不同的bug

另外,mouse.Wheel不会上升或下降,而它在我测试相关代码的项目中工作,其中鼠标位置与游戏窗口左上角的像素相关


更新-更该死的复杂-所以我只是兰德它几次再次和偏移值偏移。。。增加量是一样的,但我得到的值是(-178,-200),然后是(-228,-250)。我还发现,鼠标与游戏窗口并不相关,如果我将鼠标卡在屏幕右上角并检查坐标,然后移动游戏窗口并再次执行相同操作,坐标不会改变。请帮帮我,或者告诉我我是不是很蠢,或者什么的。谢谢。

鼠标坐标是相对于显示器的。这里是我的通用按钮类,尝试为您的情况工作

public class Button
{  
    public event EventHandler<EventArgs> Clicked;

    public Vector2 Position { get; set;}

    public Texture2D Texture { get; set;}

    public Color Tint { get; set; }

    public float Scale { get; set; }
    public float Rotation { get; set; }        

    public int Width
    {
        get
        {
            if (texture == null)
                return 0;
            else
                return texture.Width;
        }
    }

    public int Height
    {
        get
        {
            if (texture == null)
                return 0;
            else
                return texture.Height;
        }
    }

    private void OnClick()
    {
        if (Clicked != null)
            Clicked(this, EventArgs.Empty);
    }

    public Button(Vector2 position, Texture2D texture)
        : base(parent)
    {
        Position = position;      
        Texture = texture;   

        Tint = Color.White; 

        Scale = 1.0f;
        Rotation = 0.0f;  
    }

    public bool HandleClick(Vector2 vector)
    {
        if (vector.X >= Position.X)
        {
            if (vector.X <= Position.X + Width)
            {
                if (vector.Y >= Position.Y)
                {
                    if (vector.Y <= Position.Y + Height)
                    {
                        OnClick();
                        return true;
                    }
                }
            }
        }

        return false;
    }

    public bool HandleEntered(Vector2 vector)
    {
        if (vector.X >= Position.X)
        {
            if (vector.X <= Position.X + Width)
            {
                if (vector.Y >= Position.Y)
                {
                    if (vector.Y <= Position.Y + Height)
                    {
                        return true;
                    }
                }
            }
        }

        return false;
    }

    public override void Draw(SpriteBatch spriteBatch)
    {
        spriteBatch.Draw(Texture, Position, null, Tint, Rotation, Vector2.Zero, Scale, SpriteEffects.None, 0.0f); 
    }
在主游戏内的更新方法中:

public void Update (GameTime gameTime)
{
    MouseState mouseState = Mouse.GetState();

    if(mouseState.LeftButton == ButtonState.Pressed) // check if mouse is clicked
    {
        btn.HandleClicked(new Vector2(mouseState.X, mouseState.Y)); // If true then the button clicked event will fire
        // Here you can also change the color of the button if the button is currently clicked
    }

    // Here you can change the color of the button if the mouse is hover over the control
    // Example: 
    btn.Tint = btn.HandleEntered(new Vector2(mouseState.X, mouseState.Y)) ? Color.White * 0.75f : Color.White;
}

注意:您也可以使用矩形按钮来调整其大小,而不是严格使用纹理尺寸。希望这能给我们带来一些启示。

那么接下来发生了什么:我在游戏中为每一次子弹射击都上了子弹课。在这门课上,我检查子弹是否击中目标、击中小行星或摧毁小行星。如果后者为真,那么我会将playerScore增加5。PlayerScore是Game1属性,所以我认为最简单的方法是在bullet.cs中创建一个新的Game1,以允许我引用变量。删除Bullet.cs中的“Main mainGame=new Main():”修复了这个问题,我认为这个问题是由于每次我发射一颗子弹时都会制作一个新的图形设备造成的

没有代码就什么都说不出来。代码会有帮助,如果在XNA中,它就不是flash:是的,对不起,你知道我的意思:Pokay我修复了它,我创建了一个Main.cs的新实例,oops#noobI正在看这篇文章。我想删除这个问题,但出于历史原因,我将保留它。“一款小型flash游戏”;我使用的是
XNA/C
。谢谢你,我解决了这个问题,我太傻了,但我真的是个新手,所以我想我会从这个错误中吸取教训。这看起来是一个非常好的按钮类,我可以问一下为什么要使用get set例程吗?在我的游戏中我一次也没用过它们,我有12门课。谢谢你的回答)Get set是c#中的访问器。此链接是解释访问者的msdn。
Button btn = new Button(position where you want the button, texture for the button);

btn.Clicked += () => { /* Handle button clicked code here */ };
public void Update (GameTime gameTime)
{
    MouseState mouseState = Mouse.GetState();

    if(mouseState.LeftButton == ButtonState.Pressed) // check if mouse is clicked
    {
        btn.HandleClicked(new Vector2(mouseState.X, mouseState.Y)); // If true then the button clicked event will fire
        // Here you can also change the color of the button if the button is currently clicked
    }

    // Here you can change the color of the button if the mouse is hover over the control
    // Example: 
    btn.Tint = btn.HandleEntered(new Vector2(mouseState.X, mouseState.Y)) ? Color.White * 0.75f : Color.White;
}