By Rich Shupe, Zevan Rosser
Book Price: $39.99 USD
£24.99 GBP
PDF Price: $31.99
Cover | Table of Contents
Vehicle class that includes traits common to all vehicles, such as the basic physics of movement. You might then create three subclasses: GroundVehicle, WaterVehicle, and AirVehicle. These classes would alter or introduce traits specific to ground, water, and air travel, respectively, but not yet be complete enough to represent an actual vehicle. Further derived classes might be trace() method to place a word into the fOutput panel—an authoring-only panel that accepts text output from your file.
trace("Flash");
trace() method in the correct class syntax.
1 package {
2
3 import flash.display.MovieClip;
4
5 public class Main extends MovieClip {
6
7 public function Main() {
8
9 }
10
11 }
12 }
trace() method as a diagnostic tool to see immediate feedback from your scriptsthisx = x + 1, it is unlikely that you will be solving for the value of x. Instead, this line is assigning a new value to x by adding 1 to its previous value.trace commandtrace command. This instruction places any relevant text into the Output panel of the Flash interface. As such, this is an option that is available only at author-time, and has no use at runtime.myVariable = 1;
var keyword, and citing the type of data to be stored therein by following the name of the variable with a colon (:) and data type. For instance, the previous example of remembering the number 1 should be written this way:var myVariable:Number = 1;
true, and specific code is executed accordingly. If the condition is not met, either no further action is taken or an alternate set of code is executed.if statement. The statement's basic structure is the if keyword, followed by parentheses in which the conditional test resides, and braces in which the code resides that is executed when the statement evaluates to true. The first three lines in the following example establish a set of facts. The if statement evaluates the given facts. (This initial set of facts will be used for this and subsequent examples in this section.)
var a:Number = 1;
var b:String = "hello";
var c:Boolean = false;
if (a == 1) {
trace("option a");
}
for loop. This loop executes its contents a finite number of times. For example, you may wish to create a grid of 25 movie clips or check to see which of 5 radio buttons has been selected. In our first example, we want to trace content to the Output panel three times.for loop is as follows:
for (var i:Number = 0; i < 3; i++) {
trace("hello");
}
i. This is a common technique because the i variable is often used only for counting and, therefore, is created on the spot and not used again. If you have already declared and typed the counter previously, that step can be omitted here. Next is the loop test. In this case, the counter variable must have a value that is less than 3. Finally, the double-plus sign (++) is equivalent to i = i + 1, or add 1 to the current value of i. The result is three occurrences of the word "hello" in the Output panel.Array) to a comma-separated list of items, surrounded by brackets. You can also create an empty array by using the Array class. Both techniques are illustrated here:var myArray:Array = [1, 2, 3] var yourArray:Array = new Array();
push() method, which pushes the value into the array at the end. In short, a method is an action performed by an object—in this case adding something to the array—and will be discussed in detail in the next chapter. You can remove an item from the end of an array using the pop() method.var myArray:Array = new Array(); myArray.push(1); trace(myArray) // 1 appears in the Output panel myArray.push(2); // the array now has two items: 1, 2 trace(myArray.pop()); // the pop() method removes the last item, displaying its value of 2 trace(myArray) // the lone remaining item in the array, 1, is displayed
function showMsg(){
trace("hello");
}
showMsg();
//hello
msg, you can pass a value into that argument when you call the function. By using the argument in the body of the function, it takes on whatever value was sent in. In this example, the function no longer traces "hello" every time it is called. Instead, it traces whatever text is sent into its argument when the function is called. When using arguments, it is necessary to type the data coming in so Flash knows how to react and can issue any warnings needed to notify you of errors.var plane:Object = new Object(); plane.pitch = 0; plane.roll = 5; plane.yaw = 5;
trace(plane.pitch); //0
this can be your friend. It is essentially shorthand for "whichever object or scope you're working with now." Scope is the realm or space within which an object lives. For example, think of a movie clip inside Flash's main timeline. Each of these objects has a unique scope, so a variable or function defined inside the movie clip will not exist in the main timeline, and vice versa.this in context, but here are a couple of examples to get you started. If you wanted to refer to the width of a nested movie clip called mc from within the main timeline, you might say:this.mc.width;
this.parent.mc.width;
this is a reference point from which you start your path. It is fairly common to drop the keyword when going down from the current scope (as in the first example), but it is required when going up to a higher scope (as in the second example). This is because Flash must understand what the parent is actually a parent of in order to start traversing through the hierarchy. Imagine a family reunion in which several extended family members, including cousins and multiple generations, are present, and you are looking for your mother, father, or grandparent. If you just said "parent," any number of parents might answer. If you, instead, said "my parent" or "my mother's parent," that would be specific enough to get you headed in the right direction.ActionScript | Windows OS | Mac OS | Web Site |
|---|---|---|---|
root.mc1.mc2 | c:\folder1\folder2 | Macintosh/folder1/folder2 |
ActionScript | Windows OS | Mac OS | Web Site |
|---|---|---|---|
this.parent.mc1.mc2 | ..\folder1\folder2 | ../folder1/folder2 | ../dir/dir |
setSize(), for example, to simultaneously set the width and height of something. Other methods are more unique, such as navigateToURL(), which instructs a browser to display a web page.x and y properties are typically listed as inherited properties, as is true in the Flash help system. To view inherited properties, for example, in the Flash help system, just click the Show Inherited Public Properties link found immediately under the Public Properties header.alpha property shows only the final state.) The dashed stroke for the visible property is only to show that the box is not visible.Description | Property | Syntax for Setting Value | Units and/or Range |
|---|---|---|---|
Location | x, y |
box.x = 100;
box.y = 100;
| pixels |
Scale (1) | scaleX, scaleY |
box.scaleX = .5;
box.scaleY = .5; | percent / 0–1 |
Scale (2) | width, height |
box.width = 72;
box.height = 72; | pixels |
Rotation | rotation | box.rotation = 45; | degrees / 0–360 |
Transparency | alpha | box.alpha = .5; | percent / 0–1 |
Visibility | visible | box.visible = false; | Boolean |
on(Release) approach. As the language matured, you could create event handlers and apply them remotely using instance names, using myButton.onRelease for example. Finally, you could use event listeners, primarily with components or custom objects.EventDispatcher class that "oversees" event listeners is not new, but it has been improved and is now responsible for handling the majority of events in AS3.EventDispatcher class allows you to listen for the occurrence of events by putting event listeners into service, clean up your code by removing unneeded listeners from service, and manually dispatching events when you need an event to occur at a specific time. You can also check to see whether an object has a listener already set up for a specific event, which we'll look at later when we talk about event propagation.stop() method. Like properties, methods appear consistently in the dot syntax that is the foundation of ActionScript, following the object calling the method. For example, if the movie clip "box" in the main timeline issues the stop() method, the syntax would appear like this:box.stop();
switch statement, reviewed in , is simply a more easily readable if/else-if conditional structure.key down event, which is specified using a constant like most predefined events, but this time it is part of the KeyboardEvent class. When the event is heard, our listener will call the onKeyPressed() function.1 stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPressed);
mouse over and mouse out events. Whenever the user rolled the mouse over one of the clips, it would change it alpha value to indicate interaction. In this case, you would normally have to attach a listener for each event to each movie clip. The code for such an example follows, and depicts the result, where each movie clip is represented by a folder: enter frame event that some display objects have, including the main timeline and the movie clips. An enter frame event is fired