Keypress detection wont work after seemingly unrelated code change

Posted by LukeZaz on Game Development See other posts from Game Development or by LukeZaz
Published on 2013-11-04T05:19:19Z Indexed on 2013/11/04 10:20 UTC
Read the original article Hit count: 270

Filed under:
|
|

I'm trying to have the Enter key cause a new 'map' to generate for my game, but for whatever reason after implementing full-screen in it the input check won't work anymore. I tried removing the new code and only pressing one key at a time, but it still won't work.

Here's the check code and the method it uses, along with the newMap method:

public class Game1 : Microsoft.Xna.Framework.Game
{
    // ...

    protected override void Update(GameTime gameTime)
    {
        // ...

        // Check if Enter was pressed - if so, generate a new map
        if (CheckInput(Keys.Enter, 1))
        {
            blocks = newMap(map, blocks, console);
        }

        // ...
    }

    // Method: Checks if a key is/was pressed
    public bool CheckInput(Keys key, int checkType)
    {
        // Get current keyboard state
        KeyboardState newState = Keyboard.GetState();
        bool retType = false; // Return type

        if (checkType == 0)
        {
            // Check Type: Is key currently down?
            if (newState.IsKeyDown(key))
            {
                retType = true;
            }
            else
            {
                retType = false;
            }
        }
        else if (checkType == 1)
        {
            // Check Type: Was the key pressed?
            if (newState.IsKeyDown(key))
            {
                if (!oldState.IsKeyDown(key))
                {
                    // Key was just pressed
                    retType = true;
                }
                else
                {
                    // Key was already pressed, return false
                    retType = false;
                }
            }
        }

        // Save keyboard state
        oldState = newState;

        // Return result
        if (retType == true)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

    // Method: Generate a new map
    public List<Block> newMap(Map map, List<Block> blockList, Console console)
    {
        // Create new map block coordinates
        List<Vector2> positions = new List<Vector2>();
        positions = map.generateMap(console);

        // Clear list and reallocate memory previously used up by it
        blockList.Clear();
        blockList.TrimExcess();

        // Add new blocks to the list using positions created by generateMap()
        foreach (Vector2 pos in positions)
        {
            blockList.Add(new Block() { Position = pos, Texture = dirtTex });
        }

        // Return modified list
        return blockList;
    }

    // ...
}

and the generateMap code:

// Generate a list of Vector2 positions for blocks
    public List<Vector2> generateMap(Console console, int method = 0)
    {
        ScreenTileWidth = gDevice.Viewport.Width / 16;
        ScreenTileHeight = gDevice.Viewport.Height / 16;
        maxHeight = gDevice.Viewport.Height;

        List<Vector2> blockLocations = new List<Vector2>();
        if (useScreenSize == true)
        {
            Width = ScreenTileWidth;
            Height = ScreenTileHeight;
        }
        else
        {
            maxHeight = Height;
        }

        int startHeight = -500; // For debugging purposes, the startHeight is set to an
        // hopefully-unreachable value - if it returns this, something is wrong

        // Methods of land generation
        /// <summary>
        /// Third version land generation
        /// Generates a base land height as the second version does
        /// but also generates a 'max change' value which determines how much
        /// the land can raise or lower by which it now does by a random amount
        /// during generation
        /// </summary>
        if (method == 0)
        {
            // Get the land height
            startHeight = rnd.Next(1, maxHeight);
            int maxChange = rnd.Next(1, 5); // Amount ground will raise/lower by
            int curHeight = startHeight;

            for (int w = 0; w < Width; w++)
            {
                // Run a chance to lower/raise ground level
                int changeBy = rnd.Next(1, maxChange);
                int doChange = rnd.Next(0, 3);
                if (doChange == 1 && !(curHeight <= (1 + maxChange)))
                {
                    curHeight = curHeight - changeBy;
                }
                else if (doChange == 2 && !(curHeight >= (29 - maxChange)))
                {
                    curHeight = curHeight + changeBy;
                }

                for (int h = curHeight; h < Height; h++)
                {
                    // Location variables
                    float x = w * 16;
                    float y = h * 16;

                    blockLocations.Add(new Vector2(x, y));
                }
            }
            console.newMsg("[INFO] Cur, height change maximum: " + maxChange.ToString());
        }
        /// <summary>
        /// Second version land generator
        /// Generates a solid mass of land starting at a random height
        /// derived from either screen height or provided height value
        /// </summary>
        else if (method == 1)
        {
            // Get the land height
            startHeight = rnd.Next(0, 30);

            for (int w = 0; w < Width; w++)
            {
                for (int h = startHeight; h < ScreenTileHeight; h++)
                {
                    // Location variables
                    float x = w * 16;
                    float y = h * 16;

                    // Add a tile at set location
                    blockLocations.Add(new Vector2(x, y));
                }
            }
        }
        /// <summary>
        /// First version land generator
        /// Generates land completely randomly either across screen or
        /// in a box set by Width and Height values
        /// </summary>
        else
        {
            // For each tile in the map...
            for (int w = 0; w < Width; w++)
            {
                for (int h = 0; h < Height; h++)
                {
                    // Location variables
                    float x = w * 16;
                    float y = h * 16;

                    // ...decide whether or not to place a tile...
                    if (rnd.Next(0, 2) == 1)
                    {
                        // ...and if so, add a tile at that location.
                        blockLocations.Add(new Vector2(x, y));
                    }
                }
            }
        }

        console.newMsg("[INFO] Cur, base height: " + startHeight.ToString());
        return blockLocations;
    }

I never touched any of the above code for this when it broke - changing keys won't seem to fix it. Despite this, I have camera movement set inside another Game1 method that uses WASD and works perfectly. All I did was add a few lines of code here:

private int BackBufferWidth = 1280; // Added these variables
private int BackBufferHeight = 800;

public Game1()
{
    graphics = new GraphicsDeviceManager(this);
    graphics.PreferredBackBufferWidth = BackBufferWidth; // and this
    graphics.PreferredBackBufferHeight = BackBufferHeight; // this
    Content.RootDirectory = "Content";

    this.graphics.IsFullScreen = true; // and this
}

When I try adding a console line to be printed in the event the key is pressed, it seems that the If is never even triggered despite the correct key being pressed.

© Game Development or respective owner

Related posts about XNA

Related posts about keyboard