简体   繁体   中英

Rectangle makes Texture2D invisible

In the game I'm currently working on I have a play button which have a Vector2 , a Rectangle , and a Texture2D . But somehow when I run the game the play button is invisible, but it still reacts to mouse collision/detection.
This is my code:

    Texture2D buttonPlay;
    Rectangle buttonPlayRect;
    Vector2 buttonPlayPos;

    Point mousePosition;

    int resolutionX = ScreenManager.GraphicsDeviceMgr.PreferredBackBufferWidth;
    int resolutionY = ScreenManager.GraphicsDeviceMgr.PreferredBackBufferHeight;

    public void Initialize()
    {
        buttonPlayPos = new Vector2(resolutionX / 2 - 64, resolutionY / 2 - 64);
    }

    public override void LoadAssets()
    {
       buttonOptionsPos = new Vector2(resolutionX / 2 - 64, resolutionY / 2);

       backgroundTile = ScreenManager.ContentMgr.Load<Texture2D>("Menu/background");
       buttonOptions = ScreenManager.ContentMgr.Load<Texture2D>("Menu/optionsButton");
       buttonPlay = ScreenManager.ContentMgr.Load<Texture2D>("Menu/playButton");

       buttonPlayRect = new Rectangle((int)buttonPlayPos.X, (int)buttonPlayPos.Y, buttonPlay.Width, buttonPlay.Height);

        base.LoadAssets();
    }

    public override void Update(Microsoft.Xna.Framework.GameTime gameTime)
    {
        MouseState mState = Mouse.GetState();
        mousePosition = new Point(mState.X, mState.Y);

        base.Update(gameTime);
    }

    public override void Draw(Microsoft.Xna.Framework.GameTime gameTime)
    {
        for (int x = 0; x < 10; x++)
        {
            for (int y = 0; y < 10; y++)
            {
                ScreenManager.Sprites.Draw(backgroundTile, new Vector2(tileWidth * x, tileHeight * y), Color.White);
            }
        }

        ScreenManager.Sprites.Draw(buttonPlay, buttonPlayPos, buttonPlayRect, Color.White);
        ScreenManager.Sprites.Draw(buttonOptions, buttonOptionsPos, Color.White);

        if (buttonPlayRect.Contains(mousePosition))
        {

        }

        base.Draw(gameTime);
    }
}

I've had this problem for a while with other projects aswell, what makes the Texture2D not appear? Thanks in advance!

When you draw something I suggest you to use only the Vector2 position or the destinationRectangle of the SpriteBatch.Draw , sometimes it behaves unexpectedly.
What's more, there's no reason to set a position and use it to declare a rectangle if you pass both them as parameters.

And as a suggestion, you should check buttonPlayRect.Contains(mousePosition) in your Update method, Draw should only manage the drawing of your game, not the logic.

UPDATE

If you need to change the postion of your buttons depending on the current resolution, you can draw them declaring the position in every Draw cycle

new Vector2(resolutionX / 2 - 64, resolutionY / 2 - 64);

or you can add an event handler in this way:

Add this in your Initialize method:

Game.Window.ClientSizeChanged += Window_ClientSizeChanged;

then create the Window_ClientSizeChanged method with something like this:

private void Window_ClientSizeChanged(object sender, EventArgs e)
{
    buttonPlayPos = new Vector2(resolutionX / 2 - 64, resolutionY / 2 - 64);
    buttonPlayRect = new Rectangle((int)buttonPlayPos.X, (int)buttonPlayPos.Y, buttonPlay.Width, buttonPlay.Height);
}

Of course resolutionX and resolutionY have to be the updated values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM