一段时间后,团结一致的Android游戏突然停止接受触摸输入

时间:2019-05-19 21:38:15

标签: c# android unity3d

我有一个统一制作的游戏,是为Android打造的。在Unity的运行窗口上,代码可以正常工作。但是,在android设备上,触摸输入会注册一段时间,然后突然停止。可能是什么问题?

我尝试使用Android监视器进行调试,但未显示任何错误。我尝试使用旧版本的代码进行构建,但仍然相同。该错误可以在Playstore Google的构建中更清楚地看到,但我不想在这里破坏社区规则。如果可能的话,下午

多次移动代码是检查玩家是否已经在移动,以便允许玩家一次移动网格中的多个图块。

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

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;
    }
}


1 个答案:

答案 0 :(得分:0)

问题在于更新中存在多个递归调用。重新设计代码以减少递归,并将其中一些移至最新更新