[英]XNA button click not fired
I created a button class and i am calling it inside main Game class. 我创建了一个按钮类,并在游戏主类中调用它。 But it is not firing.
但这不是射击。 Actually i want to firing Update method which inside of Button class when i clicked to left button on the mouse.
实际上,我想在单击鼠标左键时触发Button类内部的Update方法。 I am checking all mouse event via Debug.WriteLine everything is looking like normally.
我正在通过Debug.WriteLine检查所有鼠标事件,一切看起来都正常。 What could be wrong?
有什么事吗
GameBase class 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);
}
}
Button class 按钮类
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);
}
}
Your logic is a bit messed up. 您的逻辑有点混乱。 In a sentence, your code is: if button is clicked, do update in which you check if it's clicked.
一句话,您的代码是:如果单击了按钮,请进行更新以检查是否被单击。 This way you will never update your button.
这样,您将永远不会更新按钮。
Try this modified version of your Update
function: 尝试使用此
Update
版本的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);
}
This code in a sentence means: do update of button which check if button is clicked, and after that, if button is clicked, wtride "Clicked" in debug and set gameState to Playing. 这句话中的代码表示:更新按钮以检查是否单击了按钮,然后,如果单击了按钮,则在调试中添加“ Clicked”并将gameState设置为Playing。
Edit 1: 编辑1:
Also, I don't see how color is supposed to be related to the clicking. 另外,我看不到颜色应该如何与点击相关。 In buttons Update methd, I am worried about this peace of code
在按钮Update methd中,我担心代码的这种和平性
if (mouse.LeftButton == ButtonState.Pressed)
{
Clicked = true;
}
else if (_color.A < 255)
{
_color.A += 3;
Clicked = false;
}
I'd rather put something like this: 我宁愿这样写:
if (mouse.LeftButton == ButtonState.Pressed)
{
Clicked = true;
}
else
{
if (_color.A < 255)
_color.A += 3;
Clicked = false;
}
This way it's simpler to put this code in a sentence, and this simplicity makes it work no matter what value _color.A is, but _color.A will also be updated when you need it. 这样,将这段代码放在句子中就更简单了,这种简单性使其无论_color.A的值是什么都可以工作,但是_color.A也会在需要时更新。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.