“Windows Phone图像为”按钮

本文关键字:按钮 图像 Windows Phone | 更新日期: 2023-09-27 18:29:50

我是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是一个公共的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);
    }
}

“Windows Phone图像为”按钮

您应该做的是在每个周期获取上一个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;
      }
}