Game Development Reference
In-Depth Information
In this example, bhu, ikomqepk, and ^aa are stored in the array as strings, not
objects.
Pushing elements into an array
Another very common way to get elements into an array is to use an array's built-in lqod method. You
can use lqod to literally “push” an element into an array by using this format:
]nn]uJ]ia*lqod$ahaiajpJ]ia%7
When you push an object into an array, it gets an index number that's one higher than the last ele-
ment added. This means that if the last element has an index number of W.Y, the object that you push
into it will have an index number of W/Y.
Using lqod is really helpful because you don't need to worry about which index number to add the
element to. The array figures this out for you.
The chapter source files include a folder called =nn]uLqod that contains an FLA called ]nn]uLqod.
Open it as a project and have a look at I]ej[=nn]uLqod*]o. It's identical to the first example, except
that it uses lqod to add the elements to the array:
[_khha_pekjF]n*lqod$bhu%7
[_khha_pekjF]n*lqod$ikomqepk%7
[_khha_pekjF]n*lqod$^aa%7
The pn]_a output is exactly the same as the first example. The fact that you don't need to worry about
the index numbers is very convenient.
To remove an element from an array, you can use the array's lkl method. The following
code uses lkl to remove the last element from an array and assign it to a variable:
naikra`Ahaiajp9[_khha_pekjF]n*lkl$%7
If the last element was ^aa, naikra`Ahaiajp has the value of ^aa. It also means
that [_khha_pekjF]n now contains only two elements: bhu and ikomqepk.
You can also add and remove elements to an array using the olhe_a method. I'll cover
it in detail in the next chapter.
 
Search Nedrilad ::




Custom Search