我试图让 Enter 键为我的游戏生成一个新的“地图”,但无论出于何种原因,在其中实现全屏后,输入检查将不再起作用。我尝试删除新代码,一次只按一个键,但它仍然不起作用。
这是检查代码及其使用的方法,以及 newMap 方法:
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?
retType = newState.IsKeyDown(key);
}
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
return retType;
}
// 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;
}
// ...
}
当它坏了时,我从来没有碰过上面的任何代码——改变键似乎无法修复它。尽管如此,我还是在另一个使用 WASD 且运行良好的 Game1 方法中设置了相机移动。我所做的只是在这里添加几行代码:
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
}
当我尝试添加要在按下键时显示的文本时,尽管按下了正确的键,但似乎从未触发 If 。
似乎当 CheckInput 方法尝试检查刚刚按下的“Enter”时,它通过了第一次检查if (newState.IsKeyDown(key))
(返回 true)但第二次if (!oldState.IsKeyDown(key))
检查失败。(并返回 true,但不应该)