2017-02-23 4 views
0
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; 
} 

Ich brauche nur Hilfe, diesen Code Jungs und Mädchen zu erklären. Die „isFish = isItAFish (Tier); trace (isFish).? Ist, wo ich bin verwirrt vonAS3 Anfänger kämpft

+0

es scheint mir gut‚isItAFish‘Teil gerade ist Aufruf der Methode namens‚rechts unten isItAFish‘, wo es heißt "Funktion ...." und der Trace-Teil ist nur auf das Flash-Ausgabe-Fenster den Wert von "isFish" – mitim

+0

Vielen Dank für das. Wie würden Sie den Code von "für" bis zum Ende zu erklären der Code? – hk111

+0

Anyone ????????? – hk111

Antwort

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; 
} 
+0

Vielen Dank – hk111

+0

@ hk111 wenn es half sicher sein, das Häkchen zu markieren oder bis die Antwort. –