I am porting a Windows game to Android. It only took a couple of hours to get an Android version up and running, which is amazing considering I have no prior Android experience and didn’t design the game up front with Android port in mind. Thanks to everyone that put time into MonoGame.
I have one major problem with the port dealing keyboard input. When I hold down a key to move a sprite, it moves smoothly for about 1/2 of a second and then it starts to stutter.
I spent some time debugging, and it appears calls to KeyboardState.IsKeyDown starts cycling between true and false after about a half second. This makes the sprite move jerky.
At first I thought maybe there was a performance problem, but when I control the mouse by checking the held state of a mouse button everything works fine.
I wasn’t sure if the keyboardstate.IsKeyDown starting to cycle after a while is a bug or intended behaviour.
To reproduce, create a clean MonoGame project and add the following code. You will have to find your own spaceship texture though… I used Monogame 3.7.1.
sealed class Game1 : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
KeyboardState currentKeyboard = new KeyboardState();
Vector2 spritePosition;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
graphics.IsFullScreen = true;
graphics.PreferredBackBufferWidth = 800;
graphics.PreferredBackBufferHeight = 480;
graphics.SupportedOrientations = DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight;
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
base.LoadContent();
}
protected override void Update(GameTime gameTime)
{
currentKeyboard = Keyboard.GetState();
if (currentKeyboard.IsKeyDown(Keys.Left))
{
spritePosition.X -= 1;
}
else if (currentKeyboard.IsKeyDown(Keys.Right))
{
spritePosition.X += 1;
}
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
spriteBatch.Draw(Content.Load<Texture2D>("Spaceship"), spritePosition, Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
}
If this looks like a bug, I’ll write up an issue on GitHub. I suspect it is intended behaviour though and was hoping there was a way to turn off the cycling. Or maybe someone has a workaround in one of there games. I’m targeting AndroidTV, so maybe it is rare that someone would attach an actual keyboard, but probably best to support it since I already have keyboard logic in the game.
Thanks,
Brett