You may be wondering, how do I group a set of data together?|
An example of a good array is:-
var names = new Array("Jim", "Bob", "John", "Jay");
This gives us an easy way to have a list of the names...
This code above is like this:-
var names = new Array();
names = "Jim";
names = "Bob";
names = "John";
names = "Jay";
So we have an array called names, that creates something like this:-
|Array Name||Element Index Number||Value|
In this array we have 4 elements.
The number of elements is also known as the array's length.
Our names array has a length of 4, we can see this by accessing the array.length property
names.length would be four now...
Arrays start at element 0, and go all the way to their length - 1.
i.e. names = "Jim" and names[names.length - 1] = "Jay";
function DrawText(x,y, txt)
DrawText(0,0, "There are " + names.length + " elements to the array...");
Whats the point then!? This just seems like too much effort for a list!
Heh, have you read the loops doc yet?!
The loops doc tries to teach you to use code that only changes slightly each time it is ran to your advantage...
Say, you need to draw all the character names to the screen...
I'd say, with the names in an array, this is just too easy...
for(var i = 0; i < names.length; ++i)
var font = GetSystemFont();
font.drawText(0, i * font.getHeight(), names[i]);
The y position of the font.drawText(x,y, txt) is changed by combining the current element index number (the variable i) with the font.getHeight() (the font's max height value)
This produces text on lines with just the right amount of space inbetween.
If you want to create a battlesystem of your own, arrays are what you are going to need to use.
Actually, arrays are useful for any kind of list, say the items, spells, and such...
Anyhow, here is my info on the methods for arrays...
*** Array ***
- returns number of elements in the array
Note + The length of an array is dynamic.
+ var drivers = new Array("opengl", "8bit", "grayscale") has a length of 3
- returns the array with the specified array concatenated onto it
Note + Simple example is... three = one.concat(two);
- returns a string of all the elements of an array concatenated together
Note + This inserts a "," between all elements
- returns the array reversed
- returns a specified part of the array,
from array_object[a] to array_object[a + b]
Note + array_object(0,0) will return undefined...
- returns a sorted array
Note + A sort function can be passed as a parameter...
+ the sort function must take two values, a and b
+ the array is then sorted according the sort functions return values
+ If the return value is less than 0, the element a should be appear before element b...
+ If the return value is 0, then the two elements being compared should not be switched.
+ If the return value is greater than 0, the element b should be appear before element a...
+ example: array_object.sort(SortByNumber);
return a - b;
- returns the array with an item pushed onto the end of the array
- returns the array with an item removed from the end of the array
- returns the array with the first element at the beginning of the array, removed
- returns the array with elements inserted at the beginnning of the array
splice(StartIndexNumber, DeleteCount, NewElements)
- allows insertion and deletion of array elements...
Thats its folks, soon, I will cover how to make an object in Sphere