哪个程序将顺序代码转换为循环代码?

时间:2013-05-15 11:47:35

标签: c# javascript

顺序代码示例:

Console.WriteLine("Hello");
Sleep(20);
Console.WriteLine("End");  

循环代码示例(函数循环):

bool step2 = false;
bool step3 = false;
bool beginn = true;
int i = 0;

void looped() //each second
{
  if (beginn == true)
  {
    Console.WriteLine("Hello");
    beginn = false;
    step2 = true;
  }
  if (step2 == true)
  {
    if (i <= 20)
    {
      i++;
    }
    else
    {
      step2 = false;
      step3 = true;
    }
  }
  if (step3 == true)
  {
    Console.WriteLine("End");
    step3 = false;
  }
}

哪个程序将顺序代码转换为循环代码?我想用它来统一,所以需要c#/ mono或javascript输出。

一般来说,每种编码的正确术语是什么?

1 个答案:

答案 0 :(得分:1)

由于这是Unity,我认为你正在寻找他们的协程和yield WaitForSeconds()范例:http://docs.unity3d.com/Documentation/ScriptReference/index.Coroutines_26_Yield.html

void Begin()
{
    StartCoroutine("looped");
}

IEnumerator looped() //each second
{
    Console.WriteLine("Hello");
    yield return new WaitForSeconds(20);
    Console.WriteLine("End");
}

编辑:JavaScript版本:

looped();

function looped()
{
    print("Hello");
    yield WaitForSeconds(20);
    print("End");
}

编辑:如果要从Update方法调用此方法,则需要使用StartCoroutine方法启动looped

C#:

void Update()
{
    StartCoroutine("looped");
}

IEnumerator looped() //each second
{
    Console.WriteLine("Hello");
    yield return new WaitForSeconds(20);
    Console.WriteLine("End");
}

JavaScript的:

function Update()
{
    StartCoroutine("looped")
}

function looped()
{
    print("Hello");
    yield WaitForSeconds(20);
    print("End");
}