Game Development Reference
In-Depth Information
var myMystery:* = " ? " ;
myMystery = 5; //WILL CAUSE A RUNTIME ERROR
t be caught during
compilation, so it might get missed until your game is deployed
live for real users. Unless absolutely unavoidable (like an instance
where you simply don
What
'
s worse, the above example won
'
t know what will be assigned to a variable),
always type your variables. You
'
'
ll create far less headaches down
the road for yourself.
When you define methods, there are similar practices to follow.
It is best practice to define what parameters a method will receive
and what, if anything, it will return.
function myFunction (myParam:String):void {
//COMMANDS HERE
}
In this example, the method accepts a single parameter,
myParam, and returns nothing. If you have a case where a method
needs to accept an unknown number of parameters, a slightly
different syntax can be used.
function myFunction (...params):void {
//COMMANDS HERE
}
Here, the single parameter, params, is prefixed by three dots.
This signifies to Flash that the parameter should be treated like an
Array of values, so getting to each parameter that was passed must
be done through array syntax:
function myFunction (...params):void {
trace(params[0]);
}
s important to remember that when accepting a variable
number of parameters, type checking during compilation will not
catch any attempts to pass invalid data to the method. In this
instance, it
It
'
s best to do some type of manual checking and gener-
ate errors at runtime. We
'
'
ll cover more on errors shortly.
function myFunction (...params):void {
for (var i:int = 0; i
params.length; i++) {
if (!(params[i] is DisplayObject)) {
throw new ArgumentError(
<
Only DisplayObjects
can be used in myFunction. " );
"
}
}
}
Search Nedrilad ::




Custom Search