Search code examples
xnawindows-phone

How do I use a image photo as a Windows phone Button


I created a button image and I named it Texture2D btn_play. It is for a main menu and I want to press it to change the CurrentGameState.

My variables:

    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;

    Texture2D Tex_Btn_play;
    Rectangle Rec_Btn_play;

    enum Menu
    {
        MainMenu,
        Playing,
        Exit,
    }

    Menu CurrentGameState = Menu.MainMenu;

And the Update method:

protected override void Update(GameTime gameTime)
{
    // Allows the game to exit
    if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
        this.Exit();

    if (Rec_Btn_play = TouchLocationState.Pressed);
    // TODO: Add your update logic here

    base.Update(gameTime);
}

The if (Rec_Btn_play = TouchLocationState.Pressed); Is wrong, I don't know why. Please help me solve this problem.


Solution

  • You need to find the touch locations and check if the rectangle intersects with that position

    TouchCollection touchCollection = TouchPanel.GetState();
    foreach (TouchLocation tl in touchCollection)
    {
        if (tl.State == TouchLocationState.Pressed)
        {
            if (Rct_Btn_Play.Contains(new Point(tl.Position.X,tl.Position.Y))
            {
                //DoStuff
            }
    
        }
    }