Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

monogame button

public class Button : GameObject
    {
        int buttonX, buttonY;

        public int ButtonX
        {
            get
            {
                return buttonX;
            }
        }

        public int ButtonY
        {
            get
            {
                return buttonY;
            }
        }

        public Button(string name, Texture2D texture, int buttonX, int buttonY)
        {
            this.Name = name;
            this.Texture = texture;
            this.buttonX = buttonX;
            this.buttonY = buttonY;
        }

        /**
         * @return true: If a player enters the button with mouse
         */
        public bool enterButton()
        {
            if (MouseInput.getMouseX() < buttonX + Texture.Width &&
                    MouseInput.getMouseX() > buttonX &&
                    MouseInput.getMouseY() < buttonY + Texture.Height &&
                    MouseInput.getMouseY() > buttonY)
            {
                return true;
            }
            return false;
        }

        public void Update(GameTime gameTime)
        {
            if (enterButton() && MouseInput.LastMouseState.LeftButton == ButtonState.Released && MouseInput.MouseState.LeftButton == ButtonState.Pressed)
            {
                switch (Name)
                {
                    case "buy_normal_fish": //the name of the button
                        if (Player.Gold >= 10)
                        {
                            ScreenManager.addFriendly("normal_fish", new Vector2(100, 100), 100, -3, 10, 100);
                            Player.Gold -= 10;
                        }
                        break;
                    default:
                        break;
                }
            }
        }
        public void Draw()
        {
            Screens.ScreenManager.Sprites.Draw(Texture, new Rectangle((int)ButtonX, (int)ButtonY, Texture.Width, Texture.Height), Color.White);   
        } 
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: instantiate type c# 
Csharp :: webclient c# example post 
Csharp :: c# substring find word 
Csharp :: exe path c# 
Csharp :: top down view movement script 
Csharp :: global variables unity 
Csharp :: change color unity over time 
Csharp :: c# remove xml invalid characters 
Csharp :: GetComponent<Button().onClick 
Csharp :: invalidoperationexception c# ui thread 
Csharp :: how to add gravity without rb in unity 
Csharp :: c# Sum of all the factors of a number 
Csharp :: c# short to int 
Csharp :: unity gui button width 
Csharp :: unity hide mouse first person 
Csharp :: vb.net delete folder if exists 
Csharp :: w3develops 
Csharp :: DataGridView ComboBox column selection changed event 
Csharp :: c# datagridview center cell text 
Csharp :: method c# 
Csharp :: rigidbody.velocity.magnitude 
Csharp :: dotnet core encryption and decryption 
Csharp :: c# datediff 
Csharp :: change object position 
Csharp :: Check if list contains any of another list 
Csharp :: c# nunit test case 
Csharp :: list contains type c# 
Csharp :: f# get last element of list 
Csharp :: change tab to enter in c# form 
Csharp :: unity android keycodes 
ADD CONTENT
Topic
Content
Source link
Name
4+7 =