Search code examples
c#xmlmonogame

Has the wrong return type monogame


Im trying to make a button which when I click it, it changes the gamestate to playing and then calls everything which would play the game however I cant get the method to return the game stat.

    private GameState Playbutton_Click(object sender, EventArgs e)
    {
        GameState CurrentState = GameState.playing;
        return CurrentState;
    }

The error I get is 'Game1.GameState Game1.Playbutton_Click(object, EventArgs)' has the wrong return type

Edit:

In game 1:

public class Game1 : Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;



    enum GameState
    {
        playing,
        menu,
        over
    }

    GameState CurrentState = GameState.playing;

    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
    }


    GameState CurrentState = GameState.playing;


              Playbutton = new Button(Content.Load<Texture2D>("button"), Content.Load<SpriteFont>("Font"))
        {
            position = new Vector2(690, 500),
            Text = ("Play")
        };
        Quitbutton = new Button(Content.Load<Texture2D>("button"), Content.Load<SpriteFont>("Font"))
        {
            position = new Vector2(690, 800),
            Text = ("Quit")
        };

        Playbutton.Click += Playbutton_Click;
        Quitbutton.Click += Quitbutton_Click;
        components = new List<Component>()
        {
            Playbutton,
            Quitbutton
        };


              private void Quitbutton_Click(object sender, EventArgs e)
    {
        Exit();
    }

    private void Playbutton_Click(object sender, EventArgs e)
    {
        GameState CurrentState = GameState.playing;
    }

Button class:

   private MouseState _currentMouse;
    private SpriteFont _font;
    private bool _isHovering;
    private MouseState _previousMouse;
    private Texture2D _texture;

    public event EventHandler Click;
    public bool Clicked { get; private set; }
    public Color PenColour { get; set; }
    public Vector2 position { get; set; }
    public Rectangle Rectangle
    {
        get
        {
            return new Rectangle((int)position.X, (int)position.Y, _texture.Width, _texture.Height);
        }
    }
    public string Text { get; set; }
    public Button(Texture2D texture, SpriteFont font)
    {
        _texture = texture;
        _font = font;
        PenColour = Color.Black;
    }
    public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
    {
        var colour = Color.White;
        if (_isHovering)
            colour = Color.Gray;
        spriteBatch.Draw(_texture, Rectangle, colour);
        if (!string.IsNullOrEmpty(Text))
        {
            var x = (Rectangle.X + (Rectangle.Width / 2)) - (_font.MeasureString(Text).X / 2);
            var y = (Rectangle.Y + (Rectangle.Height / 2)) - (_font.MeasureString(Text).Y / 2);
            spriteBatch.DrawString(_font, Text, new Vector2(x, y), PenColour);
        }
    }
    public override void Update(GameTime gameTime)
    {
        _previousMouse = _currentMouse;
        _currentMouse = Mouse.GetState();
        var mouseRectangle = new Rectangle(_currentMouse.X, _currentMouse.Y, 1, 1);
        _isHovering = false;
        if (mouseRectangle.Intersects(Rectangle))
        {
            _isHovering = true;
            if (_currentMouse.LeftButton == ButtonState.Released &&      _previousMouse.LeftButton == ButtonState.Pressed)
              {
                Click?.Invoke(this, new EventArgs());
            }
        }
    }
}

Solution

  • The signature of an event handler is almost always:

    void SomethingHappened(object sender, EventArgs e)
    

    When you subscribe to this event via your method, you have to follow its defined method signature, and that includes using the same return type, in this case void.

    The only way you would be able to return something in this case is to create a variable that you can access later and assign to it, or (if you define the event handler) redefine the method signature to return GameState.

    Edit:

    As per your linked code, it looks like you have the right idea in your fix. As a convention, you shouldn't return anything from the EventHandler, and instead have a member variable that you can set when the event is fired and access it later:

    GameState CurrentState = GameState.playing;
    
    private void Playbutton_Click(object sender, EventArgs e)
    {
        GameState CurrentState = GameState.playing;
    }