Game Development Reference
In-Depth Information
Constants
The first thing you'll probably notice about this class is the use of words that are fully capitalized, such
as >KQJ?A and CN=REPU. They are called constants , which are used exactly like variables, except that
their values never change. Their values are constant .
Constants are declared and initialized in the class definition using the keyword _kjop. Here are the
constants used in the class:
lner]pa_kjopBNE?PEKJ6Jqi^an9,*2,7
lner]pa_kjopOLAA@[HEIEP6ejp937
lner]pa_kjopCN=REPU6Jqi^an9,*47
lner]pa_kjop=??AHAN=PEKJ6Jqi^an9-*17
lner]pa_kjop>KQJ?A6Jqi^an9),*/7
lner]pa_kjopFQIL[BKN?A6Jqi^an9)-27
lner]pa_kjop>KPPKI[KB[OP=CA6qejp9-,7
After the values of constants are assigned, they can never be changed. This is helpful because it means
that your program will be free of errors that result from values being accidentally overwritten by other
values when your program depends on them to remain unchanged.
Constants also make it easy to quickly change the functioning of the program. By changing the values
of the constants at the top of the class, you can completely change the physical forces that affect
the player character, from the strength of gravity to the height of each jump. It makes the class very
customizable by just changing a few simple values.
By convention, constants are always written in full uppercase characters, which make them easy to
spot in your code. If the name of a constant is made up of two or more words, the convention is
to separate the words with an underscore character, like this: TWO_WORDS. Like variables, constants
are also referred to as properties .
One small change to this code is the use of the [bne_pekjT variable, which is used as the value of fric-
tion on the x axis. Because in this game the lh]uan object moves only left and right along the ground,
you don't need to account for friction on the y axis. This is just a slightly more specific way of dealing
with friction than in the previous examples, but the effect is exactly the same.
Player friction
The other slight modification is that friction on the x axis is completely removed when the keys are
pressed. You can see this at the beginning of the kjGau@ksj event handler:
lner]pabqj_pekjkjGau@ksj$arajp6Gau^k]n`Arajp%6rke`
w
++Naikrabne_pekjebpdak^fa_peoikrejc
[bne_pekjT9-7
A value of - means the value of [rt is multiplied by 1, so the value doesn't change. Friction is returned
to its initial value (the value of the BNE?PEKJ constant, which is ,*2) when the keys are released.
 
Search Nedrilad ::




Custom Search