XNA - 如何判断一个拇指手柄在某个方向上“抽搐”(XNA - how to tell if a

2019-10-29 06:42发布

有没有在API中的任何(3或4)告诉我,如果粘在一个方向移动,在一个菜单,这相当于打在DPAD一个方向? 似乎有一些Thumbstick*在成员Buttons枚举,但我不能在他们找到体面的文件。

只是想确保我不缺少明显的东西之前,我去和推出自己的。 谢谢!

Answer 1:

没有XNA的方法来告诉你,如果拇指操纵杆是“抽搐”这个框架。

最简单的方法是将存储旧摇杆状态。 如果状态是零,现在是非零,它已被抽动。

加成:

取而代之的检查,如果状态是零,现在是非零。 您可以使用摇杆按钮,从你在你的问题提的枚举,以确定是否棒已经“抽搐”。 在这种情况下,你是治疗的棍子像一个DPAD,并具备独立测试每个方向。 下面的代码显示此方法:

private void ProcessUserInput()
{
    GamePadState gamePadState = GamePad.GetState(PlayerIndex.One);

    if (m_lastGamePadState.IsButtonUp(Buttons.LeftThumbstickUp) && gamePadState.IsButtonDown(Buttons.LeftThumbstickUp))
    {
        PrevMenuItem();
    }

    if (m_lastGamePadState.IsButtonUp(Buttons.LeftThumbstickDown) && gamePadState.IsButtonDown(Buttons.LeftThumbstickDown))
    {
        NextMenuItem();
    }

    m_lastGamePadState = gamePadState;
}


Answer 2:

的Xbox 360控制器上的摇杆可以“在”推像按钮,它们映射到GamePadButtons.LeftStickGamePadButtons.RightStick 。 这些显然不是你想要的。

这里是,我使用的在任何方向上(其中,检测“印刷机”的代码padLeftPushActive存储帧之间):

Vector2 padLeftVector = gamePadState.ThumbSticks.Left;
bool lastPadLeftPushActive = padLeftPushActive;
if(padLeftVector.Length() > 0.85f)
    padLeftPushActive = true;
else if(padLeftVector.Length() < 0.75f)
    padLeftPushActive = false;

if(!lastPadLeftPushActive && padLeftPushActive)
{
    DoSomething(Vector2.Normalize(padLeftVector));
}

所以它检测只是压在必要的特定方向为你的菜单应该是相当简单的修改本。



Answer 3:

是GamePadState.Thumbsticks财产,你在找什么?



Answer 4:

这是我想出了一个解决方案,如果它是有用的人:

    enum Stick {
        Left,
        Right,
    }
    GamePadState oldState;
    GamePadState newState;

    /// <summary>
    /// Checks if a thumbstick was quickly tapped in a certain direction. 
    /// This is useful for navigating menus and other situations where
    /// we treat a thumbstick as a D-Pad.
    /// </summary>
    /// <param name="which">Which stick to check: left or right</param>
    /// <param name="direction">A vector in the direction to check. 
    /// The length, which should be between 0.0 and 1.0, determines 
    /// the threshold.</param>
    /// <returns>True if a twitch was detected</returns>
    public bool WasStickTwitched(Stick which, Vector2 direction)
    {
        if (direction.X == 0 && direction.Y == 0)
            return false;

        Vector2 sold, snew;
        if (which == Stick.Left)
        {
            sold = oldState.ThumbSticks.Left;
            snew = newState.ThumbSticks.Left;
        }
        else
        {
            sold = oldState.ThumbSticks.Right;
            snew = newState.ThumbSticks.Right;
        }

        Vector2 twitch = snew;
        bool x = (direction.X == 0 || twitch.X / direction.X > 1);
        bool y = (direction.Y == 0 || twitch.Y / direction.Y > 1);
        bool tnew = x && y;

        twitch = sold;
        x      = (direction.X == 0 || twitch.X / direction.X > 1);
        y      = (direction.Y == 0 || twitch.Y / direction.Y > 1);
        bool told = x && y;

        return tnew && !told;
    }


文章来源: XNA - how to tell if a thumb stick was “twitched” in a certain direction
标签: input xna