Game Development Reference
In-Depth Information
Updating objects
The
update()
function is used to set all of the movement values for the game objects and to
create
Flak
explosions from shots that have finished their movement cycle. The first part of the
function tackles the
Shot
objects and their respective
Flak
explosions. The second part moves
all the other objects.
private function update():void {
First, we start iterating through the
shotArray
using a
for:each
statement. This is useful here
because
for:each
allows us to use a typed object that we would have to create manually using a
for:next
loop. Recall that
tempShot
was created as a class variable of type
Shot
, not local to this
function. We did this to save the overhead of creating temporary variables every time we loop
through our arrays of objects. We do the same for all the objects we will be updating in this function.
for each (tempShot in shotArray){
Recall that when any of our moving objects gets to the end of the vector (or finishes animating),
they set their
finished
property to
true
. That makes it very easy for us to check to see if they
should be removed from the screen. In the case of
Shot
, not only do we remove it from the screen
but we also create an instance of
Flak
that needs to appear exactly where the
Shot
finished
moving. Since
Flak
is 32
32 pixels, and it was created with its registration point at (0,0), we
need to subtract half the width and height from x and y to make the center of the
Flak
explosion
appear exactly where the
Shot
finished moving.
if (tempShot.finished) {
tempFlak = new Flak();
tempFlak.x = tempShot.x-(tempShot.width/2);
tempFlak.y = tempShot.y-(tempShot.height/2);
Now, we will add the Flak explosion to the display list and push it into
flakArray
(see Figure 5-8).
We then play the sound of flak exploding by dispatching
CustomEventSound.PLAY_SOUND
with
Main.SOUND_EXPLODE_FLAK
as the sound parameter. Notice that we have set the volume of the
sound to
.5
. This is because the explosion sound, as recorded, is a bit louder than the other
sounds. We could have fixed this with an audio program, but we adjusted the volume here to
illustrate that you can make adjustments to some sound properties in code.
Next, we call a new function named
removeItemFromArray()
passing the item to remove and
the array to remove it from.
We will discuss this very useful function a bit later. For now, we
can just say that the function will do everything necessary to make this instance of
Shot
disappear for good.
this.addChild(tempFlak);
flakArray.push(tempFlak);
dispatchEvent( new CustomEventSound(CustomEventSound.PLAY_SOUND,
Main.SOUND_EXPLODE_FLAK,false,1,0,.5));
removeItemFromArray(tempShot,shotArray);
If the
Shot
was not finished (
finished==false
), we simply call the
update()
function for the object.
This will set up the movement values that will be applied when we call
render()
(the next function
in the game loop). If you recall from the last chapter,
update() w
ill set the
nextLocation
point for
the objects that move in our game. We will actually move the objects in the
render()
function
discussed in the next section.