XNA:有条件的抽奖

时间:2012-03-03 22:15:09

标签: button xna

我在WP7上制作游戏。我在其中添加了一个按钮。我想要做的是每当我触摸按钮时,按钮精灵应该将其框架从0更改为1(0是按钮向上框架,1是按钮按下框架)。我已经为按钮添加了精灵表,只包含两个帧。大小也没关系。

总结一下,我想这样做:

1)触摸按钮时 2)应按下按钮框,并在1或2秒后回到原始框架。 (就像每次使用按钮的游戏一样)。

1 个答案:

答案 0 :(得分:0)

听起来更像设计问题,然后是实施问题。基本上,你需要一个让你入门的想法。我不太熟悉WP7,但我会向你展示你可以使用的基本设计;有了一些sudo代码,你可以稍后填写。

class Button
{
    private Point CellSize;
    public Point Position {get; set;}

    private Texture2D buttonSheet;

    public Button (Texture2D texture, Point CellSize)
    {
        this.buttonSheet = texture;
        this.CellSize = CellSize;
    }

    public bool isPressed {get; private set;}

    public void Update (bool isPressed)
    {
        this.isPressed = isPressed;
    }

    public void Draw (SpriteBatch sb)
    {
        Rectangle Source = new Rectangle (0,0,CellSize.X,CellSize.Y);
        if (this.isPressed)
            Source = new Rectangle (CellSize.X * 1, CellSize.Y * 1, CellSize.X, CellSize.Y);
        sb.Draw (this.buttonSheet, new Rectangle(this.Position.X,this.Position.Y,CellSize.X,CellSize.Y), Source, Color.White);
    }
}

然后使用它:

class Game1
{
    Button Start = new Button(new Texture2D() /*load and include your texture here*/, new Point (100, 50) /* 100 wide, 50 tall cells */);
    public Update (GameTime gt)
    {
        bool clicked = false /* Put code here to check if the button is pressed */;
        Start.Update(clicked);
    }

    public Draw (SpriteBatch spriteBatch)
    {
        Start.Draw(spriteBatch);
    }
}

(注意未测试此代码)
希望能帮助您入门,如果您想了解特定部件的更多信息,请随时详细说明 最大