【问题标题】:Has the wrong return type monogame有错误的返回类型monogame
【发布时间】:2018-12-09 16:08:22
【问题描述】:

我正在尝试制作一个按钮,当我单击它时,它会将游戏状态更改为正在玩,然后调用将玩游戏的所有内容,但是我无法获得返回游戏统计信息的方法。

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

我得到的错误是 'Game1.GameState Game1.Playbutton_Click(object, EventArgs)' 返回类型错误

编辑:

在第 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;
    }

按钮类:

   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());
            }
        }
    }
}

【问题讨论】:

  • C# 中的所有标准事件处理程序都声明为void,它们“处理”事件,而不是返回数据。如果您想使用事件处理程序更改应用程序的状态,则必须通过调用方法、更改字段或执行其他修改应用程序状态的操作直接从事件处理程序修改它。
  • 在您的事件处理程序中,您正在设置一个私有变量

标签: c# xml monogame


【解决方案1】:

事件处理程序的签名几乎总是:

void SomethingHappened(object sender, EventArgs e)

当您通过您的方法订阅此事件时,您必须遵循其定义的方法签名,其中包括使用相同的返回类型,在本例中为 void

在这种情况下,您能够返回某些内容的唯一方法是创建一个稍后可以访问并分配给它的变量,或者(如果您定义事件处理程序)重新定义方法签名以返回 GameState

编辑:

根据您的链接代码,看起来您的修复思路是正确的。按照惯例,您不应该从 EventHandler 返回任何内容,而是有一个成员变量,您可以在事件触发时设置它并在以后访问它:

GameState CurrentState = GameState.playing;

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

【讨论】:

  • 很抱歉,我是 MonoGame 的新手。我真的不明白我应该怎么做才能修复这个错误
  • @SamCerar 如果可以,请链接您的一些代码,以便我更好地解释它
  • 我们需要从Game1 看到更多信息。具体来说,您的GameState 实例的声明和使用方式/位置。
  • @SamCerar 看到你的代码后,我编辑了我的答案。
  • 但是当我执行这样的代码时,它说它是一个局部变量,因此如果我想在其他任何地方编辑它或在代码中的其他任何地方调用它,它什么都不做
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-04-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-08
相关资源
最近更新 更多