限制状态事件下的操作c#

时间:2013-11-15 11:25:54

标签: c# xna

我正在尝试创建一个可以一次打开和关闭多个屏幕的screenManager。我按下了P键,打开了一个名为Pause的屏幕。但是,在调试语句中,我可以看到正在按P键创建和打开多次屏幕。我可以通过在创建/添加到屏幕列表之前检查屏幕变量是否为空来停止此操作。然而,在尝试处理它时,我得到了StackOverflow异常。她是我的代码:

Pause.cs-如果我不在这里调用Dispose(),我无法重新打开MainMenu中的屏幕,因为暂停不再为空。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace RoomGame
{
    public class Screen : IDisposable
    {
        public ScreenController screenController;
        public bool isUpdating=true;
        public bool isDrawn=true;
        public GraphicsDeviceManager graphics;

        /*public Screen(ScreenController sc)
        {
            this.screenController = sc;
            this.isActive = true;
        }*/

        public virtual void Initialize()
        { }

        public virtual void LoadContent(ContentManager c)
        { }


        public virtual void Update(GameTime gameTime)
        { }

        public virtual void Draw(GameTime gameTime,SpriteBatch sb)
        { }

        public void Dispose()
        {
            this.Dispose();
        }
    }
}

Screen.cs - StackOVerflow异常发生在Dispose here

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;

namespace RoomGame
{
    public class Screen : IDisposable
    {
        public ScreenController screenController;
        public bool isUpdating=true;
        public bool isDrawn=true;
        public GraphicsDeviceManager graphics;

        /*public Screen(ScreenController sc)
        {
            this.screenController = sc;
            this.isActive = true;
        }*/

        public virtual void Initialize()
        { }

        public virtual void LoadContent(ContentManager c)
        { }


        public virtual void Update(GameTime gameTime)
        { }

        public virtual void Draw(GameTime gameTime,SpriteBatch sb)
        { }

        public void Dispose()
        {
            this.Dispose();
        }
    }
}

MainMenu.cs - 这是创建“暂停”屏幕的类

using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Input;

namespace RoomGame
{
    public class MainMenu : Screen
    {
        GameController gameController;
        ScreenController screenController;
        MyTexture start;
        Rectangle area;
        Vector2 position;
        ContentManager content;
        Screen pause = null;
        //public GraphicsDeviceManager graphics;

        public MainMenu(GameController gc)
        {
            this.gameController = gc;
            gameController.Game.IsMouseVisible = true;
            //this.content = c;
            this.isUpdating = true;
            this.isDrawn = true;

            //this.graphics = this.gameController.game.graphics;
        }

        public override void LoadContent(ContentManager c)
        {

            position = new Vector2(100, 100);
            start = new MyTexture("Textures/Start",position,c);


            //texture = this.gameController.game.Content.Load<Texture2D>("Textures/Start");
            //texture=this.gameController.content.Load<Texture2D>("Textures/Start");
            //texture = this.gameController.Game.Content.Load<Texture2D>("Textures/Start");
        }


        public override void Update(GameTime gameTime)
        {
            //texture = this.gameController.content.Load<Texture2D>("Textures/Start");

            area = start.getBounds();

            MouseState mouse = Mouse.GetState();
            if ((mouse.LeftButton == ButtonState.Pressed) &&
            (area.Contains(mouse.X, mouse.Y)))
            {
                gameController.Game.Exit();
            }

            KeyboardState key = Keyboard.GetState();
            if (key.IsKeyDown(Keys.P))
            {
                if (pause == null)
                {
                    pause = new Pause(this.gameController);
                    gameController.screenController.AddScreen(pause);
                }
            }

        }

        public override void Draw(GameTime gameTime,SpriteBatch sb)
        {
            start.Draw(sb);
        }
    }
}
编辑:我的错误。调试状态位于错误的位置,因此当它显示多个添加时,List.Contains语句阻止它实际发生。

1 个答案:

答案 0 :(得分:1)

要确保与按键相关的代码只执行一次,请存储上一帧中的KeyboardState并按以下方式检查:

public override void Update(GameTime gameTime)
{
    //everything else goes here

    if (key.IsKeyDown(Keys.P) && oldState.IsKeyUp(Keys.P))
    {
        //create a screen
    }

    oldState = key;
}