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 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或调用您需要的任何方法。

无论如何,我看到你已经声明了 aTouchCollection但你从不使用它:

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 回答