AS3新手挣扎

时间:2017-02-23 07:38:50

标签: actionscript-3

var animal:String ="Cat";


var isFish:Boolean;

isFish = isItAFish(animal);
trace(isFish);

function isItAFish (animal:String):Boolean {

    var fishArray:Array = new Array("haddock", "Trout", "Salmon", "Cod");

    for(var i:int = 0; i < fishArray.length; i++){

        if (fishArray[i] == animal){

            return true;

            break;
        }
    }
    return false; 
}

我只需要帮助解释这些代码的男人和女孩。 &#34; isFish = isItAFish(动物);迹(isFish);是我困惑的地方。

1 个答案:

答案 0 :(得分:2)

//animal is a string that contains the value "Cat"
var animal:String ="Cat";

//isFish is a boolean that will be used as a flag
var isFish:Boolean;

//isFish value will be changed from the outcome of the function isItAFish with the animal value.
isFish = isItAFish(animal);
trace(isFish);

//This function requires 1 string parameter and returns a boolean.
function isItAFish (animal:String):Boolean
{
     //fishArray is a list of all your possible fishes.
     var fishArray:Array = new Array("haddock", "Trout", "Salmon", "Cod");

    /*
    We iterate the array to see if animal ("Cat") is inside the fishArray possible values.
    This loop will run exactly the number of times of the array's content. In this case, 4 times.
    */
    for(var i:int = 0; i < fishArray.length; i++)
    {
        /*
        We are comparing the values of the fishArray against animal ("Cat").
        fishArray[i] holds the value of the current loop count.
        For example, the first loop will be fishArray[0] which is "haddock".
        The 4th loop will contain the value "Cod".
        */
        if (fishArray[i] == animal)
        {
            //If we find a match, we return 'true' and stop the loop.
            return true;
            break;
        }   
    }

    //IF the loop ends without any matches we return 'false'.
    return false; 
}