2017-02-06 3 views
-7

В моей игре утверждения if проверяют, прошел ли игрок влево, а ближайшая платформа - это тот, который я ищу (слева направо вверх и вниз), затем удар будет = 1. Я хочу, чтобы int totalStrikes увеличьте только на 1 при забастовке = 1, но он продолжает увеличиваться до тех пор, пока забастовка = 1. Также по какой-то причине значение забастовки = 1 в течение нескольких секунд, когда игра начинается, и я не уверен, почему. Любая помощь будет оценена. Спасибо Хересint продолжает увеличиваться на 1

Вот если заявления им с помощью

if (strike == 1) 
    { 
     totalStrikes= totalStrikes + 1 ; 

    } 

if ((closestPlatform == left) && !leftSwipe) { strike += 1; 

    } 
    if ((closestPlatform == right) && !rightSwipe) 
    { 
     strike += 1; 

    } 
    if ((closestPlatform == up) && !upSwipe) 
    { 
     strike += 1; 


    } 
    if ((closestPlatform == down) && !downSwipe) 
    { 
     strike += 1; 


    } 

Вот весь скрипт

using UnityEngine; 
using System.Collections; 

    public class SwipeChecker : MonoBehaviour 

    { 

    public float maxTime; 
    public float minSwipeDistance; 

    float startTime; 
    float endTime; 

    Vector3 startPos; 
    Vector3 endPos; 

    float swipeDistance; 
    float swipeTime; 
    public float swipeScore; 

    public GameObject left; 
    public GameObject right; 
    public GameObject up; 
    public GameObject down; 
    public GameObject swipeChecker; 
    public GameObject[] platforms = new GameObject[5]; 

    public bool leftSwipe; 
    public bool rightSwipe; 
    public bool upSwipe; 
    public bool downSwipe; 


    public int strike; 
    public int totalStrikes; 



    public GameObject closestPlatform; 


    // Use this for initialization 

    public GameObject FindClosestPlatform() 
    { 

     GameObject[] gos; 

     gos = GameObject.FindGameObjectsWithTag("platform"); 


     GameObject closest = null; 
     float distance = Mathf.Infinity; 
     Vector3 position = transform.position; 
     foreach (GameObject go in gos) 

     { 
      Vector3 diff = go.transform.position - position; 
      float curDistance = diff.sqrMagnitude; 
      if (curDistance < distance) 
      { 
       closest = go; 
       distance = curDistance; 
      } 
     } 
     return closest; 
    } 

    public IEnumerator leftwait() 
    { 
     leftSwipe = true; 
     yield return new WaitForSeconds(2); 
     leftSwipe = false; 
    } 

    public IEnumerator rightwait() 
    { 
     rightSwipe = true; 
     yield return new WaitForSeconds(2); 
     rightSwipe = false; 
    } 
    public IEnumerator upwait() 
    { 
     upSwipe = true; 
     yield return new WaitForSeconds(2); 
     upSwipe = false; 
    } 
    public IEnumerator downwait() 
    { 
     downSwipe = true; 
     yield return new WaitForSeconds(2); 
     downSwipe = false; 
    } 
    public IEnumerator pause() 
    { 

     yield return new WaitForSeconds(2.0f); 

    } 


    void Start() 
    { 

    } 

    // Update is called once per frame 
    void Update() 
    { 
     if (strike == 1) 
     { 
      totalStrikes= totalStrikes + 1 ; 

     } 
     strike = 0; 

     closestPlatform = FindClosestPlatform(); 
     left = GameObject.Find("PurpleTile(Clone)"); 
     right = GameObject.Find("OrangeTile(Clone)"); 
     up = GameObject.Find("BlueTile(Clone)"); 
     down = GameObject.Find("RedTile(Clone)"); 


     if ((closestPlatform == left) && !leftSwipe) { strike += 1; 

     } 
     if ((closestPlatform == right) && !rightSwipe) 
     { 
      strike += 1; 

     } 
     if ((closestPlatform == up) && !upSwipe) 
     { 
      strike += 1; 


     } 
     if ((closestPlatform == down) && !downSwipe) 
     { 
      strike += 1; 


     } 


     if (Input.touchCount > 0) 
     { 
      Touch touch = Input.GetTouch(0); 

      if (touch.phase == TouchPhase.Began) 
      { 
       startTime = Time.time; 
       startPos = touch.position; 
      } 
      else if (touch.phase == TouchPhase.Ended) 
      { 
       endTime = Time.time; 
       endPos = touch.position; 

       swipeDistance = (endPos - startPos).magnitude; 
       swipeTime = endTime - startTime; 

       if (swipeTime < maxTime && swipeDistance > minSwipeDistance) 
       { 
        swipe(); 
       } 
      } 

     } 

    } 


    void swipe() 
    {  

     Vector2 distance = endPos - startPos; 
      if (Mathf.Abs(distance.x) > Mathf.Abs(distance.y)) 
      { 
       Debug.Log("Horizontal Swipe"); 
       if (distance.x > 0) 
       { 
        Debug.Log("Right Swipe"); 
       rightSwipe = true; 
       StartCoroutine(rightwait()); 
      } 
       if (distance.x < 0) 
       { 
        Debug.Log("Left Swipe"); 
        StartCoroutine(leftwait()); 

       } 

      } 

      else if (Mathf.Abs(distance.x) < Mathf.Abs(distance.y)) 
      { 
       Debug.Log("Vertical Swipe"); 
       if (distance.y > 0) 
       { 
        Debug.Log("Up Swipe"); 
        StartCoroutine(upwait()); 
       } 
       if (distance.y < 0) 
       { 
        Debug.Log("Down Swipe"); 
        StartCoroutine(downwait()); 
       } 
      } 


     } 

    } 
+0

Пожалуйста, подумайте о том, чтобы уменьшить количество кода в вашем образце до места, где у вас возникли проблемы. Вы не можете ожидать, что кто-то отладит весь ваш код для вас. – Anth12

+0

Тогда логика диктует вашу логику неправильной, если вы хотите, чтобы она увеличивалась, когда забастовка 1 .. вам нужно сказать, что – BugFinder

ответ

0

Самый простой способ определить, почему что-то происходит, - это подключить к нему отладчик. Посмотрите, когда что-то случится и проверьте ваш стек вызовов в этот момент.

Конечно, если вы собираетесь подключить отладчик к этому коду, возможно, вам нужно добавить некоторое время на тайм-ауты или контрольные точки до того, как логические значения будут установлены в false.

Смежные вопросы