【发布时间】: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,您可以发布它们以便我们更好地帮助您)