首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Unity Swipe功能问题

Unity Swipe功能问题
EN

Stack Overflow用户
提问于 2014-03-04 18:29:34
回答 1查看 1.9K关注 0票数 2

我在为游戏创建滑动控件时遇到了一些麻烦。我正在尝试重新创建地铁冲浪者游戏的控件,其中在Touch.Moved阶段而不是Touch.End阶段检测到滑动。现在,它在大多数情况下都可以工作,但有时(通常)垂直滑动会被处理为水平滑动。我已经提交了下面的代码,提前感谢您的帮助。

代码语言:javascript
复制
       int swipeIndex = -1; //index of first detected swipe to prevent multiple swipes
       Vector2 startPos = Vector.zero; //starting position of touch
       //minSwipeDist is the distance in inches the player must swipe for a correct swipe
       for (int i = 0; i < Input.touchCount; i++) {
            Touch touch = Input.touches[i];
            switch (touch.phase) {
                case TouchPhase.Began:
                    if (swipeIndex == -1) {
                        swipeIndex = i;
                        startPos = touch.position;
                    }
                    break;
                case TouchPhase.Moved:
                    if(i != swipeIndex)
                        break;
                    Vector2 direction = touch.position - startPos;
                    //vertical swipe
                    if (Mathf.Abs(direction.x) < Mathf.Abs(direction.y)) {
                        //swipe up
                        if ((touch.position.y - startPos.y) > minSwipeDist) {
                            //Process Up swipe
                            swipeIndex = -1;
                        }
                        //swipe down
                        else if ((touch.position.y - startPos.y) < -minSwipeDist) {
                            //Process down swipe
                            swipeIndex = -1;
                        }
                    }
                    //horizontal swipe
                    else {
                        //swipe left
                        if ((touch.position.x - startPos.x) < -minSwipeDist) {
                            //process left swipe
                            swipeIndex = -1;
                        }
                        //swipe right
                        else if ((touch.position.x - startPos.x) > minSwipeDist) {
                            //process right swipe
                            swipeIndex = -1;
                        }
                    }
                    break;
            }
        }
EN

回答 1

Stack Overflow用户

发布于 2014-03-26 15:44:29

我认为你应该把你的代码从TouchPhase.Moved转移到TouchPhase.Ended。基本上,当使用TouchPhase.Began时,你会得到接触点,然后在TouchPhase.Moved中不断更新你的第二个点,然后在TouchPhase.Ended中处理这两个点。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22169330

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档