Search code examples
actionscript-3

Understanding Actionscript 3 Code


I was given this piece of code and am trying to understand it as best as I can. I have ran the code and understand what it does, but there are a few lines which I do not know how to explain.

Code here

var theElements = newArray["Earth", "Wind", "Fire", "Water", "Ether"];
var elements:String;
var numberOfRoles = 10;

roleThem(theElements, numberOfRoles);

function roleThem(theElements, numberOfRoles):void{

    while (numberOfRoles > 0){
        var currentElement = role(theElements);
        trace(currentElement);
        numberOfRoles--;
    }
}

function role(theArray:Array):String{

    var picker = Math.floor(Math.random()*theArray.length);
    return theArray[picker];
}

Here is what I have come up with so far:

In line 1 of the code shown, we have created a new Array called “theElements”. It contains 5 different elements named “Earth, Wind, Fire, Water & Ether.”

In line 2 of the code, we are creating a variable with a String function, however this line of code seems to be an error that was left in to the code as “element” does not feature within our code again. This line of code was used in a previous example and had been forgotten to be removed. We can successfully run the piece of code without any errors if we remove this line.

In line 3 of the code, we create a variable called “numberOfRoles” and give this a 10 value.

In line 5 of the code, we pull the Array “theElements” and variable “numberOfRoles” and use them as attributes, while naming them both as “roleThem”

In line 7 of the code, we create a function and use roleThem

In line 9 we use the while loop, which will repeat as long as the statement “numberOfRoles > 0” is true.

In line 12 we check that our variable “numberOfRoles” is always equal.

I am stuck on explaining Line 10/11 and 16-19. I know that lines 16-19 pick 10 of the elements at random and display then which you can see, however I do not know how to explain it in the correct manner. I am still learning the basics of Actionscript so any help would be much appreciated.


Solution

  • numberOfRolls--;
    

    This subtracts 1 from the variable. It starts as 10, and runs the while loop as long as numberOfRolls > 0. As soon as it is zero, the while loop is closed.

    var currentElement = roll(theElements);
    

    This declares a variable and gives it the value that is returned by the function. In this case the function returns an Array, as is noted by line 19.

    A function that looks like

    myFunction(value:int):void{
    

    accepts an integer argument and returns nothing (void)

    myFunction(value:int):String{
    

    returns a String value. But it requires a line like this

    return "this String";
    

    You can also have optional arguments. Looks like this:

    myFunction(value:Array = null):void{
    

    So the function will still run even if you don't pass an array to it.

    one more thing

    In line 5 of the code, we pull the Array “theElements” and variable “numberOfRoles” and use them as attributes, while naming them both as “roleThem”

    This is not the right way to say this. We aren't naming anything here at all. roleThem is a function and we are calling the function (telling the play head to execute the function) and passing the function two variables: an Array called theElements and an integer value called numberOfRoles.