重新加载场景时,滑动输入失败

本文关键字:失败 输入 新加载 加载 | 更新日期: 2023-09-27 18:09:17

Application.LoadLevel("0"); 

当玩家失败并点击重试时重新加载场景。但是当关卡像这样重新加载后,滑动输入失败了,这是什么原因呢?

刷代码

void Update()
{
    foreach (Touch t in Input.touches)
    {
        switch (t.phase)
        {
            case TouchPhase.Began:
                Initial = t.position;
                Swiped = false;
                break;
            case TouchPhase.Ended:
                Direction = t.position - Initial;
                if (Direction.magnitude > 100.0f)
                {
                    Direction = Direction.normalized;
                    Swiped = true;
                }
                break;
        }
        if (Swiped && count == 0)
        {
            if (Vector2.Dot(Direction, Vector2.up) > Mathf.Sqrt(0.5f))
            {
                // Up
                BroadcastMessage("swipedUp", SendMessageOptions.DontRequireReceiver);
            }
            else if (Vector2.Dot(Direction, -1 * Vector2.up) > Mathf.Sqrt(0.5f))
            {
                // Down
                BroadcastMessage("swipedDown", SendMessageOptions.DontRequireReceiver);
            }
            else if (Vector2.Dot(Direction, Vector2.right) > Mathf.Sqrt(0.5f))
            {
                // Right
                BroadcastMessage("swipedRight", SendMessageOptions.DontRequireReceiver);
            }
            else if (Vector2.Dot(Direction, -1 * Vector2.right) > Mathf.Sqrt(0.5f))
            {
                // Left
                BroadcastMessage("swipedLeft", SendMessageOptions.DontRequireReceiver);
            }
        }
    }
}

当我第一次开始玩游戏时,上面的代码工作得很好,但当我试图重新加载相同的场景时,它失败了。游戏只有一个场景。My Start Method

void Start()
{
    Swiped = false;
    paused = false;
    GameOver = false;
    count = 0;
}

重新加载场景时,滑动输入失败

检查是否在代码中使用了静态变量。当你调用Application.LoadLevel("0")时,这些不会被重置;因为它们不是对象的一部分