By Colin Moock
Book Price: $54.99 USD
£38.99 GBP
PDF Price: $38.99
Cover | Table of Contents
virtualzoo
|- src
|- VirtualZoo.as
.) character (character is simply programming jargon for letter, number, punctuation, and so on). The package name helps distinguish the game.Player class from other classes also named Player, thus preventing name conflicts between different parts of a program or between a program's custom classes and ActionScript's built-in classes.
package packageName {
}
class Identifier {
}
{}) following Identifier in the preceding class definition are a block statement, just like the block statement in a package definition. A class definition's block statement is known as the class block or sometimes the class body. The class block contains directives that describe the characteristics and behavior of the class and its instances.
package zoo {
class VirtualZoo {
}
}
package zoo {
class VirtualPet {
}
}
//), and multiline, which start with the character sequence /*, and end with the character sequence */.// No one here but us programmers
/* No one here but us programmers */
// Contents of the file VirtualZoo.as
package zoo {
public class VirtualZoo {
}
}
// Contents of the file VirtualPet.as
package zoo {
internal class VirtualPet {
}
}
class SomeClass { function SomeClass ( ) { } }
{}) following the parameter list are a block statement, just like the block statements in package and class definitions. A constructor method's block statement is known as the constructor body. The constructor body contains the directives that initialize instances. Whenever a new instance of SomeClass is created, the directives in the constructor body are executed (sequentially, from top to bottom). Executing the directives in the constructor body is known as executing the constructor or, more casually, running the constructor.class SomeClass { // Empty constructor. This class does not require initialization. function SomeClass ( ) { } }
new ClassName
new VirtualPet
new VirtualPet new VirtualPet
new ClassName
new Date
25.4
"hello"
true, we can use the convenient literal form:true
false, we can use the convenient literal form:false
package zoo {
public class VirtualZoo {
public function VirtualZoo ( ) {
null and undefined, which represent the concept of "no value." A variable is an identifier (i.e., a name) associated with a value. For example, a variable might be the identifier submitBtn associated with an object representing a button in an online form. Or a variable might be the identifier productDescription associated with a String object that describes some product.class SomeClass { public function SomeClass ( ) { var identifier = value; } }
class SomeClass { function SomeClass (identifier = value) { } }
class SomeClass { function SomeClass (identifier1 = value1, identifier2 = value2, identifier3 = value3) { } }
value supplied in that parameter's definition. However, a constructor parameter's value can alternatively be supplied when an object is instantiated, using the following generalized object-creation code:new SomeClass(value1, value2, value3)
new Date( )
2.5
+, or a keyword, such as instanceof.*). The following code shows a compound expression that multiplies 4 and 2.5:4 * 2.5
4 * 2.5) is replaced by that single calculated result (10). Calculating the value of an expression is known as evaluating the expression.quantity * price
quantity and price are placeholders for values that will be determined at runtime. The value of quantity might be, say, a number supplied by the user, while the value of name, whose value was supplied externally by object-creation code in the VirtualZoo class. Here's the code for the VirtualPet and VirtualZoo classes, as we left them:
// VirtualPet class
package zoo {
internal class VirtualPet {
internal var petName = "Unnamed Pet";
public function VirtualPet (name) {
}
}
}
// VirtualZoo class
package zoo {
public class VirtualZoo {
public function VirtualZoo ( ) {
var pet = new VirtualPet("Stan");
}
}
}
name parameter to assign the value "Stan" to the new VirtualPet object's petName instance variable.object.instanceVariable = value
this, which is an automatically created parameter whose value is the object being created:this
this.this, we write a dot, followed by the name of the instance variable whose value we wish to assign—in this case petName.this.petName
this.petName = value
name parameter. Hence, for value, we write simply: name.this.petName = name
name, in the preceding code, with the value passed to the VirtualPet constructor. That value is then assigned to the instance variable pet. We'll make pet private so that it can be accessed by code in the VirtualZoo class only. Here's the code (the new instance variable is shown in bold):
package zoo {
public class VirtualZoo {
private var pet;
public function VirtualZoo ( ) {
this.pet = new VirtualPet("Stan");
}
}
}
class SomeClass { function identifier ( ) { } }
{}) following the parameter list are a block statement. A instance method's block statement is known as the method body. The method body contains directives that perform some task.( ), following the method name.object.methodName( )
radius is not a member of Box," meaning that the Box class does not define any methods or variables named radius.petName, currentCalories, and creationTime. Those three instance variables represent the following pet characteristics: the pet's nickname, the amount of food in the pet's stomach, and the pet's birth date. For a new VirtualPet object, the initial value of currentCalories is a number created using the literal expression 1000. The initial value of creationTime is a Date object representing the time at which each VirtualPet object is created. When a VirtualPet object is created, petName is assigned the value of the required constructor parameter, name. The constructor parameter name receives its value through a constructor argument, supplied by the new expression that creates the VirtualPet object.currentCalories by the specified numeric value. The getAge( ) method calculates and returns the pet's current age, in milliseconds.
// VirtualPet class
package zoo {
internal class VirtualPet {
internal var petName;
private var currentCalories = 1000;
private var creationTime;
public function VirtualPet (name) {
this.creationTime = new Date( );
this.petName = name;
}
public function eat (numberOfCalories) {
this.currentCalories += numberOfCalories;
}
public function getAge ( ) {
var currentTime = new Date( );
var age = currentTime.time - this.creationTime.time;
return age;
}
}
}
// VirtualZoo class
package zoo {
public class VirtualZoo {
private var pet;
public function VirtualZoo ( ) {
this.pet = new VirtualPet("Stan");
}
}
}
?:, which is covered briefly in . For details on the ?: operator, see Adobe's ActionScript language reference.if (testExpression) { codeBlock1 } else { codeBlock2 }
true, then the first block is executed. If the value of testExpression is the Boolean value false, then the second block is executed. If the value of testExpression is not a Boolean value, ActionScript automatically converts ?:, which is covered briefly in . For details on the ?: operator, see Adobe's ActionScript language reference.if (testExpression) { codeBlock1 } else { codeBlock2 }
true, then the first block is executed. If the value of testExpression is the Boolean value false, then the second block is executed. If the value of testExpression is not a Boolean value, ActionScript automatically converts testExpression to a Boolean object and uses the result of that conversion to decide which block to execute. (The rules for converting a value to the Boolean class are described in in Chapter 8.)true, so the value of the variable greeting is set to "Hello", not "Bonjour".
var greeting;
// Test expression is true, so...
if (true) {
// ...this code runs
greeting = "Hello";
} else {
// This code doesn't run
greeting = "Bonjour";
}
true. A loop, on the other hand, causes a statement block to be executed repeatedly, for as long as its test expression remains true.true:while (testExpression) { codeBlock }
true, the code in codeBlock (called the loop body) is executed. But, unlike the if statement, when the codeBlock is finished, execution begins again at the beginning of the while statement (that is, ActionScript "loops" back to the beginning of the while statement). The second pass through the while statement works just like the first: the testExpression is evaluated, and if it is still true, codeBlock is executed again. This process continues until testExpression becomes false, at which point execution continues with any statements that follow the while statement in the program. If testExpression never yields false, the loop executes infinitely, eventually causing the Flash runtime to generate an error, which stops the loop (and all currently executing code). To avoid infinite execution, a while loop's codeBlock typically includes a statement that modifies the testExpression, causing it to yield false when some condition is met.language is "english", then display "Hello". But not all programming logic is so simple. Programs often need to consider multiple factors in branching logic (i.e., decision making). To manage multiple factors in a test expression, we use the Boolean operators: || (logical OR) and && (logical AND).||. Typically, the pipe character (|) is accessible using the Shift key and the Backslash (\) key in the upper right of most Western keyboards, where it may be depicted as a dashed vertical line. Logical OR has the following general form:expression1 || expression2