intro to arrays computer game design fairport high school

Post on 18-Jan-2016

219 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Intro to ArraysCOMPUTER GAME DESIGN

FAIRPORT HIGH SCHOOL

Arrays A collection of data organized under a single variable name.

Let's imagine you are jotting down a list on a piece of paper. Let's call the piece of paper "groceries". Now, in the paper, you write a numbered list with all the items that belong there.

Start numbering from zero and enter an item that you need to purchase.

Array: VARIABLE - VALUES The piece of paper called "groceries" is your array variable. The items that you need to purchase are called the array values.

Banana-Rice Pie0 – bananas1 – milk 2 – rice3 – lemons

ActionScript3 var grocery:Array = new Array(); trace(grocery.length);

var grocery:Array = new Array("bananas", "milk", "rice", "lemons"); trace(grocery.length);

ActionScript3

var grocery = ["bananas", "milk", "rice", "lemons"];

trace(grocery);

Adding Items to the End of Your Array The Potatoes!!! We forgot the POTATOES … The array must grow. There are several ways to add items to your array:

One way would be to manually enter the index number of your array and assign a value as well. If you were to, say, add an item called "potatoes" to your current list of 4 items, simply add the following code:

?

Adding Items to the End of Your Array

grocery[4] = "potatoes";

trace(grocery);

Adding Items to the End of Your Array OH NO!!!! … THE BUTTER! We need to add butter to the end of the array. You could manually enter the index position and assign a value at that position.

Instead, we will try a more effective way. The new method of adding items to your array would be applied by using the length function to find out the length of the array. Once the length has been found, you can tell Flash to add an item to the end of the array without you having to manually enter the index position.

Adding Items to the End of Your Array

var i = grocery.length;grocery[i]="butter";trace(grocery);

In the above code, the variable "i" gets the length of the array grocery. In the second line, the value of the length of the array is set as the index position of your grocery array.

Arrays You should take care to note that the length function simply counts the number of items in your array, and it does not start at zero; it starts at 1. Therefore, the value of the length is always one more than the index position of the last item in your array.

Arrays

How can we use an Array in developing our games?

Replacing Items In Your Array

Replacing items in your array is fairly simple. Let's say you aren't happy with your second entry in your grocery list. In Flash terms, you want to replace the second item in your grocery array with something else. The code for that would be:

grocery[1]="tangerines";trace(grocery);

The second item, of course, has an index of 1, and the previous value for said position has now been overwritten with "tangerines."

Creating an Array by Combining Two Arrays

Flash allows you to create a new array by combining the values of two other arrays.

var colors = ["blue", "yellow", "green"];var shape = ["square", "triangle", "circle"];

Creating an Array using Slice()

Slice() creates a new array by extracting a specified range of values from another array.

For example, let's take the following array:

var countries = ["germany", "italy", "norway", "finland", "united states", "india"]

top related