Game Development Reference
In-Depth Information
Firing bullets
The Sa]lkj and >qhhap classes work together to fire bullets. When the player presses the spacebar,
the Sa]lkj class's odkkp>qhhap method is called:
lner]pabqj_pekjodkkp>qhhap$%6rke`
w
eb$[sa]lkj=nia`%
w
l]najp*]``?deh`$jas>qhhap$%%7
y
y
If [sa]lkj=nia` is pnqa, the directive that creates the bullets is run:
l]najp*]``?deh`$jas>qhhap$%%7
This adds a new instance of the >qhhap class to the l]najp container of the Sa]lkj class: `qjcakjKja.
The ^qhhap object is created without an instance name .
This is the first time you've seen an object created without an instance name. The format for creating
objects that you're more familiar with is this one:
r]n^qhhap6>qhhap9jas>qhhap$%7
l]najp*]``?deh`$^qhhap%7
With this format, you can use ^qhhap to refer to the bullet object you just created. With the for-
mat used in the Sa]lkj class, the object is created but it isn't given a name. If these objects aren't
assigned instance names, what happens to them when they're added to `qjcakjKja? They actually
have instance names, but these names are assigned automatically by AS3.0. That's just fine because
you don't need to reference any of the bullet objects specifically in the code.
If you want to find out the instance names to which AS3.0 assigns the bullets, drop this
directive into the >qhhap class's kj=``a`PkOp]ca event handler:
pn]_a$>qhhapejop]j_aj]ia6'pdeo*j]ia%7
When you start firing bullets, you'll see something like this in the Output panel:
>qhhapejop]j_aj]ia6ejop]j_a--.
>qhhapejop]j_aj]ia6ejop]j_a--0
>qhhapejop]j_aj]ia6ejop]j_a--2
AS3.0 assigns these names automatically. Interesting!
Figure 8-23 illustrates how the Sa]lkj class fires bullets.
 
Search Nedrilad ::




Custom Search