2012-03-03 20 views
0

我在WP7上做遊戲。我在其中添加了一個按鈕。我想要做的是,只要我觸摸按鈕,按鈕精靈應該將其框架從0更改爲1(0代表按鈕框架,1代表按鈕按下框架)。我已經添加了包含兩個幀的按鈕的精靈表。大小也沒關係。XNA:有條件抽籤

總之,我想這樣做:

1)當觸摸按鈕 2)按鈕按下幀應繪製和後1或2秒回來原始幀。 (就像在每個帶按鈕的遊戲中發生的一樣)。

回答

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

(注意沒有測試此代碼)
希望幫助你開始,如果你想在一個特定的更多信息隨意闡述部分。
最大