我创建了一个按钮类,我在主游戏类中调用它。但它并没有解雇。实际上我想点击鼠标左键上的Button类里面的Update方法。我通过Debug.WriteLine检查所有鼠标事件,一切看起来都正常。可能有什么不对?
GameBase类
public class GameBase : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
Button btn;
public GameBase()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IsMouseVisible = true;
graphics.PreferredBackBufferWidth = 600;
graphics.PreferredBackBufferHeight = 400;
}
protected override void Initialize()
{
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
btn = new Button(Content.Load<Texture2D>("Sprites/Button"), new Vector2(150, 150));
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
MouseState mouse = Mouse.GetState();
//Debug.WriteLine(mouse);
if (btn.Clicked == true)
{
Debug.WriteLine("Clicked");
currentState = GameState.Playing;
btn.Update(mouse);
}
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
spriteBatch.Draw(Content.Load<Texture2D>("Sprites/StartBg"), new Rectangle(0, 0, _screenWidth, _screenHeight), Color.White);
btn.Draw(spriteBatch);
spriteBatch.End();
base.Draw(gameTime);
}
}
按钮类
public class Button
{
public bool Clicked { get; set; }
private Texture2D _image;
private Rectangle _rectangle, _mouseRectangle;
private Vector2 _coordinate;
private Color _color;
private bool _down;
public Button(Texture2D image, Vector2 coordinate)
{
_image = image;
_coordinate = coordinate;
}
public void Update(MouseState mouse)
{
mouse = Mouse.GetState();
_rectangle = new Rectangle((int)_coordinate.X, (int)_coordinate.Y, _image.Width, _image.Height);
_mouseRectangle = new Rectangle(mouse.X, mouse.Y, 1, 1);
if (_mouseRectangle.Intersects(_rectangle))
{
if (_color.A == 255)
_down = false;
if (_color.A == 0)
_down = true;
if (_down)
{
_color.A += 3;
}
else
{
_color.A -= 3;
}
if (mouse.LeftButton == ButtonState.Pressed)
{
Clicked = true;
}
else if (_color.A < 255)
{
_color.A += 3;
Clicked = false;
}
}
}
public void Draw(SpriteBatch spriteBatch)
{
spriteBatch.Draw(_image, _coordinate, Color.White);
}
}
答案 0 :(得分:0)
你的逻辑有点搞砸了。在一个句子中,您的代码是:如果单击了按钮,请执行更新,检查是否单击了该按钮。这样您就永远无法更新按钮。
尝试Update
功能的此修改版本:
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();
MouseState mouse = Mouse.GetState();
//Debug.WriteLine(mouse);
btn.Update(mouse);
if (btn.Clicked == true)
{
Debug.WriteLine("Clicked");
currentState = GameState.Playing;
}
base.Update(gameTime);
}
句子中的这段代码意味着:更新按钮,检查按钮是否被点击,然后,如果单击按钮,则在调试中单击“Clicked”并将gameState设置为Play。
编辑1:
此外,我没有看到颜色应该与点击有关。
在按钮更新方法,我担心这个代码的和平
if (mouse.LeftButton == ButtonState.Pressed)
{
Clicked = true;
}
else if (_color.A < 255)
{
_color.A += 3;
Clicked = false;
}
我宁愿放这样的东西:
if (mouse.LeftButton == ButtonState.Pressed)
{
Clicked = true;
}
else
{
if (_color.A < 255)
_color.A += 3;
Clicked = false;
}
这样将这段代码放在一个句子中就更简单了,这种简单性使它无论_color.A是什么值都可以工作,但_color.A也会在你需要的时候更新。