【问题标题】:Long time press and random time values长按和随机时间值
【发布时间】:2019-10-07 09:55:03
【问题描述】:

我想在触摸屏上长按几秒钟后触发一个事件。 我正在尝试使用以下代码来实现这一目标。 问题是过去的时间在某种程度上是随机的。

private float timePressed = 0.0f;
private float timeLastPress = 0.0f;
public  float timeDelayThreshold = 2.0f;

void Update() {
  checkForLongPress(timeDelayThreshold);
}

void checkForLongPress(float tim) {
  for (int i = 0; i < Input.touchCount; i++)
  {
    if (Input.GetTouch(0).phase == TouchPhase.Began)
    {
      // If the user puts her finger on screen...
      Debug.Log("Touch start");
      timePressed = Time.time - timeLastPress;
    }

    if (Input.GetTouch(0).phase == TouchPhase.Ended)
    {
      // If the user raises her finger from screen
      timeLastPress = Time.time;
      Debug.Log("Releasing Touch");
      Debug.Log("Time passed --> " + timePressed);
      if (timePressed > tim)
      {
        Debug.Log("Closing APP");
        // Is the time pressed greater than our time delay threshold?
        Application.Quit();
      }
    }
  }
}

事实是,“(timePressed > tim)”这个条件永远不会成立,我不明白为什么。

【问题讨论】:

  • 您可以使用测试日志编辑您的问题(您测试应用程序,并且由于您有 Debug.Log,您可以发布它们以便我们更好地帮助您)

标签: unity3d touch


【解决方案1】:

Time.time 返回The time at the beginning of this frame (Read Only). This is the time in seconds since the start of the game.

修正伪代码:

if (Input.GetTouch(i).phase == TouchPhase.Began)
{
  _timePressed = Time.time;
  return;
}
if (Input.GetTouch(i).phase == TouchPhase.Ended)
{
  var deltaTime = Time.time - _timePressed;
  if (deltaTime > _maxTimeTreshold)
  {
    Application.Quit();
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-30
    • 2023-03-11
    • 2016-04-06
    • 2012-05-19
    • 2011-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多