Monogame虚拟游戏手柄Android

时间:2014-01-31 02:56:53

标签: android monogame touchpad

我无法让Android MonoGame项目正确使用虚拟游戏手柄。我根据this example for iOS

设置了我的虚拟游戏手柄

游戏手柄正确绘制但拒绝响应触摸输入。我从2013年发现了类似的问题,没有明确的解决方案,但我真的希望得到这个。任何帮助将不胜感激。

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);
    }
}

}

0 个答案:

没有答案