Game Development Reference
In-Depth Information
++Ql`]papdank^kp#oo_kna
[nk^kpO_kna''7
++Ql`]papdank^kp#oo_kna`eolh]ukjpdaop]ca
nk^kpO_kna@eolh]u*patp9Opnejc$[nk^kpO_kna%7
y
It should be pretty obvious what this code does. But there's something new here that you haven't seen
before:
[^qhhapo*olhe_a$e(-%7
olhe_a is used to remove an element from an array. It uses two arguments: the first is the index num-
ber of the element to remove, and the second is the number of elements it should remove from that
point onward.
In this case, you want to remove the current bullet, which is at index number e. If the loop is in its
third repetition, that would be element number .. (Remember, array elements are numbered starting
at zero.)
In the second argument, - is the number of elements to remove. In this case, you want to remove only
one element.
What olhe_a does in this program is to remove the bullet from the array so the program no longer
has to check it for a collision. Without it, the array will just get bigger and bigger, and the program
will start to generate errors because it would check for collisions with bullets that have been removed
from the stage with naikra?deh`.
You can also use olhe_a to insert elements into an array. For example, if you want to
add an object called ^kt to an array as the third element, you can use a line of code
that looks like this:
]nn]u*olhe_a$.(,(^kt%7
This adds the ^kt object at index position . . , means that you didn't delete the ele-
ment that was originally at position . . That object will be pushed up to position / , and
any objects that come after it would also be renumbered.
If you want to insert an object into an array and replace the existing element, you can
use this format:
]nn]u*olhe_a$.(-(^kt%7
The ^kt object will now be inserted at position . , but - indicates that one element
should be deleted from the array. That would be the original element at position num-
ber . . It's now gone for good, replaced by ^kt , and the rest of the elements in the array
won't be renumbered.
olhe_a is a very versatile method to use with arrays, and you'll certainly find many occasions when it
will help manage game objects.
 
Search Nedrilad ::




Custom Search