Game Development Reference
In-Depth Information
a value more than once anywhere in your code, it should be stored
in memory to change it easier. The second reason is that by declar-
ing event types and other constants in all capital letters, they stand
out in your code if someone else is looking at them. Perhaps the
most important reason, however, is compile-time checking. When
Flash compiles your SWF, it runs through all the codes to look for
misuse of syntax and other errors.
addEventListener("init", method1);
addEventListener("inti", method2);
If I had the previous two lines of code in different parts of the
same class, Flash would not throw an error when I compiled it.
public static const INIT:String = "init";
addEventLister(INIT, method1);
addEventLister(INTI, method2);
However, had I used a constant value from above and mis-
spelled the name of the constant, Flash would have warned me
about my mistake when I tried to compile it. This type of checking
is utterly invaluable at the eleventh hour when you
'
re trying to get
a project out the door and don
'
t have time to debug inexplicable
errors.
Don ' t Rely on Your Stage
When a developer is working on a game in a crunch, it is often in a
vacuum. He or she can take certain things for granted, such as the
size of the Stage of their SWF. However, if that SWF is loaded into
another container of different dimensions, the game
smechanic
can be adversely affected. For instance, the following lines of code
center an object horizontally and vertically on the stage, assuming
its container lines up with the upper left-hand corner of the stage
and its registration point is in its center.
'
player.x = stage.stageWidth/2;
player.y = stage.stageHeight/2;
If the SWF containing this code is loaded into a larger SWF, it is
unlikely it will still have the desired effect. The better option in this
case is to use the less-frequently known width and height values in
the LoaderInfo object for the SWF. Every SWF knows what its
intended stage size should be and that information is stored in an
object that is accessible to every DisplayObject in the display list.
The two lines above would simply become:
player.x = loaderInfo.width/2;
player.y = loaderInfo.height/2;
Search Nedrilad ::




Custom Search