0

私はXnaが初めてで、それを押して離すとmiコードの別のゲームステートに移動するイメージを作成するのに助けが必要です。そのため、検索を行い、私の問題を解決する人を見つけて、

     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 は public bool であり、メインコードで呼び出します。

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

1 に答える 1

0

あなたがすべきことはTouchLocationState、タッチがリリースされたときを検出するために、前のすべてのサイクルを取得することです。その瞬間に、gameState必要なメソッドを変更するか、呼び出すだけです。

とにかく、私はあなたが a を宣言したのを見ました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;
      }
}
于 2013-09-01T02:15:43.087 に答える