Skill Sets:

Home

  1. Intro to Flash
  2. User Input
  3. Fancy Controls 1
  4. Control Flow: if
  5. Control Flow: loop
  6. Nested Structures
  7. Graphics 1
  8. Arrays
  9. Strings
  10. Mouse Movement
  11. Functions
  12. Grouping Images
  13. Graphical Support
  14. Animation
  15. Physics
  16. 2010 Grade 10 Summative showcase
  17. 2010 Grade 11 Summatives
  18. 2011 Grade 10 Summatives
  19. 2011 Grade 11/12 Summatives
  20. Sept 2011 Grade 11 Summatives
  21. Sept 2011 Grade 10 Summatives
  22. Feb 2012 Grade 10 Summatives
  23. Sept 2012 Summatives
  24. Feb 2013 Grade 12 Summatives
  25. Feb 2013 2OI Summatives
  26. Sept 2013 Summatives
  27. Feb 2014 Summatives

Arrays

An array is a collection of data storage locations, each of which holds the same type of data. Each storage location is called an element of the array.

Arrays allow you to store similar data (homogeneous) under one variable name. Not only does this make your code more clear, you will also notice that you will be writing less code and increasing your level of control over different variables.

You declare an array just like other basic variables. For example:

var arnNumbers:Array = new Array();
var arsLetters:Array = new Array("a", "b", "c"); 

In the first declaration, I have created an empty array. Notice that I start the declaration with "arn" signifying that it is an array of ints.

In the second declaration, I have populated it with three String variables, therefore I prefaced the variable name with ars (an array of Strings).

Here's some code that shows how I populate the arnNumbers array, and the show it its contents:

arnNumbers.push(5);
trace(arnNumbers);
arnNumbers.push(8);
trace(arnNumbers); 

The "push" function adds one more item to the Array. Arrays in AS work like Vectors in C++ or ArrayLists in Java. Old school languages are much more inflexible when working with arrays.

If you trace the Array, you get the full Array. Here is how you can loop through each item in the array:

var i:int;
for (i = 0; i < (arnNumbers.length); i++) {         
     trace(arnNumbers[i]); 
}   

The "i" is the Array index, that grabs one Array item at a time.

Here's an example of code that allows the user to input 5 numbers into one TextInputBox and then the numbers in the array will be traced.

public class ArrayIntro extends MovieClip {
 var arnNums:Array = new Array();
       var nNum:int;
       var txtNums:TextField = new TextField();
       var i:int;
       var nLength:int;
 public function ArrayIntro() {
       dSum = 0;
       txtNums.type=TextFieldType.INPUT;
       txtNums.x=100;
       txtNums.y=100;
       txtNums.border=true;
       txtNums.height=20;
       txtNums.text="";
       addChild(txtNums);
       txtNums.addEventListener(KeyboardEvent.KEY_UP, Nums);
 }
 public function Nums(event:KeyboardEvent) {
       if (event.charCode==13) {
          nNum=int(txtNums.text);// get a number from the text box
          arnNums.push(nNum); // add the number to the array
           //trace(arnNums); // display the whole array if you want
          if(arnNums.length == 4){
            for (i = 0; i < (arnNums.length); i++) {
              trace(arnNums[i]);
            }
            //txtNums.text = "That's all folks!!!";
            /*nLength = arnNums.length; // this is one way of deleting the array.
            for (i = 0; i <nLength; i++) {
              arnNums.pop();
            }*/
            while(arnNums.length>0) arnNums.pop(); // clear the array
            trace(arnNums);
            trace("Next round");
         } 
         txtNums.text = "";
  }
}

Let's randomize 52 ints in an array, emulating the shuffling of a deck of cards:

var orderedDeck:Array = new Array();
for (var nDeckSize:int = 0; nDeckSize<52; nDeckSize++){
orderedDeck.push(nDeckSize);
} // the deck is populated. Now for the shuffle
trace("ordered is" , orderedDeck); var shuffled:Array = new Array(); while(orderedDeck.length>0){ var:nCard:int Math.floor(Math.random()*orderedDeck.length); shuffled.push(orderedDeck[nCard]); orderedDeck.splice(nCard,1); } trace("Shuffled deck: " , shuffled);

This is a bit busy, so let me explain. The first loop populates an ordered deck of cards from 0 to 51. You stay in the second loop until the ordered deck is of size 0 - notice that within the loop you are cutting one card out each time though the loop by way of the splice() funciton.

In order to get a random card, we populate the nCard variable by way of getting a random number and multiplying it by the length of the orderedDeck. As the size of the orderedDeck drops by one each time throught the loop, the permissible number drops by one also. The first time through, you may get numbers 0 through 51. The second time through, you will only get numbers 0 through 50 etc.

We then add the randomized card to the shuffled array and then remove it from the orderedDeck.