Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 有错误的返回类型单局_C#_Xml_Monogame - Fatal编程技术网

C# 有错误的返回类型单局

C# 有错误的返回类型单局,c#,xml,monogame,C#,Xml,Monogame,我试图做一个按钮,当我点击它时,它会将游戏状态更改为playing,然后调用所有可以玩游戏的东西,但是我无法获得返回游戏状态的方法 private GameState Playbutton_Click(object sender, EventArgs e) { GameState CurrentState = GameState.playing; return CurrentState; } 我得到的错误是 “Game1.GameStat

我试图做一个按钮,当我点击它时,它会将游戏状态更改为playing,然后调用所有可以玩游戏的东西,但是我无法获得返回游戏状态的方法

    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;
    }
事件处理程序的签名几乎总是:

void somethingOccessed(对象发送方,事件参数e)
通过方法订阅此事件时,必须遵循其定义的方法签名,这包括使用相同的返回类型,在本例中为
void

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

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

GameState CurrentState=GameState.playing;
私有void播放按钮\u单击(对象发送者,事件参数e)
{
GameState CurrentState=GameState.playing;
}

C#中的所有标准事件处理程序都声明为
void
,它们“处理”事件,而不是返回数据。如果您想使用事件处理程序更改应用程序的状态,则必须通过调用方法、更改字段或执行其他修改应用程序状态的操作,直接从事件处理程序对其进行修改。在事件处理程序中,您正在设置私有变量。很抱歉,我对MonoGame非常陌生。我真的不明白该怎么做才能解决这个问题error@SamCrerar如果可以,请链接一些代码,以便我更好地解释。我们需要从
Game1
中了解更多内容。具体来说,您的
GameState
实例是如何声明和使用的。@SamCrerar在看到您的代码后,我编辑了我的答案。但是当我这样做代码时,它会说它是一个局部变量,因此,如果我想在代码中的任何其他地方编辑它或调用它,它什么也不做
   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());
            }
        }
    }
}