首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用unity构建的Android游戏在一段时间后突然停止接受触摸输入

使用unity构建的Android游戏在一段时间后突然停止接受触摸输入
EN

Stack Overflow用户
提问于 2019-05-20 05:38:15
回答 1查看 242关注 0票数 0

我有一个用unity制作的游戏,是为Android构建的。在Unity上的run窗口中,代码运行正常。然而,在android设备上,触摸输入在突然停止之前注册了一段时间。可能的问题是什么?

我尝试使用Android监视器进行调试,但没有显示任何错误。我尝试过使用旧版本的代码构建,仍然是一样的。这个错误在playstore google上的构建中可以更清楚地看到,但我不想在这里违反社区规则。PM如果可能有帮助的话

多移动代码是为了检查玩家是否已经在移动,以便允许玩家一次在网格中移动多个瓦片。

代码语言:javascript
运行
复制
void setStartMove()
    {
        if (!startMove){        
            startMove = true;    
        }

    }




 public void Update()
    {

        myMap.getTileNeighbors(playerTile);
        if (neighbor == null)
        {
            neighbor = playerTile;
        }
        if (moving && (transform.position == endpos))
        {
            moving = false;
        }
        if (moving && startMove && (transform.position != endpos))
        {
            gameController.addMovesTaken(1);
            levelChanger.updateInGameMoves();
            startMove = false;
        }


        if (!moving && swipeControls.SwipeUp)
        {
            neighbor = myMap.getLastMoveableTile(playerTile, "up");
            moving = true;
            setStartMove();
        }


        if (!moving && swipeControls.SwipeDown)
        {
            neighbor = myMap.getLastMoveableTile(playerTile, "down");
            moving = true;
            setStartMove();
        }



        if (!moving && swipeControls.SwipeLeft)
        {
            neighbor = myMap.getLastMoveableTile(playerTile, "left");
            moving = true;
            setStartMove();
        }


        if (!moving && swipeControls.SwipeRight)
        {
            neighbor = myMap.getLastMoveableTile(playerTile, "right");
            moving = true;
            setStartMove();

        }

        Vector3 neighborPosCorr = new Vector3(neighbor.worldPosition.x, transform.position.y, neighbor.worldPosition.z);
        endpos = neighborPosCorr;

        transform.position = Vector3.MoveTowards(transform.position, endpos, Time.deltaTime * moveSpeed);

        playerTile = myMap.GetTileFromWorldPoint(transform.position);
    }

和SwipeControls

代码语言:javascript
运行
复制
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SwipeController : MonoBehaviour
{

    private PlayerManager playerManager;

    private bool tap, swipeLeft, swipeRight, swipeUp, swipeDown;
    private bool isDraging = false;
    private Vector2 startTouch, swipeDelta;

    private float deltaBorder = 15.0f;

    public Vector2 SwipeDelta { get { return swipeDelta; } }
    public bool Tap { get { return tap; } }
    public bool SwipeLeft { get { return swipeLeft; } }
    public bool SwipeRight { get { return swipeRight; } }
    public bool SwipeUp { get { return swipeUp; } }
    public bool SwipeDown { get { return swipeDown; } }

    public bool Dragging { get { return isDraging; } }
    private void Awake()
    {
        playerManager = GameObject.Find("GameManager").GetComponent<PlayerManager>();
    }
    private void Update()
    {
        tap = swipeLeft = swipeRight = swipeUp = swipeDown = false;

        #region Standalone Inputs
        if (Input.GetMouseButtonDown(0))
        {
            tap = true;
            isDraging = true;
            startTouch = Input.mousePosition;
        }
        else if (Input.GetMouseButtonUp(0))
        {
            isDraging = false;
            Reset();
        }
        #endregion

        #region Mobile Input
        if (Input.touches.Length > 0)
        {
            if (Input.touches[0].phase == TouchPhase.Began)
            {
                isDraging = true;
                tap = true;
                startTouch = Input.touches[0].position;
            }
            else if (Input.touches[0].phase == TouchPhase.Ended || Input.touches[0].phase == TouchPhase.Canceled)
            {
                isDraging = false;
                Reset();
            }
        }
        #endregion

        // Calculate the distance
        swipeDelta = Vector2.zero;
        if (isDraging)
        {
            if (Input.touches.Length > 0)
                swipeDelta = Input.touches[0].position - startTouch;
            else if (Input.GetMouseButton(0))
                swipeDelta = (Vector2)Input.mousePosition - startTouch;
        }

        //Did we cross the distance?
        if (swipeDelta.magnitude > deltaBorder)
        {
            //Which direction?
            float x = swipeDelta.x;
            float y = swipeDelta.y;
            if (Mathf.Abs(x) > Mathf.Abs(y))
            {
                //Left or right
                if (x < 0)
                    swipeLeft = true;
                else
                    swipeRight = true;
            }
            else
            {
                // Up or down
                if (y < 0)
                    swipeDown = true;
                else
                    swipeUp = true;
            }
            Reset();
        }

    }

    void Reset()
    {
        startTouch = swipeDelta = Vector2.zero;
        isDraging = false;
    }
}
EN

回答 1

Stack Overflow用户

发布于 2019-05-22 19:32:32

该问题与更新中的多个递归调用有关。重新设计代码以减少递归,并将部分代码移至后期更新

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

https://stackoverflow.com/questions/56212219

复制
相关文章

相似问题

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