Я пытаюсь создать кнопку, которая, когда я нажимаю на нее, изменяет игровое состояние на игру, а затем вызывает все, что будет играть в игру, однако я не могу получить метод для возврата игровой характеристики.
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());
}
}
}
}