Game Development Reference
In-Depth Information
The name getter and setter functions return the protected value
of _name, which would otherwise be inaccessible, and it also forces
any attempts to assign a value to the _name property to a fixed
length of eight characters. The lives getter is an example of a read-
only property
there is no accompanying setter function. Any
attempts to set the value will cause an error. This is very useful
when you need to use values inside the class but also want external
classes to be able to read the value.
**The standard convention for variable and method names is to
start lowercase and then use CamelCase for all subsequent words
in the name. There is some debate over how to delineate public
variables from protected, private, or internal. My preference is to
follow Adobe
the beginning of the name of any property that is not expressly
public. Doing so allows you to use getter and setter methods like
the previous example, where _name was the protected variable and
name was used for the pair of methods. This yields continuity in
your naming and makes your code easier for others (and yourself)
to follow.
s convention, which is to use an underscore (
Since writing the first edition of this topic, I
ve had the privilege of work-
ing directly with some game industry veterans and picked up some new
patterns and conventions that they commonly use. While I think there
still value in Adobe
re only work-
ing in Flash, I wanted to mention this alternate convention because it is
particularly helpful if you
re intending to try to leverage code across plat-
forms outside AS3. It
s also what I now use as my standard and believe
it is only fair to disclose that. Basically, it does not differentiate between
private and public properties but rather prefixes them all with
members of a class. There
s no real reason for public or private members
to have different conventions because the compiler will catch illegal
access of either kind
s not like you can really mess it up. Also, it has
the added benefit of grouping all member variables in a class alphabeti-
cally when using code hinting. For instance, a player
mSpeed rather than _speed, regardless of being public, protected, or pri-
vate. Method names and names of accessors are still used as normal, but
method parameters are all prefixed with an underscore so as to denote
them clearly inside the method as temporary and local. You could use
something other than a prefix
some people like
instead. Don
dollar sign
like in some other languages; an Adobe engineer has men-
tioned in his blog that this could cause problems in certain circumstances
because it conflicts with internal Flash Player naming. The last example in
Chapter 16 will use these more recent conventions, so you can see how
they compare to Adobe
s standard and decide if you prefer it. Ultimately,
the important thing to remember when working on any project is to pick
a method that makes sense and stick with it consistently.
Search Nedrilad ::

Custom Search