简体   繁体   English

需要一些帮助,在 C# 中运行控制台计时器,有什么想法吗?

[英]Need some help, running a console timer in C#, any ideas?

Since C# is being pretty cringe, I am asking for help on how to debug it based on the comments in the code.由于 C# 非常令人畏惧,因此我正在寻求有关如何根据代码中的注释对其进行调试的帮助。 As a summary, I have a timer running in console (noob here, code copy-pasted or tutorial'd) and the numbers ONE and GO are running simultaneously, and I have not found a way to stop the timer after the intended result is reached, and help would be appreciated!总而言之,我有一个在控制台中运行的计时器(这里是菜鸟,代码复制粘贴或教程)并且数字 ONE 和 GO 同时运行,我还没有找到在预期结果之后停止计时器的方法是已达到,我们将不胜感激!

using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
    public int startTime = 4;

    void Start()
    {
        InvokeRepeating("timer", 1.0f, 1.0f);
    }

    void timer()
    {
        if (startTime > 0)
        {
            if (startTime == 3)
            {
                Debug.Log("THREE");
                startTime -= 1;
            }
            else
            {
                if (startTime == 2)
                {
                    Debug.Log("TWO");
                    startTime -= 1;
                }
                else
                {
                    Debug.Log("ONE");
                    startTime -= 1;
                    //Precent "ONE" and "GO!" from running simultaneously
                }
            }
        }

        if (startTime == 0)
        {
            Debug.Log("GO!");
            //Fix "GO!" playing to console indefinetly after timer is finished
        }

        void Update()
        {

        }
    }
}

To do this, you can use the CancelInvoke method by passing the method name to quotes as a parameter you can call it when startTime goes to 0. You can safly remove the Update Method为此,您可以使用 CancelInvoke方法,方法是将方法名称作为参数传递给引号,您可以在 startTime 变为 0 时调用它。您可以安全地删除 Update 方法

public int startTime = 3;

void Start()
{
    InvokeRepeating("timer", 1.0f, 1.0f);
}

void timer()
{
    //go throw all the cases
    switch (startTime)
    {
        case 0:
            Debug.Log("GO!");
            CancelInvoke("timer");
            break;
        case 1:
            Debug.Log("ONE");
            break;
        case 2:
            Debug.Log("TWO");
            break;
        case 3:
            Debug.Log("THREE");
            break;
    }

    //substract - from time
    startTime -= 1;

}

Or use this Coroutine或者使用这个协程

void Start()
    {
        StartCoroutine("timer");
    }
    IEnumerator timer()
    {
        //debug the time
        Debug.Log(startTime); //you can use the if statement do show it as text or use any humanizer **C# 101 series**
        startTime--; //here we substract 1 from time
        //we wait for 1s
        yield return new WaitForSeconds(1); 
        //if the time still >= 0 repeat
        if(startTime >=0)
            StartCoroutine("timer");
    }

i think you want //if (startTime == 0) else { Debug.Log("GO!");我想你想要 //if (startTime == 0) else { Debug.Log("GO!"); //Fix "GO!" //修复“开始!” playing to console indefinetly after timer is finished }计时器结束后无限期地播放到控制台}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM