Unity - 为我的按钮添加延迟

时间:2017-05-23 15:54:56

标签: c# unity3d unity5

我正在进行蛇形游戏,我遇到了一个问题,如果我的输入(向上,向下,向左,向右)过快,我的蛇就会陷入其中。我试图在Update()下创建一个计时器来调节我的动作,但我发现我的蛇仍然会不时地进入自身。

我使用InvokeRepeating()每隔0.05秒移动我的蛇,我有2种输入方法:1​​)更新以监听键盘输入(向上,向下,向左,向右箭头)和2)游戏手柄UI(向上,向下,向左,向右按钮)。有关如何调节方向变化的任何建议吗?

    void Start () {
        Screen.orientation = ScreenOrientation.Portrait;
        isPaused = false;
        InvokeRepeating("TimerInvoke", 0, deltaTimer);
    }

    void TimerInvoke() {
    Movement();
    StartCoroutine(checkVisible());
    if (currentSize == maxSize)
        TailFunction();
    else
        currentSize++;
    }

    void Update () {
         CompDirectionControl();
         PauseCMD();
    }

    void CompDirectionControl() {
    if (direction != Direction.South && Input.GetKeyDown(KeyCode.UpArrow))
        direction = Direction.North;
    if (direction != Direction.West && Input.GetKeyDown(KeyCode.RightArrow))
        direction = Direction.East;
    if (direction != Direction.North && Input.GetKeyDown(KeyCode.DownArrow))
        direction = Direction.South;
    if (direction != Direction.East && Input.GetKeyDown(KeyCode.LeftArrow))
        direction = Direction.West;
    }

    public void  MPadDirectionControl(int numDirection)
    {
    if (direction != Direction.South && numDirection == 0)
        direction = Direction.North;
    if (direction != Direction.West && numDirection == 1)
        direction = Direction.East;
    if (direction != Direction.North && numDirection == 2)
        direction = Direction.South;
    if (direction != Direction.East && numDirection == 3)
        direction = Direction.West;
    }

我尝试创建这样的东西,但延迟非常不一致

    if (Time.time >= timeStamp)
    {
        CompDirectionControl();
        timeStamp = Time.time + moveInterval;
    }

更新 通过使用协程,我能够为键盘输入添加延迟。但是,我还没想出如何对我的按钮UI输入做同样的事情。如果我创建一个公共IEnumerator GamePadDirControl(int方向),如何将我的按钮参数传递给我的IEnumator?即使我将GamePadDirControl设置为公开,我也没有在我的Inspector中看到它,所以我无法将我的按钮链接到它

IEnumerator DirControl() {
    if (direction != Direction.South && Input.GetKeyDown(KeyCode.UpArrow))
    {
        yield return new WaitForSeconds(deltaTimer);
        direction = Direction.North;
    }
    else if (direction != Direction.West && Input.GetKeyDown(KeyCode.RightArrow))
    {
        yield return new WaitForSeconds(deltaTimer);
        direction = Direction.East;
    }
    else if (direction != Direction.North && Input.GetKeyDown(KeyCode.DownArrow))
    {
        yield return new WaitForSeconds(deltaTimer);
        direction = Direction.South;
    }
    else if (direction != Direction.East && Input.GetKeyDown(KeyCode.LeftArrow))
    {
        yield return new WaitForSeconds(deltaTimer);
        direction = Direction.West;
    }
}

更新2 对于我的按钮UI - 由于我的按钮不能与IEnumator函数链接,我决定为每个方向创建单独的协同程序,并从我的public void MPadDirectionControl(int numDirection)函数调用它们。

按钮ui输入上的更新代码

public void MPadDirectionControl(int numDirection)
{
    if (direction != Direction.South && numDirection == 0)
    {
        StartCoroutine(IntervalN());
        //direction = Direction.North;
    }
    else if (direction != Direction.West && numDirection == 1)
    {
        StartCoroutine(IntervalE());
        //direction = Direction.East;
    }
    else if (direction != Direction.North && numDirection == 2)
    {
        StartCoroutine(IntervalS());
        //direction = Direction.South;
    }
    else if (direction != Direction.East && numDirection == 3)
    {
        StartCoroutine(IntervalW());
        //direction = Direction.West;
    }
}

IEnumerator IntervalN() {
    yield return new WaitForSeconds(deltaTimer);
    direction = Direction.North;
}

IEnumerator IntervalE()
{
    yield return new WaitForSeconds(deltaTimer);
    direction = Direction.East;
}

IEnumerator IntervalS()
{
    yield return new WaitForSeconds(deltaTimer);
    direction = Direction.South;
}

IEnumerator IntervalW()
{
    yield return new WaitForSeconds(deltaTimer);
    direction = Direction.West;
}

2 个答案:

答案 0 :(得分:0)

我不确定我完全理解你的问题......

你试图在延迟之前改变方向吗?

如果是这样,我会尝试:

StartCoroutine(ChangeDirectionTo(Direction.North, 0.05f));
IEnumarator ChangeDirectionTo(Direction direction, float delay) {
   yield return new WaitForSeconds(delay);
   this.direction = direction;
}

答案 1 :(得分:0)

我想我看到了问题。您需要区分蛇的当前方向和用户的命令方向。尝试这样的事情:

if (direction != Direction.South && Input.GetKeyDown(KeyCode.UpArrow))
    nextDirection = Direction.North;
if (direction != Direction.West && Input.GetKeyDown(KeyCode.RightArrow))
    nextDirection = Direction.East;
if (direction != Direction.North && Input.GetKeyDown(KeyCode.DownArrow))
    nextDirection = Direction.South;
if (direction != Direction.East && Input.GetKeyDown(KeyCode.LeftArrow))
    nextDirection = Direction.West;
}

然后在您实际创建多维数据集的代码中,在产生它之前,使用nextDirection告诉您玩家想要去的地方:

direction = nextDirection;

因此,玩家在生成多维数据集之前根本没有机会改变方向。

这应该可行,但它的缺点是你不会得到好的输入缓冲"一些蛇游戏的效果,你可以点击向右,向下,而蛇向北进行掉头。如果你想这样做,你必须实现某种智能缓冲系统,该系统可以检测玩家是否输入了一种非常特殊的"无效的"输入(北/南方向为北,nextDirection为东)并设置蛇以便将来方向改变。有点像缓冲"特殊动作"战斗游戏的输入。