有错误的 return 类型 monogame
Has the wrong return type monogame
我试图制作一个按钮,当我点击它时,它会将游戏状态更改为正在玩,然后调用所有可以玩游戏的东西,但是我无法获得 return 游戏统计数据的方法。
private GameState Playbutton_Click(object sender, EventArgs e)
{
GameState CurrentState = GameState.playing;
return CurrentState;
}
我得到的错误是
'Game1.GameState Game1.Playbutton_Click(object, EventArgs)' 有错误的 return 类型
编辑:
在游戏 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;
}
按钮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());
}
}
}
}
事件处理程序的签名几乎总是:
void SomethingHappened(object sender, EventArgs e)
当您通过您的方法订阅此事件时,您必须遵循其定义的方法签名,这包括使用相同的 return 类型,在本例中为 void
.
在这种情况下,您能够 return 的唯一方法是创建一个您可以稍后访问并分配给它的变量,或者(如果您定义了事件处理程序)重新定义方法签名至 return GameState
.
编辑:
根据您的链接代码,您的修复思路似乎是正确的。作为惯例,您不应该 return 来自 EventHandler 的任何内容,而是拥有一个成员变量,您可以在触发事件时设置该成员变量并稍后访问它:
GameState CurrentState = GameState.playing;
private void Playbutton_Click(object sender, EventArgs e)
{
GameState CurrentState = GameState.playing;
}
我试图制作一个按钮,当我点击它时,它会将游戏状态更改为正在玩,然后调用所有可以玩游戏的东西,但是我无法获得 return 游戏统计数据的方法。
private GameState Playbutton_Click(object sender, EventArgs e)
{
GameState CurrentState = GameState.playing;
return CurrentState;
}
我得到的错误是 'Game1.GameState Game1.Playbutton_Click(object, EventArgs)' 有错误的 return 类型
编辑:
在游戏 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;
}
按钮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());
}
}
}
}
事件处理程序的签名几乎总是:
void SomethingHappened(object sender, EventArgs e)
当您通过您的方法订阅此事件时,您必须遵循其定义的方法签名,这包括使用相同的 return 类型,在本例中为 void
.
在这种情况下,您能够 return 的唯一方法是创建一个您可以稍后访问并分配给它的变量,或者(如果您定义了事件处理程序)重新定义方法签名至 return GameState
.
编辑:
根据您的链接代码,您的修复思路似乎是正确的。作为惯例,您不应该 return 来自 EventHandler 的任何内容,而是拥有一个成员变量,您可以在触发事件时设置该成员变量并稍后访问它:
GameState CurrentState = GameState.playing;
private void Playbutton_Click(object sender, EventArgs e)
{
GameState CurrentState = GameState.playing;
}