I am currently making game and i got stuck making the menue. I finished most of it but ther is one thing i can't solve on my own... i made a button class:
public class Button
{
public Texture2D texture1;
Texture2D texture2;
Texture2D texture3;
Vector2 position;
Rectangle rectangle;
public Vector2 size;
public bool isMousOver;
public bool isClicked;
public bool isPressed;
public Button(int texture1, int texture2, int texture3, int position_x, int position_y)
{
this.texture1 = Game_class.menue[texture1];
this.texture2 = Game_class.menue[texture2];
this.texture3 = Game_class.menue[texture3];
this.position.X = position_x;
this.position.Y = position_y;
this.isMousOver = false;
this.isClicked = false;
this.isPressed = false;
this.size = new Vector2(Game_class.menue[texture1].Width / 2, Game_class.menue[texture1].Height / 2);
rectangle = new Rectangle((int)position.X, (int)position.Y, (int)size.X, (int)size.Y);
}
public Button(int texture1, int texture2, int texture3, int position_x, int position_y, float x, float y)
{
this.texture1 = Game_class.menue[texture1];
this.texture2 = Game_class.menue[texture2];
this.texture3 = Game_class.menue[texture3];
this.position.X = position_x;
this.position.Y = position_y;
this.isMousOver = false;
this.isClicked = false;
this.isPressed = false;
this.size = new Vector2(Game_class.menue[texture1].Width / x, Game_class.menue[texture1].Height / y);
rectangle = new Rectangle((int)position.X, (int)position.Y, (int)size.X, (int)size.Y);
}
public void Update(MouseState mouse)
{
Rectangle mouseRectangle = new Rectangle(mouse.X, mouse.Y, 1, 1);
if (mouseRectangle.Intersects(rectangle))
{
this.isMousOver = true;
if (mouse.LeftButton == ButtonState.Pressed)
{
isPressed = true;
this.isClicked = true;
}
else
{
this.isClicked = false;
isPressed = false;
}
}
else
{
this.isMousOver = false;
this.isClicked = false;
}
}
public void Draw(SpriteBatch spriteBatch)
{
if (isMousOver == true)
{
if (isPressed == true)
{
spriteBatch.Draw(texture3, rectangle, Color.White);
}
else
{
spriteBatch.Draw(texture2, rectangle, Color.White);
}
}
else
{
spriteBatch.Draw(texture1, rectangle, Color.White);
}
}
}
as you can see my button uses 3 different textures. One for the normal-state, one for the MouseOver-State and the last one for the isClicked-state. The problem now is everything is working except the isCklicked state. I know how to solve it but not exatcly how to realize it... the problem is that the button should do its click-event after i released the left mouse button, not by clicking it... so there will be enough time for the icClicked animation and it will stop doing the click-event as long as the button is clicked, because this makes it hard to choose a option to be eiher on or off...
So i just need a way to tell the game to stay ready when the button is clicked and only playing the click event after releasing the button, if you know what i mean.
So, any idears?
PS: Sorry for my bad english but i hope i could tell you my problem in an understandable way ^^
use this:
public void Update(MouseState mouse)
{
MouseState lastmousestate, currentmousestate;
lastmousestate = currentmousestate;
currentmousestate = Mouse.GetState();
Rectangle mouseRectangle = new Rectangle(mouse.X, mouse.Y, 1, 1);
if (mouseRectangle.Intersects(rectangle))
{
this.isMousOver = true;
if (lastmousestate.LeftButton == ButtonState.Released && currentmousestate.LeftButton == ButtonState.Pressed)
{
isPressed = true;
this.isClicked = true;
}
else
{
this.isClicked = false;
isPressed = false;
}
}
else
{
this.isMousOver = false;
this.isClicked = false;
}
}
If it solves the problem make sure to come back and mark the answer as correct so others can benefit from your question :)
This is the complete Button-Class with the correctly working code:
public class Button
{
public Texture2D texture1;
MouseState currentMouseState;
MouseState lastMouseState;
Texture2D texture2;
Texture2D texture3;
Vector2 position;
Rectangle rectangle;
public Vector2 size;
public bool isMousOver;
public bool isClicked;
public bool isPressed;
public Button(int texture1, int texture2, int texture3, int position_x, int position_y)
{
this.texture1 = Game_class.menue[texture1];
this.texture2 = Game_class.menue[texture2];
this.texture3 = Game_class.menue[texture3];
this.position.X = position_x;
this.position.Y = position_y;
this.isMousOver = false;
this.isClicked = false;
this.isPressed = false;
this.size = new Vector2(Game_class.menue[texture1].Width / 2, Game_class.menue[texture1].Height / 2);
rectangle = new Rectangle((int)position.X, (int)position.Y, (int)size.X, (int)size.Y);
}
public Button(int texture1, int texture2, int texture3, int position_x, int position_y, float x, float y)
{
this.texture1 = Game_class.menue[texture1];
this.texture2 = Game_class.menue[texture2];
this.texture3 = Game_class.menue[texture3];
this.position.X = position_x;
this.position.Y = position_y;
this.isMousOver = false;
this.isClicked = false;
this.isPressed = false;
this.size = new Vector2(Game_class.menue[texture1].Width / x, Game_class.menue[texture1].Height / y);
rectangle = new Rectangle((int)position.X, (int)position.Y, (int)size.X, (int)size.Y);
}
public void Update(MouseState mouse)
{
Rectangle mouseRectangle = new Rectangle(mouse.X, mouse.Y, 1, 1);
lastMouseState = currentMouseState;
currentMouseState = Mouse.GetState();
if (mouseRectangle.Intersects(rectangle))
{
this.isMousOver = true;
if (currentMouseState.LeftButton == ButtonState.Pressed || lastMouseState.LeftButton == ButtonState.Pressed)
{
isPressed = true;
if (lastMouseState.LeftButton == ButtonState.Pressed && currentMouseState.LeftButton == ButtonState.Released)
{
this.isClicked = true;
}
else
{
this.isClicked = false;
}
}
else
{
isPressed = false;
isClicked = false;
}
}
else
{
this.isMousOver = false;
this.isClicked = false;
}
}
public void Draw(SpriteBatch spriteBatch)
{
if (isMousOver == true)
{
if (isPressed == true)
{
spriteBatch.Draw(texture3, rectangle, Color.White);
}
else
{
spriteBatch.Draw(texture2, rectangle, Color.White);
}
}
else
{
spriteBatch.Draw(texture1, rectangle, Color.White);
}
}
}