XNA动画按钮

时间:2014-01-28 12:07:56

标签: c# button xna animated

我目前正在制作游戏,而我却陷入困境。我完成了大部分工作但是我自己无法解决的一件事......我做了一个按钮课程:

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);
        }

    }
}

你可以看到我的按钮使用了3种不同的纹理。一个用于正常状态,一个用于MouseOver-State,最后一个用于isClicked状态。现在的问题是除了isCklicked状态之外一切正常。我知道如何解决它,但不知道如何实现它...问题是按钮应该在我发布鼠标左键后点击事件,而不是点击它...所以会有足够的时间icClicked动画,只要单击该按钮,它就会停止执行点击事件,因为这样就很难选择要打开或关闭的选项......

所以我只需要一种方法来告诉游戏在点击按钮时保持准备就绪,只有在释放按钮后才播放点击事件,如果你知道我的意思。

那么,任何想法?

PS:抱歉我的英语不好但我希望我能以一种可以理解的方式告诉你我的问题^^

1 个答案:

答案 0 :(得分:1)

使用它:

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;
    }
}

如果它解决了问题,请务必回来并将答案标记为正确,以便其他人可以从您的问题中受益:)

这是具有正确工作代码的完整Button类:

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);
        }

    }
}