2013-09-01 66 views
0

我是Xna的新手,我需要幫助製作圖像,當我按下它並釋放它時,會轉到另一個mi代碼的遊戲狀態,所以我搜索並找到他們告訴他的問題要做到這一點Windows Phone圖像作爲按鈕

 TouchLocation tl; 
    TouchCollection touchCollection = TouchPanel.GetState(); 


     if (tl.State == TouchLocationState.Pressed) 
     { 

      if (tl.State == TouchLocationState.Pressed) 
       if (ButtonRecPlay.Contains(new Point((int)tl.Position.X, (int)tl.Position.Y))) 
       { 
        IsPlayClicked = true; 
       } 
     } 

我試試這個,但是當我按下按鈕沒有發生的IsPlayClicked是一個公共布爾 ,我在這裏把它在我的maincode它不會工作是我的主要代碼:

public class MainFile : Microsoft.Xna.Framework.Game 
{ 
    GraphicsDeviceManager graphics; 
    SpriteBatch spriteBatch; 
    ScreenStates screentState; 

    Rectangle TouchS_Y_X; 

    Logo logo; 
    Menu0 menu; 
    Choose_Pets choose_pets; 
    ScreenStates.CurrentGameState GameState; 

    public MainFile() 
    { 
     graphics = new GraphicsDeviceManager(this); 
     Content.RootDirectory = "Content"; 

     // Frame rate is 30 fps by default for Windows Phone. 
     TargetElapsedTime = TimeSpan.FromTicks(333333); 

     // Extend battery life under lock. 
     InactiveSleepTime = TimeSpan.FromSeconds(1); 

    } 


    protected override void Initialize() 
    { 
     // TODO: Add your initialization logic here 
     screentState = new ScreenStates(); 
     logo = new Logo(); 
     menu = new Menu0(); 
     choose_pets = new Choose_Pets(); 
     base.Initialize(); 

    } 


    protected override void LoadContent() 
    { 
     TouchS_Y_X = new Rectangle(0, 0, 1, 1); 
     // Create a new SpriteBatch, which can be used to draw textures. 
     spriteBatch = new SpriteBatch(GraphicsDevice); 
     GameState = ScreenStates.CurrentGameState.Logo; 

     this.logo.Load(this.Content, this.GraphicsDevice); 
     this.menu.Load_Menu(GraphicsDevice, Content); 
     this.choose_pets.Load_ChoosePet(Content, GraphicsDevice); 

     base.LoadContent(); 
    } 


    protected override void UnloadContent() 
    { 
     // TODO: Unload any non ContentManager content here 
     //this.logo.Unload_logo(Content); 
    } 

    protected override void Update(GameTime gameTime) 
    { 
     // Allows the game to exit 
     if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) 
      this.Exit(); 

     #region Games States 

     switch (GameState) 
     { 
      case ScreenStates.CurrentGameState.Logo: 
        logo.Update_logo(gameTime); 

        if (logo.FadeOut_logo == true) 
         GameState = ScreenStates.CurrentGameState.Menu; 
        break; 

      case ScreenStates.CurrentGameState.Menu: 
       menu.Update_Menu(gameTime); 

       if (menu.IsPlayClicked == true) 
        GameState = ScreenStates.CurrentGameState.CharactersChooser; 

       break; 

      case ScreenStates.CurrentGameState.CharactersChooser: 
        choose_pets.Update_petchoose(gameTime); 
       break; 
     } 
     #endregion 

     // TODO: Add your update logic here 
     base.Update(gameTime); 
    } 


    protected override void Draw(GameTime gameTime) 
    { 
     GraphicsDevice.Clear(Color.Black); 
     spriteBatch.Begin(); 
     #region GameStateDraw 



     switch(GameState) 
     { 
      case ScreenStates.CurrentGameState.Logo:   
      logo.Draw(spriteBatch);  
      break; 

       //Menu Draw State 
      case ScreenStates.CurrentGameState.Menu: 
      menu.Draw_Menu(spriteBatch); 
      break; 

      case ScreenStates.CurrentGameState.CharactersChooser: 
       choose_pets.Draw_petChoose(spriteBatch); 
      break; 
     } 

     #endregion 

     spriteBatch.End(); 
     base.Draw(gameTime); 
    } 
} 

,這是我的Menu0文件,我將它用於遊戲菜單和它有想在這裏做了按下按鈕的代碼是代碼:

class Menu0 
    { 
    Texture2D Text2D_Background; 
    Rectangle Rec_Background; 

    Texture2D ButtonText2DPlay; 
    Rectangle ButtonRecPlay; 

    Boolean Music_playing = false; 
    public Song BackMusic; 

    TouchLocation tl; 
    TouchCollection touchCollection = TouchPanel.GetState(); 

    public bool IsPlayClicked = false; 

    MainFile main = new MainFile(); 

    public void Load_Menu(GraphicsDevice Graphics_Menu, ContentManager Content_Menu) 
    { 

     //main = new MainFile(); 

     Text2D_Background = Content_Menu.Load<Texture2D>("UI\\MenuBack"); 
     Rec_Background = new Rectangle(0, 0, Graphics_Menu.Viewport.Width, Graphics_Menu.Viewport.Height); 

     ButtonText2DPlay = Content_Menu.Load<Texture2D>("UI\\Buttons\\New Games"); 
     ButtonRecPlay = new Rectangle(350, 250, 150, 60); 

     BackMusic = Content_Menu.Load<Song>("UI\\Music_01"); 

    } 

    public void Update_Menu(GameTime Menu_GameTime) 
    { 
     if (Music_playing == false) 
     { 
      MediaPlayer.Play(BackMusic); 
      Music_playing = true; 
     } 
     MediaPlayer.Resume(); 


     if (tl.State == TouchLocationState.Pressed) 
     { 

      if (tl.State == TouchLocationState.Pressed) 
       if (ButtonRecPlay.Contains(new Point((int)tl.Position.X, (int)tl.Position.Y))) 
       { 
        IsPlayClicked = true; 
       } 
     } 
    } 

    public void Draw_Menu(SpriteBatch Spritebatch_Menu) 
    { 
     Spritebatch_Menu.Draw(Text2D_Background, Rec_Background, Color.White); 
     Spritebatch_Menu.Draw(ButtonText2DPlay, ButtonRecPlay, Color.White); 
    } 
} 

回答

0

你應該做的是讓以前TouchLocationState每個週期中,爲了當觸摸是剛發佈的檢測。在那一刻,您只需更改您的gameState或撥打任何您需要的方法。

反正我看你已經聲明瞭一個TouchCollection但你永遠不使用它:

TouchCollection touchCollection = TouchPanel.GetState(); 

您需要掃描的是收集和分析每TouchLocation它,或者僅僅是最後一個:

if (touchCollection.Count > 0) 
{ 
    TouchLocation tl = touchCollecion.Last(); 

    if (tl.State == TouchLocationState.Released && previousState == TouchLocationState.Pressed) 
     { 
     if (ButtonRecPlay.Contains((int)tl.Position.X, (int)tl.Position.Y)) 
      IsPlayClicked = true; 
     } 
} 
+0

許多Thx爲你回答我會立即嘗試我現在​​有問題我告訴你,如果它工作,當我修復問題xDD – user2727653

+0

AAh!許多thx它的工作我想給你一個upvote,但我沒有代表唯一的事情,我可以給你很多thx xDDDDD^- ^/thxx! – user2727653

+0

大聲笑不要擔心 – pinckerman