2
\$\begingroup\$

My code is unable to accept keyboard inputs in a specific class.

I know that I have used inheritance for that class, but I'm pretty sure that it is not the main reason why it doesn't work.

Can you guys look over my code to see if you can help me with my specific issue? The problem is in the Update() in HowToPlayMenu.cs.

HowToPlayMenu.cs (derived class)

using System;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Our_First_Game
{
    class HowToPlayMenu : SplashScreen
    {
        private static KeyboardState keyOldState, keyNewState;

        public HowToPlayMenu(Texture2D Instructions) : base(Instructions)
        {
            image = Instructions;
            endScreen = true;
        }

        public override void Update()
        {
            if (endScreen && keyNewState.IsKeyDown(Keys.M))
            {
                endScreen = false;
                Console.WriteLine("end Screen false!");
            }
            else if (!endScreen && keyNewState.IsKeyDown(Keys.M) && keyOldState.IsKeyUp(Keys.M))
            {
                endScreen = true;
                Console.WriteLine("end Screen true!");
            }
        }

        public override void Draw(SpriteBatch spriteBatch)
        {
            base.Draw(spriteBatch);
        }
    }
}

SplashScreen.cs (base class)

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace Our_First_Game
{
    class SplashScreen
    {
        protected Texture2D image;
        protected bool endScreen = false;

        public SplashScreen(Texture2D SplashImage)
        {
            image = SplashImage;
        }

        public virtual void Update()
        {
            if (!endScreen)
            {
                if (Keyboard.GetState().IsKeyDown(Keys.Space))
                {
                    endScreen = true;
                    Game1.isGameActive = true;
                    Game1.reload1 = 0;
                    Game1.reload2 = 0;
                }
            }
        }

        public virtual void Draw(SpriteBatch spriteBatch)
        {
            if (!endScreen)
            {
                Game1.isGameActive = false;
                spriteBatch.Draw(image, new Rectangle(0 ,0 , 800, 480), Color.White);
            }
        }
    }
}
\$\endgroup\$
4
\$\begingroup\$

You're not updating keyOldState or keyNewState. You probably meant to add keyOldState = keyNewState; keyNewState = Keyboard.GetState(); to the beginning of your Update() method.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.