Game Development Reference
In-Depth Information
y
ahoaeb$^qhhapPula99omq]na%
w
ckpk=j`Opkl$OMQ=NA%7
y
ahoa
w
ckpk=j`Opkl$?EN?HA%7
y
Start with the
switch keyword.
The property you want to check.
The switch statement will produce a
different result, depending on its value.
A pair of curly
braces contains
the cases in the
switch statement.
osep_d$r]hqaPk?da_g%
w
_]oar]hqaKja6
pn]_a$r]hqaKjabkqj`%7
^na]g7
A possible value of the
property in the argument.
It can be a string, number,
or variable. It's followed
by a colon.
The case keyword
preceeds the name
of the variable you
want to check for.
_]oar]hqaPsk6
pn]_a$r]hqaPskbkqj`%7
pn]_a$nqj]ju`ena_perao%7
^na]g7
_]oar]hqaPdnaa6
pn]_a$r]hqaPdnaabkqj`%7
^na]g7
Any directives you want
to run if the value matches
the property in the argument.
You can add as many
directives as you like, each
on a different line.
If the case matches
the value in the
switch statement's
argument, break
prevents the switch
statement from
running further.
`ab]qhp6
pn]_a$Jki]p_dao%7
y
If there are no matches,
an optional default
directive can be run.
Figure 10-17. The switch statement
If there is no functional benefit to using a osep_d statement, why bother using one? It's purely a stylis-
tic difference—osep_d statements are a little easier to read. They clearly stand out in your code, and
you don't need to navigate through a tangle of disorienting curly braces to clearly see which condi-
tions result in which outcomes. If you have more than two conditions that you're checking for, try to
implement a osep_d statement.
Object factories
The kind of system you've just implemented is a very common one in computer programming. It's
a very basic example of what's known as a b]_pknu. The >qhhap class represents one type of object:
a bullet. But it can actually manufacture many different types of bullets, depending on the value that's
supplied to it as a parameter. It's a bullet factory!
 
Search Nedrilad ::




Custom Search