3

I'm having trouble getting an Android MonoGame project to use the virtual gamepad correctly. I set up my virtual gamepad according to this example for iOS

The gamepad draws correctly but refuses to respond to touch input. I've found similar problems from 2013 with no definitive solutions but I would really like to get this working. Any help would be greatly appreciated.

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;

namespace VirtualGamePad
{
public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    Texture2D texture, character;
    Vector2 position = new Vector2();
    Color characterColor = Color.White;
    SpriteFont font;

    int screenWidth, screenHeight;

    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        graphics.IsFullScreen = true;
        graphics.SupportedOrientations = DisplayOrientation.LandscapeLeft |     DisplayOrientation.LandscapeRight;

        screenHeight = graphics.PreferredBackBufferHeight;
        screenWidth = graphics.PreferredBackBufferWidth;

        Content.RootDirectory = "Content";

        graphics.IsFullScreen = true;
        graphics.PreferredBackBufferWidth = 800;
        graphics.PreferredBackBufferHeight = 480;
        graphics.SupportedOrientations = DisplayOrientation.LandscapeLeft | DisplayOrientation.LandscapeRight;
    }

    protected override void Initialize()
    {
        base.Initialize();
    }

    protected override void LoadContent()
    {
        // Create a new SpriteBatch, which can be used to draw textures.
        spriteBatch = new SpriteBatch(GraphicsDevice);

        texture = Content.Load<Texture2D>("gamepad.png");
        character = Content.Load<Texture2D>("monogameicon.png");
        font = Content.Load<SpriteFont>("font");

        //Set the virtual gamepad
        ButtonDefinition BButton = new ButtonDefinition();
        BButton.Texture = texture;
        BButton.Position = new Vector2(screenWidth-92, screenHeight-72);
        BButton.Type = Buttons.B;
        BButton.TextureRect = new Rectangle(72, 77, 36, 36);

        ButtonDefinition AButton = new ButtonDefinition();
        AButton.Texture = texture;
        AButton.Position = new Vector2(screenWidth-112, screenHeight-46);
        AButton.Type = Buttons.A;
        AButton.TextureRect = new Rectangle(73, 114, 36, 36);

        GamePad.ButtonsDefinitions.Add(BButton);
        GamePad.ButtonsDefinitions.Add(AButton);

        ThumbStickDefinition thumbStick = new ThumbStickDefinition();
        thumbStick.Position = new Vector2(10,screenHeight-78);
        thumbStick.Texture = texture;
        thumbStick.TextureRect = new Rectangle(2,2,68,68);

        GamePad.LeftThumbStickDefinition = thumbStick;
    }

    protected override void Update(GameTime gameTime)
    {
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            Exit();
        if (GamePad.GetState(PlayerIndex.One).Buttons.A == ButtonState.Pressed)
            characterColor = Color.Green;
        if (GamePad.GetState(PlayerIndex.One).Buttons.B == ButtonState.Pressed)
            characterColor = Color.Red;

        //Change character position using thumbstick
        GamePadState state = GamePad.GetState(PlayerIndex.One);
        position.Y += (int)(state.ThumbSticks.Left.Y * -4);
        position.X += (int)(state.ThumbSticks.Left.X * 4);

        //Keep inside screen
        if (position.X + character.Width > Window.ClientBounds.Width)
            position.X = Window.ClientBounds.Width - character.Width;
        if (position.Y + character.Height > Window.ClientBounds.Height)
            position.Y = Window.ClientBounds.Height - character.Height;
        if (position.X < 0)
            position.X = 0;
        if (position.Y < 0)
            position.Y = 0;

        base.Update(gameTime);
    }

    protected override void Draw(GameTime gameTime)
    {
        graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

        spriteBatch.Begin();

        spriteBatch.Draw(character, position, characterColor);
        spriteBatch.DrawString(font, GamePad.GetState(PlayerIndex.One).ThumbSticks.Left.ToString(), Vector2.One, Color.Black);

        GamePad.Draw(gameTime, spriteBatch);

        spriteBatch.End();

        base.Draw(gameTime);
    }
}

}

Will Decker
  • 3,216
  • 20
  • 30

0 Answers0