Game Development Reference
In-Depth Information
runtime like in the Flash Player. As I mentioned earlier, it is
compiled ahead-of-time (AOT) into iOS machine code along with
the standard Flash libraries. In this entirely different process, cer-
tain elements of ActionScript are slower and performance of code
becomes essential to maintaining responsiveness and a decent
frame rate in your games. We
ll now look at the best practices to
follow when coding games for the iPhone.
'
Declare Your Types
This first and most basic practice should be familiar to anyone who
has read any programming topics (or Chapter 4 of this topic), but
the importance of it is magnified in Flash on the iPhone. When you
declare a variable to be of a specific type, you are allocating a speci-
fic amount of memory for that object, as well as keeping the runtime
code from having to look up the variable in a table in memory.
var myString:String =
; //CORRECT
var myUnknown = 5; //INCORRECT, AND YOU SHOULD BE ASHAMED
"
something
"
always be sure to specify
the types of parameters a method accepts, as well as a return type.
Avoid this use of dynamic arguments except when critically necessary.
This also applies to method signatures
//GOOD PRACTICE
private function doSomething(arg1:int, arg2:String):void
//BAD PRACTICE
private function doSomething(...args)
While this seems elementary, it makes a huge difference to both
thecompilerandtheapplicationatruntime.Frankly,ifyouaren
t
already typing your variables in AS3, you really should be ashamed
anyway.
'
Use Static Properties and Functions
When a member variable or function of a class is static , only one
instance of it is ever created for the class. Many of the variables
and methods used by a class will only make sense within the con-
text of a specific instance of that class, but any values that are con-
stant across all instances should become static. Here are a couple
of examples:
private static const MAX_SPEED:int = 10;
private static const MAX_AMMO:int = 20;
private var _health:int = 100;
Assume the three lines above were part of an Enemy class for a
game. The first two, MAX_SPEED and MAX_AMMO, are not going to
change per Enemy instance; they are universal across all Enemies, so
Search Nedrilad ::




Custom Search