打击功能无法正常工作

时间:2017-02-08 22:32:35

标签: c# android if-statement

你好我的团结c#安卓游戏平台是在玩家面前随机生成的。此脚本中的if语句会检查最近的平台是否是我正在查找的平台(左,右,上,下),并且播放器未按正确方向滑动strike设为1.

每当strike等于1时,我希望我的生命价值下降一。当我玩游戏时strike = 1,生命立即降至0,但我希望它们从3开始当strike= 1时为2,如果为strike = 1,则为2,最后为1,为0。

以下是if语句

void Start()
{
    lives = 3;
    theScoreManager = FindObjectOfType<ScoreManager>();
}

// Update is called once per frame
void Update()
{
    if (strike == 1)
    {
        lives = 2;
    }

    if (strike ==1 && lives == 2)
    {
        lives = 1;
    }

    if (strike == 1 && lives == 2)
    {
        lives = 0;
    }

这是整个c#脚本

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 lives;
    public int livesToGive;

    public GameObject closestPlatform;

    private ScoreManager theScoreManager;

// 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(3);
    leftSwipe = false;
}

public IEnumerator rightwait()
{
   rightSwipe = true;
   yield return new WaitForSeconds(3);
   rightSwipe = false;
}

public IEnumerator upwait()
{
    upSwipe = true;
    yield return new WaitForSeconds(3);
    upSwipe = false;
}

public IEnumerator downwait()
{
    downSwipe = true;
    yield return new WaitForSeconds(3);
    downSwipe = false;
}

public IEnumerator pause()
{
    strike = 1;
    yield return new WaitForSeconds(0.3f);
    strike = 0;
}

void Start()
{
    lives = 3;
    theScoreManager = FindObjectOfType<ScoreManager>();
}

// Update is called once per frame
void Update()
{
    strike = 0;

    if (strike == 1)
    {
        lives = 2;
    }

    if (strike ==1 && lives == 2)
    {
        lives = 1;
    }
    if (strike == 1 && lives == 2)
    {
        lives = 0;
    }

    livesToGive = lives;
    theScoreManager.AddLives(livesToGive);

    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) 
    {
        StartCoroutine(pause());
    }
    if ((closestPlatform == right) && !rightSwipe)
    {
        StartCoroutine(pause());
    }
    if ((closestPlatform == up) && !upSwipe)
    {
        StartCoroutine(pause());
    }
    if ((closestPlatform == down) && !downSwipe)
    {
        StartCoroutine(pause());

    }

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

3 个答案:

答案 0 :(得分:1)

您在strike的开头将Update设置为0。

答案 1 :(得分:1)

您将strike==1设置为更新中的第一件事,这样检查if (strike==1){ lives-- //decrement lives by one strike=0 //reset strike } 的所有if语句将始终为false。你需要在测试后将反击设置回0,而不是之前。

另外那些if语句是坏的,第一个会将生命减少到2,这将使第二个生命点火并将生命设置为1,第三个永远不会,因为它也会检查2的生命。也许是这样的事情?

else if

我不是团结专家,但这应该适用于任何C#代码。如果你不想按照我的方式使用if语句,你需要使用int main (void) { FILE *outFile; //The output file (encrypted) /* A 256 bit key */ unsigned char *key = (unsigned char *)"01234567890123456789012345678901"; /* A 128 bit IV */ unsigned char *iv = (unsigned char *)"01234567890123456"; int fd; struct stat sb; void * memblock; fd = open("result0.jpg",O_RDONLY); outFile=fopen("result0enc.jpg","wb"); fstat(fd, &sb); printf("Size: %lu\n", sb.st_size); unsigned char decryptedtext[sb.st_size]; int decryptedtext_len, ciphertext_len; /* Initialise the library */ ERR_load_crypto_strings(); OpenSSL_add_all_algorithms(); OPENSSL_config(NULL); memblock = mmap(NULL, sb.st_size,PROT_READ, MAP_SHARED, fd, 0); if (memblock == MAP_FAILED) { close(fd); perror("Error mmapping the file"); exit(EXIT_FAILURE); } ciphertext_len = encrypt((unsigned char *)memblock, sb.st_size,key,iv,ciphertext); fwrite( ciphertext,1, sb.st_size,outFile); if (munmap(memblock, sb.st_size) == -1) { perror("Error un-mmapping the file"); /* Decide here whether to close(fd) and exit() or not. Depends... */ } close(fd); fclose(outFile); EVP_cleanup(); ERR_free_strings(); return 0; } 并在条件链的末尾重置strike,而不是之前。

答案 2 :(得分:0)

Strike永远不会是1,因为每次更新都会将其重置为零

void Update()  
{  
   strike = 0;  
   ...