Game Development Reference
In-Depth Information
Store Relevant Values as Variables and
Constants
If you work with string or numeric properties that represent a value
in your code (such as the speed of a player, the value of gravity in
a simulation, or the multiplier for a score bonus), store them as a
variable or a constant.
'
Well, duh,
you
re probably thinking right
now,
ve seen a lot
of codes over the years which were hurriedly thrown together, and
the same numeric values were repeated all over the place instead
of using a variable. Here
Who wouldn
'
tdothat?!?
Sadly, I have to say I
'
'
s an example:
player.x += 10 * Math.cos(angle);
player.y += 10 * Math.sin(angle);
In their haste, a developer was probably testing values to deter-
mine the proper speed at which to move the player Sprite and just
used the number directly in the equation. It would have been vir-
tually no extra time to simply assign the number to a variable,
speed, and then use the variable in the code instead.
var speed:Number = 10;
//
player.x += speed * Math.cos(angle);
player.y += speed * Math.sin(angle);
s finished
which requires a change in player speed, it will require altering
onlyasinglelineofcodeversushowevermanyplacesthatvalue
was used. Although this seems like a very simple exercise, a num-
ber of otherwise good developers have been guilty of this at one
time or another because they were rushing. While this example is
obvious, there are other instances of this phenomenon, which
might not occur to developers immediately. One example that
comes to mind is the names of event types. Many Flash developers
with a background in ActionScript 2 are used to name events using
raw strings:
Now if something changes in the game before it
'
addEventListener("init",initMethod);
In ActionScript 3, Adobe introduced constants: values that will
never change but are helpful to enumerate. One of the key uses of
constants is in naming event types.
public static const INIT:String = "init";
addEventListener(INIT, initMethod);
There are a number of reasons for following this syntax. The
first is that it follows the above example: if you are going to use
Search Nedrilad ::




Custom Search