代码错误屏幕淡入淡出过渡失败

时间:2014-08-24 18:10:57

标签: c# xna

当我开始调试我的代码时,它加载正常但只有淡入时我按Z键Enter键不会导致淡入淡出或下一个带有文本“标题屏幕”的菜单出现我想知道是否有人可以告诉我在哪里我出错了上传下面的完整代码。 附:没有错误信息。

游戏开始时会发生什么“启动画面”  应该出现在屏幕上按下输入淡出屏幕到标题屏幕说“标题屏幕”,启动屏幕加载,当按下Z时,淡入淡出动画发生但Enter键对游戏有0效果。

namespace Platformer
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

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

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            Screen_Manager.Instance.Initiaize();

            Screen_Manager.Instance.Dimensions = new Vector2(800, 600);
            graphics.PreferredBackBufferWidth = (int)Screen_Manager.Instance.Dimensions.X;
            graphics.PreferredBackBufferHeight = (int)Screen_Manager.Instance.Dimensions.Y;
            graphics.ApplyChanges();



            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);

            Screen_Manager.Instance.LoadContent(Content);
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here

        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();

            Screen_Manager.Instance.Update(gameTime);
            base.Update(gameTime);


        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);


            spriteBatch.Begin();
            Screen_Manager.Instance.Draw(spriteBatch);
            spriteBatch.End();
            base.Draw(gameTime);
        }
    }
}
{
    public class Game_Screen
    {
        protected ContentManager content;

        public virtual void LoadContent(ContentManager Content) 
        {
            content = new ContentManager(Content.ServiceProvider, "content");
        }

        public virtual void UnloadContent() 
        {
            content.Unload();
        }

        public virtual void Update(GameTime gameTime) 
        { 

        }

        public virtual void Draw(SpriteBatch spriteBatch) 
        { 

        }
    }
}

{
    public class Screen_Manager
    {

        #region variables

        ContentManager content;                     //  creating custom ContentManger

        Game_Screen currentScreen;                  // screen being currently displayed

        Game_Screen newScreen;                      // new screen taking effect

        private static Screen_Manager instance;     // Screen Manger instance


       // Dictionary<string, Game_Screen> screens = new Dictionary<string, Game_Screen>();     // Game Screen storage

        Stack<Game_Screen> screenStack = new Stack<Game_Screen>();                           //  screens Stack

        Vector2 dimensions;                                                                  // width&Height of Screens  

        bool transition;

        Fade_Animation fade;

        Texture2D fadeTexture;



        #endregion

        #region Properties

        public static Screen_Manager Instance
        {
            get
            {
                if (instance == null)
                    instance = new Screen_Manager();
                return instance;
            }
        }

        public Vector2 Dimensions
        { 
            get { return dimensions; }
            set { dimensions = value; }
        }


        #endregion

        #region Main Methods

        public void AddScreen(Game_Screen screen)
        {
            transition = true;
            newScreen = screen;
            fade.IsActive = true;
            fade.Alpha = 1.0f;
            fade.ActivateValue = 1.0f;  
        }

        public void Initiaize() 
        {
            currentScreen = new Splash_Screen();
            fade = new Fade_Animation();
        }

        public void LoadContent(ContentManager Content)
        {
            content = new ContentManager(Content.ServiceProvider, "Content");
            currentScreen.LoadContent(Content);

            fadeTexture = content.Load<Texture2D>("fade");
            fade.LoadContent(content, fadeTexture, "", Vector2.Zero);
            fade.Scale = dimensions.X;
        }
        public virtual void Update(GameTime gameTime)
        {
            if (!transition)
                 currentScreen.Update(gameTime);
            else 
                Transition(gameTime);
        }

        public virtual void Draw(SpriteBatch spriteBatch) 
        { 
            currentScreen.Draw(spriteBatch);
            if (transition)
                fade.Draw(spriteBatch);
        }

        #endregion

        #region Provate Methods

        private void Transition(GameTime gameTime)
        {
            fade.Update(gameTime);
                if(fade.Alpha == 1.0f && fade.Timer.TotalSeconds == 1.0f)
                {
                    screenStack.Push(newScreen);
                    currentScreen.UnloadContent();
                    currentScreen = newScreen;
                    currentScreen.LoadContent(content);
                }
                else if (fade.Alpha == 0.0f)
                {
                    transition =false;
                    fade.IsActive = false;

                }
        }


        #endregion

    }
}
{
    public class Splash_Screen : Game_Screen
    {
        KeyboardState keystate;
        SpriteFont font;

        public override void LoadContent(ContentManager Content)
        {
            base.LoadContent(Content);
            if (font == null)
                font = content.Load<SpriteFont>("Font1");
        }

        public override void UnloadContent()
        {
            base.UnloadContent();
        }

         public override void Update(GameTime gameTime)
         {
             keystate = Keyboard.GetState();
             if (keystate.IsKeyDown(Keys.Z))
                 Screen_Manager.Instance.AddScreen(new Title_Screen());
         }

         public override void Draw(SpriteBatch spriteBatch)
         {
             spriteBatch.DrawString(font, "SplashScreen",
                 new Vector2(100, 100), Color.Black);
         }

    }
}
{
    public class Title_Screen : Game_Screen
    {
        KeyboardState keystate;
        SpriteFont font;

        public override void LoadContent(ContentManager Content)
        {
            base.LoadContent(Content);
            if (font == null)
                font = content.Load<SpriteFont>("Font1");
        }

        public override void UnloadContent()
        {
            base.UnloadContent();
        }

        public override void Update(GameTime gameTime)
        {
            keystate = Keyboard.GetState();
            if (keystate.IsKeyDown(Keys.Enter))
                Screen_Manager.Instance.AddScreen(new Splash_Screen());
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.DrawString(font, "Title Screen",
                new Vector2(100, 100), Color.Black);
        }

    }
}
{
    public class Animation
    {
        protected Texture2D image;
        protected string text;
        protected SpriteFont font;
        protected Color color;
        protected Rectangle sourceRect;
        protected float rotation, scale, axis, alpha;
        protected Vector2 origin, position;
        protected ContentManager content;
        protected bool isActive;


        public virtual float Alpha 
        {
            get { return alpha; }
            set { alpha = value; }
        }




        public bool IsActive
        { 
            set { isActive = value; }
            get { return isActive; }
        }

        public float Scale
        {
            set { scale = value; }
           // get { return scale; }
        }

        public virtual void LoadContent(ContentManager Content, Texture2D image, 
            string text, Vector2 position)
        {
            content = new ContentManager(Content.ServiceProvider, "Content");
            this.image = image;
            this.text = text;
            this.position = position;
            if (text != string.Empty)
            {

                font = Content.Load<SpriteFont>("Font1");
                color = new Color(114, 77, 225);
            }
            if (image != null)
                sourceRect = new Rectangle(0, 0, image.Width, image.Height);
            rotation = 0.0f;
            axis = 0.0f;
            scale = 1.0f;
            alpha = 1.0f;
            isActive = false;

        }

        public virtual void UnloadContent()
        {
            content.Unload();
            text = string.Empty;
            position = Vector2.Zero;
            sourceRect = Rectangle.Empty;
            image = null;


        }
        public virtual void Update(GameTime gameTime)
        {

        }

        public virtual void Draw(SpriteBatch spriteBatch)
        {
            if (image != null)
            {
                origin = new Vector2(sourceRect.Width / 2, 
                    sourceRect.Height / 2);
                spriteBatch.Draw(image, position + origin, sourceRect,
                    Color.White * alpha, rotation, origin, scale,
                    SpriteEffects.None, 0.0f);
            }

            if (text != String.Empty)
            {
                origin = new Vector2(font.MeasureString(text).X / 2,
                    font.MeasureString(text).Y / 2);
                spriteBatch.DrawString(font, text, position + origin,
                    color * alpha, rotation, origin, scale, SpriteEffects.None,
                    0.0f);

            }
        }
    }
}
{
    class Fade_Animation : Animation
    {
        bool increase;
        float fadespeed;
        TimeSpan defaultTime, timer;
        bool startTimer;
        float activatevalue;
        bool stopUpdateing;
        float defaultAlpha;

        public TimeSpan Timer
        {
            get { return timer; }
            set { defaultTime = value; timer = defaultTime; }
        }

        public float FadeSpeed
        {
            get { return fadespeed; }
            set { fadespeed = value; }
        }

        public override float Alpha
        {
            get
            {
                return alpha;
            }
            set
            {
                alpha = value;

                if (alpha == 1.0f)
                    increase = false;
                else if (alpha == 0.0f)
                    increase = true;

            }
        }

        public float ActivateValue
        {
            get { return activatevalue; }
            set { activatevalue = value; }
        }

        public override void LoadContent(ContentManager Content, 
            Texture2D image, string text, Vector2 position)
        {
            base.LoadContent(Content, image, text, position);
            increase = false;
            fadespeed = 1.0f;
            defaultTime = new TimeSpan(0, 0, 1);
            timer = defaultTime;
            activatevalue = 0.0f;
            stopUpdateing = false;
            defaultAlpha = alpha;
        }
        public override void Update(GameTime gameTime)
        {
            if (isActive)
            {
                if (!stopUpdateing)
                {
                    if (!increase)
                        alpha -= fadespeed * (float)gameTime.ElapsedGameTime.TotalSeconds;
                    else
                        alpha += fadespeed * (float)gameTime.ElapsedGameTime.TotalSeconds;

                    if (alpha <= 0.0f)
                        alpha = 0.0f;
                    else if (alpha >= 1.0f)
                             alpha = 1.0f;    
                }

                if (alpha == activatevalue)
                {
                    stopUpdateing = true;
                    timer -= gameTime.ElapsedGameTime;
                    if (timer.TotalSeconds <= 0)
                    {
                        increase = !increase;
                        timer = defaultTime;
                        stopUpdateing = false;


                    }

                }
            }
            else
            {
                alpha = defaultAlpha;
            }
        } 
    }
}

0 个答案:

没有答案