使用' for'环。如何减去价值' i'使用if语句?

时间:2016-12-15 05:26:08

标签: javascript for-loop unity3d

我使用的脚本使用for循环在Unity中生成对象。当玩家对象击中其中一个衍生对象时,它会调用一个使布尔值为true的函数。当此布尔值为true时,它应从for循环中减去1。基本上for循环的最大值为10,当一个对象产生时它会加一个,我希望它在玩家击中一个对象时减去1。我在尝试使这项工作遇到问题。任何和所有的帮助,非常感谢。

var theObject:GameObject;
var maxPos:float = 500;
var minPos:float = -500;
var max = 10;
var switch1 = false;

function Start(){
    StartCoroutine(spawn());
}

function spawn() : IEnumerator {
    for (var i = 0; i < max;){
        if (switch1 == true){
            i--;
        }
        else if (switch1 == false){
            i++;

            //Debug.Log("spawn");
            //Time between spawns
            yield WaitForSeconds(0.4);

            //Determines and spawns new object
            var theNewPos = new Vector3(Random.Range(minPos,maxPos),0,Random.Range(minPos,maxPos));
            var go : GameObject = Instantiate(theObject);
            go.transform.position = theNewPos;
        }
        else{
            switch1 = false;
        }
    }
}

function AtomHit(){
    switch1 = true;
}

1 个答案:

答案 0 :(得分:1)

我想你是想这样做的??

var theObject:GameObject;
var maxPos:float = 500;
var minPos:float = -500;
var max = 10;
var switch1 = false;

function Start(){
    StartCoroutine(spawn());
}

function spawn() : IEnumerator {
    for (var i = 0; i < max;){
        if (switch1){
            i--;
        }
        else{
            i++;

            //Determines and spawns new object
            var theNewPos = new Vector3(Random.Range(minPos,maxPos),0,Random.Range(minPos,maxPos));
            var go : GameObject = Instantiate(theObject);
            go.transform.position = theNewPos;
        }

        //Time between spawns
        yield WaitForSeconds(0.4);
        switch1 = false;
    }
}

function AtomHit(){
    switch1 = true;
}