Summary of DIRECTOR MX 2004-USING DIRECTOR

  • Page 1

    Using actionscript in flash.

  • Page 2

    Trademarks add life to the web, afterburner, aftershock, andromedia, allaire, animation powerpack, aria, attain, authorware, authorware star, backstage, bright tiger, clustercats, coldfusion, contribute, design in motion, director, dream templates, dreamweaver, drumbeat 2000, edje, ejipt, extreme 3d...

  • Page 3: Contents

    3 contents introduction: getting started with actionscript . . . . . . . . . . . . . . . . . . . . . . . 7 intended audience . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 system requirements . . . . . . . . . . . . . . . . . . . . . . . . . . ....

  • Page 4

    4 contents behaviors conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 screens conventions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107 video conventions. . . . . . . . . . . . . . . . . . . ....

  • Page 5

    Contents 5 adding parameters to dynamically created movie clips . . . . . . . . . . . . . . . . . . . . 213 managing movie clip depths . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215 drawing shapes with actionscript. . . . . . . . . . . . . . . . . . . . . . . . ....

  • Page 6

    6 contents appendix a: error messages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 305 appendix b: deprecated flash 4 operators . . . . . . . . . . . . . . . . . . . . . . . . . . . 311 appendix c: keyboard keys and key code values . . . . . . . . . . . . . . . . . . ....

  • Page 7: Introduction

    7 introduction getting started with actionscript macromedia flash mx 2004 and macromedia flash mx professional 2004 are the professional standard authoring tools for producing high-impact web experiences. Actionscript is the language you use to add interactivity to a flash applications, whether your...

  • Page 8

    8 introduction: getting started with actionscript • chapter 1, “what’s new in flash mx 2004 actionscript,” on page 11 describes features that are new in actionscript, changes to the compiler and debugger, and the new programming model for the actionscript 2.0 language. • chapter 2, “actionscript bas...

  • Page 9

    Additional resources 9 before writing your own scripts, you should complete the lessons “write scripts with actionscript” and “create a form with conditional logic and send data,” which provide a hands-on introduction to working with actionscript. To find these lessons, select help > how do i > quic...

  • Page 10

    10 introduction: getting started with actionscript the macromedia devnet website ( www.Macromedia.Com/devnet ) is updated regularly with the latest information on flash, plus advice from expert users, advanced topics, examples, tips, and other updates. Check the website often for the latest news on ...

  • Page 11: Chapter 1

    11 chapter 1 what’s new in flash mx 2004 actionscript macromedia flash mx 2004 and macromedia flash mx professional 2004 provide several enhancements that make it easy for you to write robust scripts using the actionscript language. The new features, which are discussed in this chapter, include new ...

  • Page 12

    12 chapter 1: what’s new in flash mx 2004 actionscript new and changed language elements this section describes the actionscript language elements that are new or changed in flash mx 2004. To use any of these elements in your scripts, you must target flash player 7 (the default) when you publish you...

  • Page 13

    Porting existing scripts to flash player 7 13 • the textfield.Condensewhite property lets you remove extra white space from html text fields that are rendered in a browser. • the textfield.Mousewheelenabled property lets you specify whether a text field’s contents should scroll when the mouse pointe...

  • Page 14

    14 chapter 1: what’s new in flash mx 2004 actionscript also, flash player 7 implements several changes that affect how one swf file can access another swf file, how external data can be loaded, and how local settings and data (such as privacy settings and locally persistent shared objects) can be ac...

  • Page 15

    Porting existing scripts to flash player 7 15 domain-name rules for settings and local data in flash player 6, superdomain matching rules are used by default when accessing local settings (such as camera or microphone access permissions) or locally persistent data (shared objects). That is, the sett...

  • Page 16

    16 chapter 1: what’s new in flash mx 2004 actionscript in files published for flash player 5 or earlier, there were no restrictions on cross-domain or subdomain access. In files published for flash player 6, you could use the localconnection.Allowdomain handler or system.Security.Allowdomain() metho...

  • Page 17

    Porting existing scripts to flash player 7 17 you might also have to add statements such as these to your files if you aren’t currently using them. For example, if your swf file is hosted at www.Somesite.Com and you want to allow access by a swf file published for flash player 7 at store.Somesite.Co...

  • Page 18

    18 chapter 1: what’s new in flash mx 2004 actionscript http to https protocol access between swf files as discussed in the previous section, rules for cross-domain and subdomain access have changed in flash player 7. In addition to the exact-domain matching rules now being implemented, you must expl...

  • Page 19

    Porting existing scripts to flash player 7 19 • if the called file is published for flash player 6 or earlier, and both the calling and called files are in same domain (for example, a file in http://www.Domain.Com is calling a file in https:// www.Domain.Com), no modification is needed. • if the cal...

  • Page 20

    20 chapter 1: what’s new in flash mx 2004 actionscript actionscript editor changes the actionscript editor has been updated in several ways to make it more robust and easier to use. These changes are summarized in this section. Word wrapping you can now use the options pop-up menu in the script pane...

  • Page 21

    New object-oriented programming model 21 debugging changes this section describes changes that improve your ability to debug your scripts. Output window changed to output panel you can now move and dock the output panel in the same way as any other panel in flash. Improved error reporting at compile...

  • Page 22

    22 chapter 1: what’s new in flash mx 2004 actionscript • interface • dynamic • static • public • private • get • set • import key facts about actionscript 2.0 include the following points: • scripts that use actionscript 2.0 to define classes or interfaces must be stored as external script files, wi...

  • Page 23: Chapter 2

    23 chapter 2 actionscript basics actionscript has rules of grammar and punctuation that determine which characters and words are used to create meaning and in which order they can be written. For example, in english, a period ends a sentence; in actionscript, a semicolon ends a statement. The genera...

  • Page 24

    24 chapter 2: actionscript basics differences between actionscript and javascript actionscript is similar to the core javascript programming language. You don’t need to know javascript to use and learn actionscript; however, if you know javascript, actionscript will seem familiar. This manual does n...

  • Page 25

    Terminology 25 constructors are functions that you use to define (initialize) the properties and methods of a class. By definition, constructors are functions within a class definition that have the same name as the class. For example, the following code defines a circle class and implements a const...

  • Page 26

    26 chapter 2: actionscript basics instance names are unique names that let you target instances you create, or movie clip and button instances on the stage. For example, in the following code, “names” and “studentname” are instance names for two objects, an array and a string: var names:array = new ...

  • Page 27

    Terminology 27 operators are terms that calculate a new value from one or more values. For example, the addition ( + ) operator adds two or more values together to produce a new value. The values that operators manipulate are called operands. Parameters (also called arguments) are placeholders that ...

  • Page 28

    28 chapter 2: actionscript basics syntax as with all scripting languages, actionscript has syntax rules that you must follow to create scripts that can compile and run correctly. This section describes the elements that comprise actionscript syntax: • “case sensitivity” on page 28 • “dot syntax” on ...

  • Page 29

    Syntax 29 case-sensitivity is implemented on a per-movie basis. If a strict flash player 7 application calls a non-strict flash player 6 movie, actionscript executed in the latter movie is non-strict. For example, if you use loadmovie() to load a flash player 6 swf into a flash player 7 swf, the ver...

  • Page 30

    30 chapter 2: actionscript basics slash syntax slash syntax was used in flash 3 and 4 to indicate the target path of a movie clip or variable. This syntax is still supported by flash player 7, but its use is not recommended, and slash syntax is not supported in actionscript 2.0. However, if you are ...

  • Page 31

    Syntax 31 semicolons an actionscript statement is terminated with a semicolon ( ; ), as shown in the following examples: var column:number = passeddate.Getday(); var row:number = 0; if you omit the terminating semicolon, flash still compiles your script successfully. However, it is good scripting pr...

  • Page 32

    32 chapter 2: actionscript basics as shown in the following example, to indicate that a line or portion of a line is a comment, precede the comment with two forward slashes ( // ): my_btn.Onrelease = function() { // create new date object var mydate:date = new date(); var currentmonth:number = mydat...

  • Page 33

    Syntax 33 all class names, component class names, and interface names are reserved words: eq extends finally for function ge get gt if ifframeloaded implements import in instanceof interface intrinsic le lt ne new not on onclipevent or private public return set static switch telltarget this throw tr...

  • Page 34

    34 chapter 2: actionscript basics constants a constant is a property whose value never changes. Actionscript contains predefined constants. For example, the constants backspace , enter , space , and tab are properties of the key object and refer to keyboard keys. To test whether the user is pressing...

  • Page 35

    About data types 35 you can convert one data type to another using one of the following conversion functions: array() , boolean() , number() , object() , string() . In actionscript 2.0, you can assign data types to variables when you initialize them. The data types you assign can be any of the built...

  • Page 36

    36 chapter 2: actionscript basics number data type the number data type is a double-precision floating-point number. The minimum value of a number object is approximately 5e-324. The maximum is approximately 1.79e+308. You can manipulate numbers using the arithmetic operators addition ( + ), subtrac...

  • Page 37

    About data types 37 } }; submit_button.Addeventlistener("click", btnlistener); for more information, see “using built-in functions” on page 60 and “logical operators” on page 53 . Object data type an object is a collection of properties. Each property has a name and a value. The value of a property ...

  • Page 38

    38 chapter 2: actionscript basics my_mc.Startdrag(true); parent_mc.Geturl("http://www.Macromedia.Com/support/" + product); the second example returns the width of a movie clip called my_mc on the stage. The targeted instance must be a movie clip, and the returned value must be a numeric value. Funct...

  • Page 39

    Assigning data types to elements 39 null data type the null data type has only one value, null . This value means no value—that is, a lack of data. You can assign the null value in a variety of situations to indicate that a property or variable does not yet have a value assigned to it. The following...

  • Page 40

    40 chapter 2: actionscript basics • values returned from a function or method • objects created as subclasses of existing classes in actionscript 2.0, you should explicitly assign data types to items, which can help prevent or diagnose certain errors in your scripts at compile time and offers other ...

  • Page 41

    Assigning data types to elements 41 strict data typing actionscript 2.0 lets you explicitly declare the object type of a variable when you create it, which is called strict data typing. Strict data typing offers several benefits at compile time. Because data type mismatches trigger compiler errors, ...

  • Page 42

    42 chapter 2: actionscript basics using strict typing also helps to ensure that you do not attempt to access properties or methods that are not part of an object’s type. Another advantage of strict data typing is that flash mx 2004 automatically shows code hints for built-in objects when they are st...

  • Page 43

    Assigning data types to elements 43 in this situation, you asserted to the compiler that foo is a dog object, and, therefore, the compiler assumes that temp.Bark() ; is a legal statement. However, the compiler doesn’t know that the cast will fail (that is, that you tried to cast a cat object to an a...

  • Page 44

    44 chapter 2: actionscript basics about variables a variable is a container that holds information. The container itself is always the same, but the contents can change. By changing the value of a variable as the swf file plays, you can record and save information about what the user has done, recor...

  • Page 45

    About variables 45 you should not use any element in the actionscript language as a variable name because it can cause syntax errors or unexpected results. In the following example, if you name a variable string and then try to create a string object using new string() , the new object is undefined:...

  • Page 46

    46 chapter 2: actionscript basics make sure to declare a timeline variable before trying to access it in a script. For example, if you put the code var x = 10; in frame 20, a script attached to any frame before frame 20 cannot access that variable. Local variables to declare local variables, use the...

  • Page 47

    About variables 47 } } count(); trace(counter); // accesses global variable and displays 100 this example simply shows that the global variable is not accessed in the scope of the count() function. To avoid confusion in your applications, name your variables uniquely. The flash player version 7 and ...

  • Page 48

    48 chapter 2: actionscript basics in the following example, the variable invalue contains a primitive value, 3, so that value is passed to the sqr() function and the returned value is 9: function sqr(x:number):number { var x:number = x * x; return x; } var invalue:number = 3; var out:number = sqr(in...

  • Page 49

    Using operators to manipulate values in expressions 49 using operators to manipulate values in expressions an expression is any statement that flash can evaluate and that returns a value. You can create an expression by combining operators and values or by calling a function. Operators are character...

  • Page 50

    50 chapter 2: actionscript basics when two or more operators share the same precedence, their associativity determines the order in which they are performed. Associativity can be either left-to-right or right-to-left. For example, the multiplication (*) operator has an associativity of left-to-right...

  • Page 51

    Using operators to manipulate values in expressions 51 numeric operators numeric operators add, subtract, multiply, divide, and perform other arithmetic operations. The most common use of the increment operator is i++ instead of the more verbose i = i+1 . You can use the increment operator before or...

  • Page 52

    52 chapter 2: actionscript basics comparison operators comparison operators compare the values of expressions and return a boolean value ( true or false ). These operators are most commonly used in loops and in conditional statements. In the following example, if the variable score is 100 , a certai...

  • Page 53

    Using operators to manipulate values in expressions 53 string operators the addition (+) operator has a special effect when it operates on strings: it concatenates the two string operands. For example, the following statement adds "congratulations," to "donna!" : "congratulations, " + "donna!" the r...

  • Page 54

    54 chapter 2: actionscript basics consider the order of operands, especially if you’re setting up complex conditions and you know how often one condition is true compared with other conditions. In the previous example, if you know that, in most cases, i will be greater than 50, consider putting the ...

  • Page 55

    Using operators to manipulate values in expressions 55 the strict equality ( === ) operator is similar to the equality operator, with one important difference: the strict equality operator does not perform type conversion. If the two operands are of different types, the strict equality operator retu...

  • Page 56

    56 chapter 2: actionscript basics dot and array access operators you can use the dot operator ( . ) and the array access operator ( [] ) to access built-in or custom actionscript object properties, including those of a movie clip. Dot operator. The dot operator uses the name of an object on its left...

  • Page 57

    Specifying an object’s path 57 function bargetprop():string { return "bar"; } function barsetprop(str:string):void { } bar.Addproperty("someprop", bargetprop, barsetprop); //trace(bar.Someprop); // generates an error trace(bar["someprop"]); // no error you can use the eval() function to dynamically ...

  • Page 58

    58 chapter 2: actionscript basics to dynamically create a movie clip: • use the movieclip.Attachmovie() , movieclip.Createemptymovieclip() , or movieclip.Duplicatemovieclip() method. The following example uses the attachmovie() method to dynamically create the purpledot2_mc movie clip and place it o...

  • Page 59

    Using condition statements 59 gotoandplay("startprogram"); } if you want to check for one of several conditions, you can use the switch statement rather than multiple else if statements. Repeating actions actionscript can repeat an action a specified number of times or while a specific condition exi...

  • Page 60

    60 chapter 2: actionscript basics to loop through the children of a movie clip or an object: • use the for..In statement. Children include other movie clips, functions, objects, and variables. The following example uses the trace statement to print its results in the output panel: var myobject:objec...

  • Page 61

    Creating functions 61 creating functions you can define functions to execute a series of statements on passed values. Your functions can also return values. After a function is defined, it can be called from any timeline, including the timeline of a loaded swf file. A well-written function can be th...

  • Page 62

    62 chapter 2: actionscript basics passing parameters to a function parameters are the elements on which a function executes its code. (in this manual, the terms parameter and argument are interchangeable.) for example, the following function takes the parameters initials and finalscore : function fi...

  • Page 63

    Creating functions 63 for example, the following function returns the square of the parameter x and specifies that the returned value must be a number: function sqr(x:number):number { return x * x; } some functions perform a series of tasks without returning a value. For example, the following funct...

  • Page 64

    64 chapter 2: actionscript basics.

  • Page 65: Chapter 3

    65 chapter 3 using best practices it is important for macromedia flash designers and developers to build applications or animations while being concerned about writing code and structuring applications in a way that is beneficial either to themselves, or to the other people who might work on the pro...

  • Page 66

    66 chapter 3: using best practices therefore, it is particularly useful to follow guidelines in the following situations, and for the following reasons: when working on fla documents adopting consistent and efficient practices helps you speed up your workflow. It is faster to develop using best prac...

  • Page 67

    Working with fla files 67 • clearly describe the purpose or content of each layer or folder when you name them in a fla file. This helps users to quickly understand where particular assets are found in layers or folders. It is a good and common practice to name the layer that contains your actionscr...

  • Page 68

    68 chapter 3: using best practices there are some situations where few of these disadvantages apply, such as when you create lengthy animations, which is a good time to use scenes. If disadvantages apply to your document, consider using screens to build an animation instead of using scenes. For more...

  • Page 69

    General coding conventions 69 caution: when you select save and compact, you cannot undo any changes you made before you saved the file. If you select save when working with a document, you can undo prior to that save point. Because save and compact deletes the earlier version of the file and replac...

  • Page 70

    70 chapter 3: using best practices limit your use of abbreviations, but use them consistently. An abbreviation must clearly stand for only one thing. For example, the abbreviation sec might represent section and second. Make sure you use it only for one term, and choose a different abbreviation for ...

  • Page 71

    General coding conventions 71 switch = true; new = "funk"; always strict data type your variables, which helps avoid errors in your code and trigger code completion. For more information on strict typing and suffixes, see “using code completion and suffixes” on page 78 . You should use suffixes with...

  • Page 72

    72 chapter 3: using best practices constants variables should be lowercase or mixed-case letters; however, constants (variables that do not change) should be uppercase. Separate words with underscores, as the following actionscript shows: var base_url:string = "http://www.Macromedia.Com"; //constant...

  • Page 73

    General coding conventions 73 classes and objects class names are usually nouns or qualified nouns, beginning with an uppercase first letter. A qualifier describes the noun or phrase. For example, instead of member, you might qualify the noun using newmember or oldmember . Sometimes a class name is ...

  • Page 74

    74 chapter 3: using best practices // or mx.Containers.Textinput this convention uses a prefix of mx.Containers . If you create your own packages, use a consistent naming convention, such as starting with com.Macromedia.Packagename . Or, use a convention of naming your packages com . Yourcompany.Pac...

  • Page 75

    General coding conventions 75 for more information on component conventions, see “working with components in flash player” on page 115 . Remember that the naming conventions in this section are guidelines. It is most important to use a naming scheme that works well for you and use it consistently. A...

  • Page 76

    76 chapter 3: using best practices all class names, component class names, and interface names are also reserved words, as listed in the following table: formatting code formatting actionscript is essential to writing maintainable code. For example, it would be extremely difficult to follow the logi...

  • Page 77

    General coding conventions 77 for more information on formatting code, see the following topics: • “using comments in code” on page 77 • “using code completion and suffixes” on page 78 • “using recommended suffixes” on page 80 • “spacing and readability” on page 81 using comments in code one of most...

  • Page 78

    78 chapter 3: using best practices note: if you place the comment characters ( /* and */) on separate lines at the beginning and end of the comment, you can easily comment them out by placing double slash characters (//) in front of them (for example, ///* and //*/). This lets you quickly and easily...

  • Page 79

    General coding conventions 79 to strict data type your variables, you must define the variable using the var keyword. In the following example, when creating a loadvars object, you would use strict data typing: var params_lv:loadvars = new loadvars(); strict data typing provides you with code comple...

  • Page 80

    80 chapter 3: using best practices however, the following actionscript throws an error and does not allow you to use code completion: var myscrollpane:scrollpane; //no code completion, throws error using recommended suffixes you can use the following suffixes to generate code completion in the actio...

  • Page 81

    General coding conventions 81 use the following suffixes with component instances. These suffixes do not generate code completion in the actions panel. However, using suffixes encourages consistency in actionscript that might be shared among developers. Spacing and readability use spaces, line break...

  • Page 82

    82 chapter 3: using best practices use line breaks to make complex statements easier to read you can format some statements, such as conditional statements, in several ways. Sometimes, formatting statements across several lines rather than a single line makes it easier to read. For more information ...

  • Page 83

    Actionscript coding standards 83 keeping actions together whenever possible, put your actionscript in a single location. If you put code in a fla file, put actionscript on the first or second frame on the timeline, in a layer called actions that is the first or second layer on the timeline. Sometime...

  • Page 84

    84 chapter 3: using best practices attaching actionscript to a button called mybutton_btn looks like the following actionscript, and should be avoided: on (release) { //do something } however, placing actionscript with the same purpose on the timeline looks like the following code, which is encourag...

  • Page 85

    Actionscript coding standards 85 remember, regardless of the version of actionscript, you should follow good practices. Many of these practices, such as remaining consistent with case sensitivity, using code completion, enhancing readability, avoiding keywords for instance names, and keeping a consi...

  • Page 86

    86 chapter 3: using best practices for more information on guidelines for writing actionscript, see the following topics: • “adding initialization” on page 86 • “using trace statements” on page 87 • “using the super prefix” on page 87 • “avoiding the with statement” on page 87 • “using variables” on...

  • Page 87

    Actionscript coding standards 87 using trace statements use trace statements in your documents to help you debug your code while authoring the fla file. For example, by using a trace statement and for loop, you can see the values of variables in the output panel, such as strings, arrays, and objects...

  • Page 88

    88 chapter 3: using best practices createtextfield("label_txt", 100, 0, 20, 100, 22); label_txt.Text = "circle 1"; somevariable = true; } in this code, you attach a movie clip instance from the library and modify its properties using the with statement. When you do not specify a variable’s scope, yo...

  • Page 89

    Actionscript coding standards 89 } } delete variables or make variables null when you no longer need them. Setting variables to null can still enhance performance. This process is commonly called garbage collection. Deleting variables helps optimize memory use during runtime, because unneeded assets...

  • Page 90

    90 chapter 3: using best practices writing conditional statements place conditions put on separate lines in if , else-if , and if-else statements. Your if statements should use braces ( {} ). You should format braces like the following examples. The if , if-else , and else-if statements have the fol...

  • Page 91

    Actionscript coding standards 91 the shortened if statement syntax is known as the conditional operator ( ?: ). It lets you convert simple if-else statements into a single line of code. In this case, the shortened syntax reduces readability, and so it is not preferable. Do not use this syntax for co...

  • Page 92

    92 chapter 3: using best practices using the for statement write the for statement using the following format: for (init; condition; update) { // statements; } the following structure demonstrates the for statement: for (var i = 0; i myclip_mc.Duplicatemovieclip("newclip"+i+"_mc", i+10, {_x:i*100, _...

  • Page 93

    Actionscript coding standards 93 using return statements do not use parentheses [()]with any return statements that have values. The only time you should use parentheses with return statements is when it makes the value more obvious, which is shown in the third line of the following actionscript: re...

  • Page 94

    94 chapter 3: using best practices using listener syntax there are several ways to write listeners for events in flash mx 2004. Some popular techniques are shown in the following code examples. The first example shows a properly formatted listener syntax, which uses a loader component to load conten...

  • Page 95

    Actionscript coding standards 95 following general formatting guidelines adding spacing (or white space) to your syntax is recommended because it makes your actionscript easier to read. The following formatting points are recommended to help promote readability in your actionscript. The following ex...

  • Page 96

    96 chapter 3: using best practices • “using the this keyword” on page 96 • “using scope in classes” on page 97 avoiding _root there are several ways to target instances that let you avoid using _root ; these are discussed later in this section. Avoid using _root in actionscript because it can cause ...

  • Page 97

    Actionscript coding standards 97 this.Stopdrag(); }; for a class, you can write code in the following format: class user { private var m_username:string; private var m_password:string; function user(username:string, password:string) { this.M_username = username; this.M_password = password; } public ...

  • Page 98

    98 chapter 3: using best practices // begin loading the xml document. Prod_xml.Load(targetxml_string); } public function init():void { // display the xml packet. Trace(this.M_products_xml); } } because you are trying to reference the private member variable within an onload handler, the this keyword...

  • Page 99

    Using classes and actionscript 2.0 99 class mx.Site.Utils { static function randomrange(min:number, max:number):number { if (min>max) { var temp:number = min; min = max; max = temp; } return (math.Floor(math.Random()*(max-min+1))+min); } static function arraymin(num_array:array):number { if (num_arr...

  • Page 100

    100 chapter 3: using best practices the flash mx 2004 object model does not support multiple inheritance; it supports only single inheritance. Therefore, a class can come from a single parent class. This parent class can be either a native flash class or a user-defined class. Flash mx 2004 uses inte...

  • Page 101

    Using classes and actionscript 2.0 101 instance variables follow static variables. Write the public member variables first, and follow them with private member variables. Following the public and private member variables, add the constructor statement, such as the one in the following example: publi...

  • Page 102

    102 chapter 3: using best practices programming classes there are several general guidelines for programming classes. These guidelines help you write well-formed code, but also remember to follow the guidelines provided in “general coding conventions” on page 69 and “actionscript coding standards” o...

  • Page 103

    Using classes and actionscript 2.0 103 there are two kinds of comments in a typical class or interface file: documentation comments and implementation comments. Documentation comments are used to describe the code’s specifications and do not describe the implementation. Implementation comments are u...

  • Page 104

    104 chapter 3: using best practices using design patterns design patterns help developers structure their application in a particular, established way. There are many different design patterns that developers use in classes and for application design. Using a design pattern is helpful when working i...

  • Page 105

    Behaviors conventions 105 behaviors conventions behaviors are prewritten code snippets that can be instantly added to parts of a fla file. The introduction of behaviors has added to the complexity of determining best practices in flash, because the way some behaviors are added does not follow typica...

  • Page 106

    106 chapter 3: using best practices find and replace lets you search for strings and replace them in a fla document. Script pinning lets you pin multiple scripts from various objects and work with them simultaneously in the actions panel. This works best with the script navigator. Movie explorer let...

  • Page 107

    Screens conventions 107 being consistent there are some guidelines for using behaviors; the main thing is consistency. If you add actionscript to a fla file, put code in the same locations where behaviors are added, and document how and where you add code. Note: if you are using a screen-based fla f...

  • Page 108

    108 chapter 3: using best practices like the behaviors guidelines, there are issues with how to organize and structure projects built with the screen-based authoring environment. Screens provide an intelligent and easy to use framework to control loading, persistence of data, and state using classes...

  • Page 109

    Screens conventions 109 if you use behaviors placed on screens (or other instances), remember to document the location on frame 1 of the main timeline. This is particularly important if you also place actionscript on the timeline. The following code is an example of the comment you might want to add...

  • Page 110

    110 chapter 3: using best practices working with other structural elements a screen-based document, when published, is essentially a single movie clip on the first frame of a timeline. This movie clip contains a few classes that compile into the swf file. These classes add additional file size to th...

  • Page 111

    Video conventions 111 give users a certain amount of control (such as the ability to stop, pause, play, and resume the video, and control volume) over the video in a swf file. For more information on using video in flash, see “working with video” in using flash. Importing and embedding video you can...

  • Page 112

    112 chapter 3: using best practices note: remember to delete the flv file from your flash document and library if you intend to dynamically load the video into that document at runtime. Flash mx professional includes an external flv exporter that compresses video from third-party video editing softw...

  • Page 113

    Video conventions 113 using media components media components are used to display flv files or play mp3 files in a swf file, and they only support these two file types. You can export each file format using a variety of software. For information on exporting the flv format, see “exporting flv files”...

  • Page 114

    114 chapter 3: using best practices to dynamically load video using actionscript: 1. Add a video object to the stage by selecting new video from the library panel’s options menu. 2. Drag the video object on to the stage, and resize the instance using the property inspector to the same dimensions as ...

  • Page 115

    Performance and flash player 115 optimize bitmaps as much as possible without overcompressing them. A 72-dpi resolution is optimal for the web. Compressing a bitmap image reduces file size, but compressing it too much compromises the quality of the graphic. Check that the settings for jpeg quality i...

  • Page 116

    116 chapter 3: using best practices if you use multiple components that do not share the same framework, they might add substantial file size to the swf file. For example, the xmlconnector component adds 17k to the swf file, and textinput components add 24k to your document. If you add the combobox ...

  • Page 117

    Performance and flash player 117 if you must restyle your components, you can improve efficiency in your application by using the loader component. If you want to implement several styles in different components, you can place each component in its own swf file. If you change styles on the loader co...

  • Page 118

    118 chapter 3: using best practices you cannot use any classes until the playhead reaches the frame you choose to load them into. Because components require classes for their functionality, you must load components after the export frame for actionscript 2.0 classes. If you export for frame 3, you c...

  • Page 119

    Performance and flash player 119 using system.Usecodepage in your code forces the swf file to use the system’s code page instead of unicode. Only use this process in the following situations: when you are loading non-unicode encoded text from an external location and when this text is encoded with t...

  • Page 120

    120 chapter 3: using best practices optimizing actionscript in flash player there are several ways that you can optimize your code for better swf file performance, but remember that optimizing your code for flash player might reduce readability and consistency for code maintenance. Only practice opt...

  • Page 121

    Guidelines for flash applications 121 guidelines for flash applications the best way to create different flash applications depends on the application you create and the technology that you are using to build the application. There are guidelines that can help make the application process easier. Th...

  • Page 122

    122 chapter 3: using best practices collecting and formatting data applications depend on user interaction with the swf file. Frequently, it depends on the user entering data into forms, such as using combo boxes, buttons, text fields, sliders, and so on. You might create custom input devices, use t...

  • Page 123

    Guidelines for flash applications 123 you can also use escape and unescape for decoding. For a complete list of url-encoded special characters, see www.Macromedia.Com/support/flash/ts/documents/url_encoding.Htm . You must typically process information before you send it to the server, so it’s format...

  • Page 124

    124 chapter 3: using best practices there are some limitations when using web services with a swf file, including the following: • flash does not support web services with more than one defined port when using the webserviceconnector component. • flash does not support web services with more than on...

  • Page 125

    Guidelines for flash applications 125 you might receive data from a web service or from flash remoting. In both cases, you could receive complex data structures, such as arrays, objects, or record sets, which you must parse and bind appropriately. Using error handling and debugging an important part...

  • Page 126

    126 chapter 3: using best practices using the mvc design pattern many flash developers implement the mvc (model, view, controller) design pattern when they build applications to separate the logic and data of the application from the user interface. This section defines how the design pattern works,...

  • Page 127

    Projects and version control guidelines 127 there are other reasons why mvc is valuable for some applications. An important reason to use the pattern is to separate data and logic from the user interface. By separating these parts of the process, you can have several different graphical interfaces t...

  • Page 128

    128 chapter 3: using best practices using projects you can group multiple files into a single project file using the project panel in flash mx professional 2004. This helps simplify application building, where managing related files could get complex and confusing. You can define a site for your wor...

  • Page 129

    Guidelines for accessibility in flash 129 authoring projects authors on a flash project do not change the project root, directory structure of the project, or the site. This includes adding, removing, or changing subdirectory names, or adding additional subdirectories to the project on their local c...

  • Page 130

    130 chapter 3: using best practices creating accessible sites flash player uses microsoft active accessibility (msaa) to expose flash content to screen readers. Msaa is a windows-based technology that provides a standardized platform for information exchange between assistive technologies, such as s...

  • Page 131

    Guidelines for accessibility in flash 131 using screen readers a screen reader is software that lets your visitors hear a description of the contents of web pages. Text is read aloud using specially designed software. Obviously, a screen reader can only interpret textual content. However, any descri...

  • Page 132

    132 chapter 3: using best practices controlling descriptions and repetition designers and developers can assign descriptions for the animations, images, and graphics in a swf file. Provide names for graphics so the screen reader can interpret them. If a graphic or animation does not communicate vita...

  • Page 133

    Guidelines for accessibility in flash 133 ordering, tabbing, and the keyboard the reading order and tabbing are possibly the two most important considerations for making accessible flash websites. When you design an interface, the order that it appears on the page might not match the order in which ...

  • Page 134

    134 chapter 3: using best practices handling audio and animation many swf files contain audio, video, and narrations, because it is easy and robust to deliver this media using flash. When you provide audio narrations or video that contains speech, it is important to provide captions for those users ...

  • Page 135

    Guidelines for accessibility in flash 135 thoroughly test component instances in your applications with a screen reader, because some components have problems when you use them with screen readers. In particular, test how the screen reader reads components aloud when the component is in an opened an...

  • Page 136

    136 chapter 3: using best practices advertising with flash many opportunities exist for creating interactive and engaging advertisements using swf files. Macromedia recommends that you follow several guidelines when you produce flash advertisements, based on standards set up by the interactive adver...

  • Page 137

    Advertising with flash 137 use the get command to pass data between an advertisement and a server, and do not use the post command. For more information on get and post, see geturl() in flash actionscript language reference. Note: remember to provide control to the user. If you add sound to an adver...

  • Page 138

    138 chapter 3: using best practices and you would add the following code in your html: =http://adnetwork.Com/tracking?Http://www.Destinationurl.Com"> for more information on advanced tracking techniques, see the rich media advertising center at www.Macromedia.Com/devnet/rich_media_ads . To download ...

  • Page 139: Chapter 4

    139 chapter 4 writing and debugging scripts adding scripts to your flash applications enables rich functionality. In macromedia flash mx 2004 and macromedia flash mx professional 2004, you have two choices: • you can write scripts that are embedded in your flash document (fla file). To write embedde...

  • Page 140

    140 chapter 4: writing and debugging scripts controlling when actionscript runs when you write a script, you use the actions panel to attach the script to a frame on a timeline or to a button or movie clip on the stage. Scripts attached to a frame run, or execute, when the playhead enters that frame...

  • Page 141

    Using the actions panel and script window 141 to display the actions panel: do one of the following: • select window > development panels > actions. • press f9. (flash professional only) to display the script window: do one of the following: • to begin writing a new script, select file > new > actio...

  • Page 142

    142 chapter 4: writing and debugging scripts in the actions panel, the actions toolbox contains a script navigator, which is a visual representation of the locations in the fla file that have associated actionscript; you can navigate through your fla file to locate actionscript code. If you click an...

  • Page 143

    Using the actions panel and script window 143 managing scripts in a fla file if you don’t centralize your code within a fla file in one location, you can pin multiple scripts in the actions panel to make it easier to move among them. In the following figure, the script associated with the current lo...

  • Page 144

    144 chapter 4: writing and debugging scripts to use keyboard shortcuts with pinned scripts: • you can use the following keyboard shortcuts to work with pinned scripts: using the actionscript editor when you use the actionscript editor (in either the actions panel or, in flash mx professional 2004, t...

  • Page 145

    Using the actionscript editor 145 to set preferences for syntax coloring as you type, do one of the following: • select edit > preferences, and specify syntax coloring settings on the actionscript tab. • in the actions panel, select preferences from the options pop-up menu (at the upper right of the...

  • Page 146

    146 chapter 4: writing and debugging scripts then you can type either of the following (the variable name followed by a period): my_array. My_cam. You will see code hints for the array and camera object, respectively, appear. For objects that appear on the stage, use the suffix in the instance name ...

  • Page 147

    Using the actionscript editor 147 for information on using code hints when they appear, see “using code hints” on page 147 . Using comments to trigger code hints you can also use actionscript comments to specify an object’s class for code hinting. The following example tells actionscript that the cl...

  • Page 148

    148 chapter 4: writing and debugging scripts to work with tooltip-style code hints: 1. Display the code hint by typing an opening parens [ ( ] after an element that requires parentheses (such as a method name, a command such as if or do while , and so on). The code hint appears. Note: if a code hint...

  • Page 149

    Using the actionscript editor 149 note: if a code hint doesn’t appear, make sure you haven’t disabled code hints on the actionscript tab. If you want to display code hints for a variable or object you created, make sure that you have named your variable or object correctly (see “using suffixes to tr...

  • Page 150

    150 chapter 4: writing and debugging scripts if you want to learn (or be reminded) about which commands have escape shortcut keys, you can show them next to elements in the actions panel. To show or hide escape shortcut keys: • from the view options pop-up menu, enable or disable view escape shortcu...

  • Page 151

    Using the actionscript editor 151 note: if you check syntax in external actionscript 2.0 class files, the global class path will affect this process. Sometimes you will generate errors—even if the global class path is set correctly—because the compiler is not aware that this class is being compiled....

  • Page 152

    152 chapter 4: writing and debugging scripts to use automatic indentation: • automatic indentation is turned on by default. To turn it off, deselect automatic indentation in actionscript preferences. When automatic indentation is turned on, the text you type after ( or { is automatically indented ac...

  • Page 153

    Debugging your scripts 153 to select text encoding options for importing or exporting actionscript files: 1. In the preferences dialog box (edit > preferences), click the actionscript tab. 2. Under editing options, do one or both of the following: ■ for open/import, select utf-8 to open or import us...

  • Page 154

    154 chapter 4: writing and debugging scripts the debugger shows a hierarchical display list of movie clips currently loaded in flash player. Using the debugger, you can display and modify variable and property values as the swf file plays, and you can use breakpoints to stop the swf file and step th...

  • Page 155

    Debugging your scripts 155 debugging a swf file from a remote location you can debug a remote swf file using the stand-alone, activex, or plug-in versions of flash player. When exporting a swf file, you can enable debugging in your file and create a debugging password. If you don’t enable debugging,...

  • Page 156

    156 chapter 4: writing and debugging scripts to activate the debugger from a remote location: 1. Open the flash authoring application. 2. In a browser or in the debug version of the stand-alone player, open the published swf file from the remote location. The remote debug dialog box appears. If this...

  • Page 157

    Debugging your scripts 157 displaying and modifying variables the variables tab in the debugger shows the names and values of any global and timeline variables in the swf file selected in the display list. If you change the value of a variable on the variables tab, you can see the change reflected i...

  • Page 158

    158 chapter 4: writing and debugging scripts using the watch list to monitor a set of critical variables in a manageable way, you can mark variables to appear in the watch list. The watch list shows the absolute path to the variable and the value. You can also enter a new variable value in the watch...

  • Page 159

    Debugging your scripts 159 to display a movie clip’s properties in the debugger: 1. Select a movie clip from the display list. 2. Click the properties tab in the debugger. To modify a property value: • double-click the value, and enter a new value. The value cannot be an expression. For example, you...

  • Page 160

    160 chapter 4: writing and debugging scripts caution: if you set breakpoints in the actions panel and press the auto format button, you might notice that some breakpoints are no longer in the correct location. Actionscript might be moved to a different line when your code is formatted because someti...

  • Page 161

    Debugging your scripts 161 when you click the button, the breakpoint is reached and flash player pauses. You can now step in to bring the debugger to the first line of myfunction() wherever it is defined in the document. You can also step through or out of the function. As you step through lines of ...

  • Page 162

    162 chapter 4: writing and debugging scripts continue leaves the line at which the player is stopped and continues playing until a breakpoint is reached. Stop debugging makes the debugger inactive but continues to play the swf file in flash player. Using the output panel in test mode, the output pan...

  • Page 163

    Using the output panel 163 for more information on the output panel, see the following topics: • “listing a swf file’s objects” on page 163 • “listing a swf file’s variables” on page 164 • “displaying text field properties for debugging” on page 164 • “using the trace statement” on page 165 listing ...

  • Page 164

    164 chapter 4: writing and debugging scripts listing a swf file’s variables in test mode, the list variables command shows a list of all the variables currently in the swf file. This is especially useful for finding the correct variable target path and variable name. Unlike the debugger, the list do...

  • Page 165

    Updating flash player for testing 165 • color properties appear as hexadecimal numbers (0x00ff00). • the properties appear in the following order: variable , text , htmltext , html , textwidth , textheight , maxchars , bordercolor , backgroundcolor , textcolor , border , background , wordwrap , pass...

  • Page 166

    166 chapter 4: writing and debugging scripts.

  • Page 167: Chapter 5

    167 chapter 5 handling events an event is a software or hardware occurrence that requires a response from a macromedia flash application. For example, an event such as a mouse click or a keypress is called a user event because it occurs as a result of direct user interaction. An event that is genera...

  • Page 168

    168 chapter 5: handling events the following actionscript classes define event handlers: “button class”, “contextmenu class”, “contextmenuitem class”, “key class”, “loadvars class”, “localconnection class”, “mouse class”, “movieclip class”, “moviecliploader class”, “selection class”, “sharedobject c...

  • Page 169

    Using event listeners 169 for example, the following code inserts some text into a text field that no longer has keyboard focus: this.Createtextfield("my_txt", 99, 10, 10, 200, 20); my_txt.Border = true; my_txt.Type = "input"; this.Createtextfield("myother_txt", 100, 10, 50, 200, 20); myother_txt.Bo...

  • Page 170

    170 chapter 5: handling events the listener/broadcaster model for events, unlike event handler methods, lets you have multiple pieces of code listen to the same event without conflict. Event models that do not use the listener/ broadcaster model, such as xml.Onload , can be problematic when various ...

  • Page 171

    Using button and movie clip event handlers 171 to create a simple focus manager with event listeners: 1. Using the text tool, create a text field on the stage. 2. Select the text field and, in the property inspector, select input from the text type pop-up menu, and select the show border around text...

  • Page 172

    172 chapter 5: handling events note: do not confuse button and movie clip event handlers with component events, such as simplebutton.Click , uiobject.Hide, and uiobject.Reveal, which must be attached to component instances and are discussed in using components help. You can attach onclipevent() and ...

  • Page 173

    Creating movie clips with button states 173 using on() and onclipevent() with event handler methods you can, in some cases, use different techniques to handle events without conflict. Using on() and onclipevent() doesn’t conflict with using event handler methods that you define. For example, suppose...

  • Page 174

    174 chapter 5: handling events event handler scope the scope, or context, of variables and commands that you declare and execute within an event handler depends on the type of event handler you use: event handlers or event listeners, or on() and onclipevent() handlers. If you’re defining an event ha...

  • Page 175

    Event handler scope 175 when attached to a button object, the play() function applies to the timeline that contains the button—that is, the button’s parent timeline. But when the on(press) handler is attached to a movie clip object, the play() function call applies to the movie clip that bears the h...

  • Page 176

    176 chapter 5: handling events this code cannot work correctly because there is a problem involving scope with the event handlers, and what this refers to is confused between the onload event handler and the class. The behavior that you might expect in this example is that the onloadvarsdone() metho...

  • Page 177

    Scope of the this keyword 177 within an on() handler attached to a button , this refers to the timeline that contains the button, as shown in the following code: // attached to button on main timeline on(press) { trace(this); // displays '_level0' }.

  • Page 178

    178 chapter 5: handling events.

  • Page 179: Chapter 6

    179 chapter 6 creating interaction with actionscript in simple animations, macromedia flash player plays the scenes and frames of a swf file sequentially. In an interactive swf file, your audience uses the keyboard and mouse to jump to different parts of a swf file, move objects, enter information i...

  • Page 180

    180 chapter 6: creating interaction with actionscript controlling swf file playback the following actionscript functions let you control the playhead in the timeline and load a new web page into a browser window: • the gotoandplay() and gotoandstop() functions send the playhead to a frame or scene. ...

  • Page 181

    Controlling swf file playback 181 // plays the timeline that contains the button play(); } this same on() event handler code produces a different result when attached to a movie clip object rather than a button. When attached to a button object, statements made within an on() handler are applied to ...

  • Page 182

    182 chapter 6: creating interaction with actionscript the following code then loads adduser.Cfm into a blank browser window and passes to the cfm page variables_mc.Name and variables_mc.Age in the post header: variables_mc.Geturl("adduser.Cfm", "_blank", "post"); for more information, see geturl() i...

  • Page 183

    Creating interactivity and visual effects 183 the mouse.Hide() method hides the pointer when the movie clip first appears on the stage; the onmousemove function positions the custom pointer at the same place as the pointer and calls updateafterevent whenever the user moves the mouse. The updateafter...

  • Page 184

    184 chapter 6: creating interaction with actionscript the following procedure shows several ways to get the pointer position within the main timeline or within a movie clip. To get the current pointer position: 1. Create two dynamic text fields, and name them box1_txt and box2_txt. 2. Add labels for...

  • Page 185

    Creating interactivity and visual effects 185 to decide which keys to use and determine their virtual key codes, use one of the following approaches: • see the list of key codes in appendix c, “keyboard keys and key code values,” on page 313 . See the list of key codes in appendix c, “keyboard keys ...

  • Page 186

    186 chapter 6: creating interaction with actionscript the following procedure shows how to capture keypresses to move a movie clip up, down, left, or right on the stage, depending on which corresponding arrow key (up, down, left, or right) is pressed. The movie clip is confined to an arbitrary area ...

  • Page 187

    Creating interactivity and visual effects 187 display_txt.Text = "right"; } else if (key.Isdown(key.Up)) { this._y = math.Max(this._y-distance, 0); display_txt.Text = "up"; } else if (key.Isdown(key.Down)) { this._y = math.Min(this._y+distance, 400-this._height); display_txt.Text = "down"; } }; 8. S...

  • Page 188

    188 chapter 6: creating interaction with actionscript 5. To create a color object that targets the car_mc movie clip, add the following code to the actions panel: var mycolor_color:color = new color(car_mc); 6. To make the red button change the color of the carcolor movie clip to red, add the follow...

  • Page 189

    Creating interactivity and visual effects 189 creating sound controls you use the built-in sound class to control sounds in a swf file. To use the methods of the sound class, you must first create a sound object. Then you can use the attachsound() method to insert a sound from the library into a swf...

  • Page 190

    190 chapter 6: creating interaction with actionscript the following procedures show how to create sound controls similar to the ones shown in the figure. To attach a sound to a timeline: 1. Select file > import to import a sound. 2. Select the sound in the library, right-click (windows) or control-c...

  • Page 191

    Creating interactivity and visual effects 191 6. Enter the following event handlers: slider_mc.Knob_btn.Onpress = function() { startdrag(this._parent, false, this._parent.Left, this._parent.Top, this._parent.Right, this._parent.Bottom); }; slider_mc.Knob_btn.Onrelease = function() { stopdrag(); }; s...

  • Page 192

    192 chapter 6: creating interaction with actionscript detecting collisions the hittest() method of the movieclip class detects collisions in a swf file. It checks to see if an object has collided with a movie clip and returns a boolean value ( true or false ). There are two cases in which you would ...

  • Page 193

    Creating interactivity and visual effects 193 to detect a collision between a movie clip and a point on the stage: 1. Create a new movie clip on the stage, and enter box_mc as the instance name in the property inspector. 2. Select the first frame on layer 1 in the timeline. 3. Select window > develo...

  • Page 194

    194 chapter 6: creating interaction with actionscript creating a simple line drawing tool you can use methods of the movieclip class to draw lines and fills on the stage as the swf file plays. This lets you create drawing tools for users and draw shapes in the swf file in response to events. The dra...

  • Page 195

    Deconstructing a sample script 195 mouselistener.Onmouseup = function() { isdrawing = false; }; mouse.Addlistener(mouselistener); 5. Select control > test movie to test the movie. Click and drag your pointer to draw a line on the stage. Click the button to erase what you’ve drawn. Deconstructing a s...

  • Page 196

    196 chapter 6: creating interaction with actionscript if (zapped) { bug_mc._y += 25; } }; the bug’s instance name is bug_mc , and the outlet’s instance name is zapper_mc . In the script, the bug is referred to as this because the script is attached to the bug and the reserved word this refers to the...

  • Page 197: Chapter 7

    197 chapter 7 using the built-in classes in addition to the actionscript core language elements and constructs ( for and while loops, for example) and primitive data types (numbers, strings, and arrays) described earlier in this manual (see “actionscript basics” on page 23 ), actionscript also provi...

  • Page 198

    198 chapter 7: using the built-in classes for more information, see the following topics: • “creating a new object” on page 198 • “accessing object properties” on page 198 • “calling object methods” on page 198 • “about class (static) members” on page 199 creating a new object to create an instance ...

  • Page 199

    Overview of built-in classes 199 about class (static) members some built-in actionscript classes have class members (or static members).Class members (properties and methods) are accessed or invoked on the class name, not on an instance of the class. Therefore, you don’t create an instance of the cl...

  • Page 200

    200 chapter 7: using the built-in classes classes specific to flash player the following sections list the classes that are specific to flash player and the flash runtime model. For the purposes of understanding actionscript, these classes are typically grouped into four categories: • “media classes...

  • Page 201

    Overview of built-in classes 201 media classes the media classes provide playback control of sound and video in a swf file as well as access to the user’s microphone and camera, if they are installed. These classes are located in the built-in classes > media directory in the actions toolbox. Class d...

  • Page 202

    202 chapter 7: using the built-in classes movie classes the movie classes provide control over most visual elements in a swf file, including movie clips, text fields, and buttons. The movie classes are located in the actions toolbox in the built-in classes > movie directory. Class description access...

  • Page 203

    Overview of built-in classes 203 client-server classes the following table lists classes that let you send and receive data from external sources or communicate with application servers over ftp, http, or https. Note: in flash player 7, a swf file can load data only from exactly the same domain from...

  • Page 204

    204 chapter 7: using the built-in classes these classes are located in the built-in classes > client/server folder in the actions toolbox. Authoring classes the authoring classes are available only in the flash authoring environment. These classes are found in the built-in classes > authoring direct...

  • Page 205: Chapter 8

    205 chapter 8 working with movie clips movie clips are self-contained swf files that run independently of each other and the timeline that contains them. For example, if the main timeline has only one frame and a movie clip in that frame has ten frames, each frame in the movie clip plays when you pl...

  • Page 206

    206 chapter 8: working with movie clips when a function and a method offer similar behaviors, you can select to control movie clips by using either one. The choice depends on your preference and your familiarity with writing scripts in actionscript. Whether you use a function or a method, the target...

  • Page 207

    Loading and unloading additional swf files 207 the with statement takes a movie clip as a parameter. The object you specify is added to the end of the current target path. All actions nested inside a with statement are carried out inside the new target path, or scope. For example, in the following s...

  • Page 208

    208 chapter 8: working with movie clips • develop a branching interface with links that lets the user select among several swf files that are used to display a site’s content. • build a navigation interface with navigation controls in level 0 that loads content into other levels. Loading content int...

  • Page 209

    Changing movie clip position and appearance 209 this step ensures that any references to _root in contents.Swf—or any swf file loaded into target_mc —will refer to its own timeline, not the actual root timeline of container.Swf. Now when you click the button, " tim " appears. Equivalently, the autho...

  • Page 210

    210 chapter 8: working with movie clips in addition, you can write statements that get the value of a movie clip property. For example, the following statement gets the value of the _xmouse property on the current level’s timeline and sets the _x property of the my_mc instance to that value: this.On...

  • Page 211

    Creating movie clips at runtime 211 creating movie clips at runtime not only can you create movie clip instances in the flash authoring environment, you can also create movie clip instances at runtime in the following ways: • “creating an empty movie clip” on page 211 • “duplicating or removing a mo...

  • Page 212

    212 chapter 8: working with movie clips for more information, see movieclip.Createemptymovieclip() in flash actionscript language reference. Duplicating or removing a movie clip to duplicate or remove movie clip instances, use the duplicatemovieclip() or removemovieclip() global functions, or the mo...

  • Page 213

    Adding parameters to dynamically created movie clips 213 7. If you don’t want the movie clip to load before the first frame, deselect the export in first frame option. If you deselect this option, place an instance of the movie clip on the frame of the timeline where you want it to be available. For...

  • Page 214

    214 chapter 8: working with movie clips to populate a dynamically created movie clip with parameters from a specified object: do one of the following: • use the following syntax with attachmovie() : mymovieclip.Attachmovie(idname, newname, depth [, initobject]) • use the following syntax with duplic...

  • Page 215

    Managing movie clip depths 215 managing movie clip depths every movie clip has its own z-order space that determines how objects overlap within its parent swf file or movie clip. Every movie clip has an associated depth value, which determines if it will render in front of or behind other movie clip...

  • Page 216

    216 chapter 8: working with movie clips in this case, the variable named nextdepth contains the value 11 because that’s the next highest available depth for the movie clip edit_mc . To obtain the current highest occupied depth, subtract 1 from the value returned by getnexthighestdepth() , as shown i...

  • Page 217

    Using movie clips as masks 217 you can use movie clips with drawing methods as masks; however, as with all movie clip masks, strokes are ignored. To draw a shape: 1. Use movieclip.Createemptymovieclip() to create an empty movie clip on the stage. The new movie clip is a child of an existing movie cl...

  • Page 218

    218 chapter 8: working with movie clips about masking device fonts you can use a movie clip to mask text that is set in a device font. In order for a movie clip mask on a device font to work properly, the user must have flash player 6 (6.0.40.0) or later. When you use a movie clip to mask text set i...

  • Page 219

    Initializing class properties 219 } } 4. Save the document as moveright.As in the balltest directory. To assign the class to a movie clip symbol: 1. In flash, select file > new, select flash document from the list of file types, and click ok. 2. Using the oval tool, draw a circle on the stage. 3. Se...

  • Page 220

    220 chapter 8: working with movie clips assuming this class is assigned to a symbol with a linkage identifier of ball, the following code creates two new instances of the symbol on the root timeline of the swf file. The first instance, named ball50_mc , moves 50 pixels each time it is clicked; the s...

  • Page 221: Chapter 9

    221 chapter 9 working with text a dynamic or input text field is a textfield object (an instance of the textfield class). When you create a text field, you can assign it an instance name in the property inspector. You can use the instance name in actionscript statements to set, change, and format th...

  • Page 222

    222 chapter 9: working with text to control a dynamic or input text field using actionscript, you must assign it an instance name in the property inspector. You can then reference the text field with the instance name, and use the methods and properties of the textfield class to control the contents...

  • Page 223

    Creating text fields at runtime 223 however, you can’t use the variable name mytextvar to set the text field’s text property. You have to use the instance name, as shown in the following code: //this won’t work mytextvar.Text = "a text field variable is not an object reference"; //for input text fie...

  • Page 224

    224 chapter 9: working with text using the textformat class you can use the actionscript textformat class to set formatting properties of a text field. The textformat class incorporates character and paragraph formatting information. Character formatting information describes the appearance of indiv...

  • Page 225

    Using the textformat class 225 5. Apply the textformat object to the text field you created in step 1 using textfield.Settextformat() : mytext_txt.Settextformat(txtfmt); this version of settextformat() applies the specified formatting to the entire text field. There are two other versions of this me...

  • Page 226

    226 chapter 9: working with text getting text metric information you can use the textformat.Gettextextent() method to obtain detailed text measurements for a text string that has specific formatting. For example, suppose you need to create, at runtime, a new textfield object containing an arbitrary ...

  • Page 227

    Formatting text with cascading style sheets 227 • “using style classes” on page 231 • “styling built-in html tags” on page 231 • “an example of using styles with html” on page 232 • “using styles to define new tags” on page 234 • “an example of using styles with xml” on page 235 supported css proper...

  • Page 228

    228 chapter 9: working with text creating a style sheet object css are represented in actionscript by the textfield.Stylesheet class. This class is available only for swf files that target flash player 7 or later. To create a style sheet object, call the textfield.Stylesheet class’s constructor func...

  • Page 229

    Formatting text with cascading style sheets 229 7. Add the following code to the actions panel: var styles = new textfield.Stylesheet(); styles.Load("styles.Css"); styles.Onload = function(ok) { if (ok) { // display style names trace(this.Getstylenames()); } else { trace("error loading css file."); ...

  • Page 230

    230 chapter 9: working with text applying styles to a textfield object to apply a style sheet object to a textfield object, you assign the style sheet object to the text field’s stylesheet property. Textobj_txt.Stylesheet = stylesheetobj; note: do not confuse the textfield.Stylesheet property with t...

  • Page 231

    Formatting text with cascading style sheets 231 mytextarea.Html = true; var myvars:loadvars = new loadvars(); //var styles = new textfield.Stylesheet(); // load text to display and define onload handler myvars.Load("mytext.Htm"); myvars.Ondata = function(content) { _root.Mytextarea.Text = content; }...

  • Page 232

    232 chapter 9: working with text p { font-family: arial,helvetica,sans-serif; font-size: 12px; display: inline; } the following table shows which built-in html tags can be styled and how each style is applied: an example of using styles with html this section presents an example of using styles with...

  • Page 233

    Formatting text with cascading style sheets 233 font-family: arial,helvetica,sans-serif; font-size: 18px; font-weight: bold; display: block; } .Byline { color: #666600; font-style: italic; font-weight: bold; display: inline; } this style sheet defines styles for two built-in html tags ( and ) that w...

  • Page 234

    234 chapter 9: working with text styles.Load(css_url); styles.Onload = function(ok) { if (ok) { // if the style sheet loaded without error, // then assign it to the text object, // and assign the html text to the text field. News_txt.Stylesheet = styles; news_txt.Text = storytext; } }; note: in this...

  • Page 235

    Formatting text with cascading style sheets 235 an example of using styles with xml in this section, you can create a fla file that has xml-formatted text. You’ll create a style sheet using actionscript, rather than importing styles from a css file as shown in “an example of using styles with html” ...

  • Page 236

    236 chapter 9: working with text 7. To create the xml text to assign to the text field, open a text editor and enter the following text into a new document: flash player now supports csssan francisco, ca--macromedia inc. Announced today a new version of flash player that supports cascading style she...

  • Page 237

    Using html-formatted text 237 for more information on html-formatted text, see the following topics: • “overview of using html-formatted text” on page 237 • “supported html tags” on page 237 • “embedding images, swf files, and movie clips in text fields” on page 242 overview of using html-formatted ...

  • Page 238

    238 chapter 9: working with text for example, the following html code creates the link “go home,” which opens www.Macromedia.Com in a new browser window: urltext_txt.Htmltext = “go home”; you can use the special asfunction protocol to cause the link to execute an actionscript function in a swf file ...

  • Page 239

    Using html-formatted text 239 • size specifies the size of the font, in pixels, as shown in the following example: mytext_txt.Htmltext = "this is blue, 24- point text"; you can also use relative point sizes instead of a pixel size, such as +2 or -4. Image tag () the tag lets you embed external jpeg ...

  • Page 240

    240 chapter 9: working with text italic tag () the tag displays the tagged text in italics, as shown in the following code: that is very interesting. This code example would render as follows: that is very interesting. An italic typeface must be available for the font used. List item tag ( the tag p...

  • Page 241

    Using html-formatted text 241 span tag () the tag is available only for use with css text styles. (for more information, see “formatting text with cascading style sheets” on page 226 .) it supports the following attribute: • class specifies a css style class defined by an textfield.Stylesheet object...

  • Page 242

    242 chapter 9: working with text 4. Open the actions panel (window > development panels > actions), and enter the following code in the actions panel: //creates column headers, formatted in bold, separated by tabs var rowheaders = "name\tage\tdepartment"; //creates rows with data var row_1 = "tim\t3...

  • Page 243

    Using html-formatted text 243 embedding movie clip symbols to embed a movie clip symbol in a text field, you specify the symbol’s linkage identifier for the tag’s src attribute. (for information on defining a linkage identifier, see “attaching a movie clip symbol to the stage” on page 212 .) for exa...

  • Page 244

    244 chapter 9: working with text making hypertext links out of embedded media to make a hypertext link out of an embedded jpeg file, swf file, or movie clip, enclose the tag in an tag: textfield.Htmltext = "click the image to return home src='home.Jpg'>"; src='home.Jpg'> when the mouse pointer is ov...

  • Page 245

    Creating scrolling text 245 2. Create an up button and a down button, or select window > other panels > common libraries > buttons, and drag buttons to the stage. You will use these buttons to scroll the text up and down. 3. Select the down button on the stage and type down_btn into the instance nam...

  • Page 246

    246 chapter 9: working with text.

  • Page 247: Chapter 10

    247 chapter 10 creating custom classes with actionscript 2.0 actionscript 2.0 is a restructuring of the actionscript language that provides several powerful new programming features found in other programming languages, such as java. Actionscript 2.0 encourages program structures that are reusable, ...

  • Page 248

    248 chapter 10: creating custom classes with actionscript 2.0 this section contains code examples that you can use to become familiar with creating classes in actionscript 2.0. If you’re not familiar with actionscript 2.0 scripting, see chapter 2, “actionscript basics,” on page 23 and chapter 3, “us...

  • Page 249

    Principles of object-oriented programming 249 inheritance one of the primary benefits of object-oriented programming is that you can create subclasses of a class; the subclass then inherits all the properties and methods of the superclass. The subclass typically defines additional methods and proper...

  • Page 250

    250 chapter 10: creating custom classes with actionscript 2.0 encapsulation is why actionscript 2.0 includes, for example, member access control, so details of the implementation can be made private and invisible to code outside an object. The code outside the object is forced to interact with the o...

  • Page 251

    Using classes: a simple example 251 creating a class file to create a class, you must first create an external actionscript (as) file. Classes (and interfaces) can be defined only in external script files. You can’t define a class in a script attached to a frame or button in a flash document (fla fi...

  • Page 252

    252 chapter 10: creating custom classes with actionscript 2.0 6. To create the properties for the person class, use the var keyword to define two variables named age and name , as shown in the following example: class person { var age:number; var name:string; } tip: by convention, class properties a...

  • Page 253

    Using classes: a simple example 253 // constructor function function person (myname:string, myage:number) { this.Name = myname; this.Age = myage; } // method to return property values function getinfo():string { return("hello, my name is " + this.Name + " and i’m " + this.Age + " years old."); } } t...

  • Page 254

    254 chapter 10: creating custom classes with actionscript 2.0 to create an instance of the person class in a flash document: 1. In flash, select file > new, select flash document from the list of document types, and click ok. 2. Save the file as createperson.Fla in the personfiles directory you crea...

  • Page 255

    Creating and using classes 255 all as class files that you create must be saved in one of the designated classpath directories— directories where flash looks for class definitions when compiling scripts—that is, in the same directory where the fla file that refers to the class is stored. (see “under...

  • Page 256

    256 chapter 10: creating custom classes with actionscript 2.0 the this keyword is not required in actionscript 2.0 class definitions because the compiler resolves the reference and adds it into the bytecode. However, using this can improve your code’s readability. See “using the this keyword” on pag...

  • Page 257

    Creating and using classes 257 } } private members (properties and methods) are accessible only to the class that defines those members and to subclasses of that original class. Instances of the original class, or instances of subclasses of that class, cannot access privately declared properties and...

  • Page 258

    258 chapter 10: creating custom classes with actionscript 2.0 function myfunc():string { return "hello world"; } } this rule applies only to instance variables (variables that are copied into each instance of a class), not class variables (variables that belong to the class). For more information ab...

  • Page 259

    Creating dynamic classes 259 if you don’t place a call to super() in the constructor function of a subclass, the compiler automatically generates a call to the constructor of its immediate superclass with no parameters as the first statement of the function. If the superclass doesn’t have a construc...

  • Page 260

    260 chapter 10: creating custom classes with actionscript 2.0 now, instances of the person class can add and access properties and methods that aren’t defined in the original class, as shown in the following example: var a_person:person2 = new person2(); a_person.Haircolor = "blue";//no compiler err...

  • Page 261

    Creating and using interfaces 261 // in shapes/circle.As: class shapes.Circle {} // in shapes/triangle.As: class shapes.Triangle {} to reference a class that resides in a package directory, you can either specify its fully qualified class name or import the package by using the import statement (see...

  • Page 262

    262 chapter 10: creating custom classes with actionscript 2.0 for example, the following code declares an interface named myinterface that contains two methods, method_1() and method_2() . The first method, method_1() , has no parameters and specifies a return type of void (meaning it does not retur...

  • Page 263

    Instance and class members 263 in another script, such as the following code, where you create an instance of the box class, you could declare a variable to be of the movable type: import box; var newbox:movable = new box(); at runtime, in flash player 7 and later, you can cast an expression to an i...

  • Page 264

    264 chapter 10: creating custom classes with actionscript 2.0 another type of member is a class member. There is only one copy of a class member, which is used for the entire class. The age property would not be a good class member, because each person has a different age. Only properties and method...

  • Page 265

    Instance and class members 265 often there are situations when you need exactly one object of a particular type in a system. For example, in a chess game, there is only one chessboard, and in a country, there is only one capitol city. Even though there is only one object, it is attractive to encapsu...

  • Page 266

    266 chapter 10: creating custom classes with actionscript 2.0 the widgetcount variable is declared as static, so it initializes to 0 only once. Each time the widget class’s constructor function is called, it adds 1 to widgetcount and then shows the number of the current instance that’s being created...

  • Page 267

    Implicit getter/setter methods 267 implicit getter/setter methods object-oriented programming practice discourages direct access to properties within a class. Classes typically define getter methods that provide read access and setter methods that provide write access to a given property. For exampl...

  • Page 268

    268 chapter 10: creating custom classes with actionscript 2.0 return this.Username; } function set user(name:string):void { this.Username = name; } } a getter method must not take any parameters. A setter method must take exactly one required parameter. A setter method can have the same name as a ge...

  • Page 269

    Understanding the classpath 269 by default, the global classpath contains one absolute path and one relative path. The absolute path is denoted by $(localdata)/classes in the preferences dialog box. The location of the absolute path is shown here: • (windows 2000 or windows xp) \documents and settin...

  • Page 270

    270 chapter 10: creating custom classes with actionscript 2.0 in flash professional, when you check syntax while editing an as file, the compiler looks only in the global classpath; as files aren't associated with flas in edit mode and don't have their own classpath. Modifying the classpath you can ...

  • Page 271

    Importing classes 271 alternatively, double-click the path in the classpath list, type the desired path, and click ok. ■ to delete a directory from the classpath, select the path in the classpath list and click the remove selected path (-) button. Importing classes to reference a class in another sc...

  • Page 272

    272 chapter 10: creating custom classes with actionscript 2.0 the following example shows that you can then reference either of the classes directly in the same script: var myros:rosencrantz = new rosencrantz(); var myguil:guildenstern = new guildenstern(); the import statement applies only to the c...

  • Page 273

    Excluding classes 273 if you experience problems in which flash appears to be compiling older versions of a file you have edited, delete the aso files and then recompile. If you plan to delete aso files, delete them when flash is not performing other operations, such as checking syntax or exporting ...

  • Page 274

    274 chapter 10: creating custom classes with actionscript 2.0

  • Page 275: Chapter 11

    275 chapter 11 working with external data in macromedia flash mx 2004 and macromedia flash mx professional 2004, you can use actionscript to load data from external sources into a swf file. You can also send data from a swf file for processing by an application server (such as macromedia coldfusion ...

  • Page 276

    276 chapter 11: working with external data • the functions and movieclip methods that use the http or https protocol to send information in url-encoded format are geturl() , loadvariables() , loadvariablesnum() , loadmovie() , and loadmovienum() . • the loadvars methods that use the http or https pr...

  • Page 277

    Sending and loading variables to and from a remote source 277 for example, the following actionscript loads the variables from the file mydata.Txt into the movie clip loadtarget_mc . An ondata() handler assigned to the loadtarget_mc instance uses the variable lastsitevisited , which is loaded from t...

  • Page 278

    278 chapter 11: working with external data for example, if you want to track the high scores for a game, you could store the scores on a server and use loadvariables() to load them into the swf file each time someone played the game. The function call might look like the following example: loadvaria...

  • Page 279

    Sending and loading variables to and from a remote source 279 • the callback interface is through actionscript methods ( onload ) instead of the obsolete, deprecated onclipevent (data) approach required for loadvariables. • there are error notifications. • you can add custom http request headers. Yo...

  • Page 280

    280 chapter 11: working with external data this actionscript creates a new loadvars object instance, copies the values from the text fields into the instance, and then sends the data to the server. The cfm file sends the e-mail and returns a variable ( true or false ) to the swf file called result, ...

  • Page 281

    Sending and loading variables to and from a remote source 281 the following xml class methods send and load xml data to a server by using the http post method: • the load() method downloads xml from a url and places it in an actionscript xml object. • the send() method encodes the xml object into an...

  • Page 282

    282 chapter 11: working with external data //(optional) create two text fields for status/debugging //status_txt.Text = this.Firstchild.Attributes.Status; //debug_txt.Text = this.Firstchild; switch (this.Firstchild.Attributes.Status) { case 'ok' : _global.Session = this.Firstchild.Attributes.Session...

  • Page 283

    Sending and loading variables to and from a remote source 283 you can test this code by using a user name of jeansmith and the password verysecret . The first section of the script generates the following xml when the user clicks the login button: the server receives the xml, generates an xml respon...

  • Page 284

    284 chapter 11: working with external data using the xmlsocket class actionscript provides a built-in xmlsocket class, which lets you open a continuous connection with a server. A socket connection lets the server publish, or push, information to the client as soon as that information is available. ...

  • Page 285

    Sending messages to and from flash player 285 thesocket.Send(myxml); } //button sends data sendbutton.Onrelease = function() { senddata(); }; //traces data returned from socket connection thesocket.Ondata = function(msg:string):void { trace(msg); }; for more information, see the “xmlsocket class” en...

  • Page 286

    286 chapter 11: working with external data the following table shows the values you can specify for the command and arguments parameters of fscommand() to control the playback and appearance of a swf file playing in the stand-alone player, including projectors. To use fscommand() to send a message t...

  • Page 287

    Sending messages to and from flash player 287 7. In an html or text editor, open the html file that was generated in step 6 and examine the code. When you published your swf file using the flash with fscommand template on the html tab of the publish settings dialog box, some additional code was inse...

  • Page 288

    288 chapter 11: working with external data there are two technologies that enable communication between the browser and flash player: liveconnect (netscape navigator 3.0 or later on windows 95/98/2000/nt/xp or power macintosh) and activex (internet explorer 3.0 and later on windows 95/98/2000/nt/xp)...

  • Page 289

    Flash player security features 289 for more information, see the following topics: • “about allowing data access between cross-domain swf files” on page 289 • “about allowing http to https protocol access between swf files” on page 290 • “about allowing cross-domain data loading” on page 290 • “abou...

  • Page 290

    290 chapter 11: working with external data now the getdata function in the loaded swf file can be called by the macromedia.Swf file. Notice that allowdomain permits any swf file in the allowed domain to script any other swf file in the domain permitting the access, unless the swf file being accessed...

  • Page 291

    Flash player security features 291 for example, a policy file located at https://www.Macromedia.Com:8080/crossdomain.Xml will apply only to data loading calls made to www.Macromedia.Com over https at port 8080. An exception to this rule is the use of an xmlsocket object to connect to a socket server...

  • Page 292

    292 chapter 11: working with external data a policy file that contains no tags has the same effect as not having a policy on a server. About custom policy file locations flash player 7 (7.0.19.0) supports a new method called system.Security.Loadpolicyfile . This method lets you specify a custom loca...

  • Page 293

    Flash player security features 293 about xmlsocket policy files for an xmlsocket connection attempt, flash player 7 (7.0.14.0) looked for crossdomain.Xml on an http server on port 80 in the subdomain to which the connection attempt was being made. Flash player 7 (7.0.14.0) and all earlier versions r...

  • Page 294

    294 chapter 11: working with external data because the ability to connect to ports lower than 1024 is new in flash player 7 (7.0.19.0), a policy file loaded with loadpolicyfile is always required to authorize this, even when a swf file is connecting to its own subdomain. About compatibility with pre...

  • Page 295: Chapter 12

    295 chapter 12 working with external media if you import an image or a sound while you author a document in macromedia flash mx 2004 or macromedia flash mx professional 2004, the image and sound are packaged and stored in the swf file when you publish it. In addition to importing media while authori...

  • Page 296

    296 chapter 12: working with external media to play back an external mpeg layer 3 (mp3) file, use the loadsound() method of the sound class. This method lets you specify whether the mp3 file should progressively download or finish downloading completely before it starts to play. You can also read th...

  • Page 297

    Loading external mp3 files 297 the following code loads the jpeg image flowers.Jpg into the movie clip instance image_clip : image_clip.Loadmovie("flowers.Jpg"); for more information about these methods, see loadmovie() , loadmovienum() , and movieclip.Loadmovie() in flash actionscript language refe...

  • Page 298

    298 chapter 12: working with external media in most cases, set the isstreaming parameter to true , especially if you’re loading large sound files that should start playing as soon as possible—for example, when creating an mp3 “jukebox” application. However, if you’re downloading shorter sound clips ...

  • Page 299

    Playing back external flv files dynamically 299 the onid3 event handler is called each time new id3 data is available. This means that if an mp3 file contains id3 2.0 tags and id3 1.0 tags, the onid3 handler will be called twice because the tags are located in different parts of the file. For a list...

  • Page 300

    300 chapter 12: working with external media to play back an external flv file in a flash document: 1. With the document open in the flash authoring tool, in the library panel (window > library) select new video from the library options menu to create a video object. 2. Drag a video object from the l...

  • Page 301

    Preloading external media 301 preloading swf and jpeg files to preload swf and jpeg files into movie clip instances, you can use the “moviecliploader class”. This class provides an event listener mechanism to give notification about the status of file downloads into movie clips. Using a moviecliploa...

  • Page 302

    302 chapter 12: working with external media to display download progress using the progressbar component: 1. In a new flash document, create a movie clip on the stage and give it an instance name target_mc . 2. Open the components panel (window > development panels > components). 3. Drag a progressb...

  • Page 303

    Preloading external media 303 trace("%" + percentloaded + " loaded."); }; // when the file has finished loading, clear the interval polling. Songtrack.Onload = function () { clearinterval(poll); }; // load streaming mp3 file and start calling checkprogress() songtrack.Loadsound("http://yourserver.Co...

  • Page 304

    304 chapter 12: working with external media.

  • Page 305: Appendix A

    305 appendix a error messages macromedia flash mx 2004 and macromedia flash mx professional 2004 provide enhanced compile-time error reporting when you publish to actionscript 2.0 (the default). The following table contains a list of error messages that the flash compiler can generate: error number ...

  • Page 306

    306 appendix a: error messages 1114 there is no interface defined with this name. 1115 a class may not extend an interface. 1116 an interface may not extend a class. 1117 an interface name is expected after the ‘implements’ keyword. 1118 a class may not implement a class, only interfaces. 1119 the c...

  • Page 307

    307 1144 instance variables cannot be accessed in static functions. 1145 class and interface definitions cannot be nested. 1146 the property being referenced does not have the static attribute. 1147 this call to super does not match the superconstructor. 1148 only the public attribute is allowed for...

  • Page 308

    308 appendix a: error messages 1174 the class or interface name you have entered contains a syntax error. 1175 ‘variable’ is not accessible from this scope. 1176 multiple occurrences of the ‘get/set/private/public/static’ attribute were found. 1177 a class attribute was used incorrectly. 1178 instan...

  • Page 309

    309 1198 the member function ‘’ has a different case from the name of the class being defined, ‘’, and will not be treated as the class constructor at runtime. 1199 the only type allowed for a for-in loop iterator is string. 1200 a setter function may not return a value. 1201 the only attributes all...

  • Page 310

    310 appendix a: error messages.

  • Page 311: Appendix B

    311 appendix b deprecated flash 4 operators the following table lists flash 4-only operators, which are deprecated in actionscript 2.0. Do not use these operators unless you are publishing to flash player 4 and earlier. Operator description associativity not logical not right to left and logical and...

  • Page 312

    312 appendix b: deprecated flash 4 operators.

  • Page 313: Appendix C

    313 appendix c keyboard keys and key code values the following tables list all the keys on a standard keyboard and the corresponding ascii key code values that are used to identify the keys in actionscript: • “letters a to z and standard numbers 0 to 9” • “keys on the numeric keypad” on page 314 • “...

  • Page 314

    314 appendix c: keyboard keys and key code values keys on the numeric keypad the following table lists the keys on a numeric keypad, with the corresponding ascii key code values that are used to identify the keys in actionscript: n 78 o 79 p 80 q 81 r 82 s 83 t 84 u 85 v 86 w 87 x 88 y 89 z 90 0 48 ...

  • Page 315

    Function keys 315 function keys the following table lists the function keys on a standard keyboard, with the corresponding ascii key code values that are used to identify the keys in actionscript: numbpad 5 101 numbpad 6 102 numbpad 7 103 numbpad 8 104 numbpad 9 105 multiply 106 add 107 enter 13 sub...

  • Page 316

    316 appendix c: keyboard keys and key code values other keys the following table lists keys on a standard keyboard other than letters, numbers, numeric keypad keys, or function keys, with the corresponding ascii key code values that are used to identify the keys in actionscript: key key code backspa...

  • Page 317

    Other keys 317 ] } 221 " ' 222 key key code.

  • Page 318

    318 appendix c: keyboard keys and key code values.

  • Page 319: Appendix D

    319 appendix d writing scripts for earlier versions of flash player actionscript has changed considerably with the release of macromedia flash mx 2004 and macromedia flash mx professional 2004. When you create content for macromedia flash player 7, you can use the full power of actionscript. You can...

  • Page 320

    320 appendix d: writing scripts for earlier versions of flash player using flash mx 2004 to create content for flash player 4 to use flash mx 2004 to create content for flash player 4, specify flash player 4 on the flash tab of the publish settings dialog box (file > publish settings). Flash player ...

  • Page 321

    Using flash mx 2004 to create content for flash player 4 321 in flash 4 actionscript, these operators were always numeric operators. In flash 5 and later, they behave differently, depending on the data types of the operands. To prevent any semantic differences in imported files, the number() functio...

  • Page 322

    322 appendix d: writing scripts for earlier versions of flash player.

  • Page 323: Appendix E

    323 appendix e object-oriented programming with actionscript 1 the information in this appendix comes from the macromedia flash mx documentation and provides information on using the actionscript 1 object model to write scripts. It is included here for the following reasons: • if you want to write o...

  • Page 324

    324 appendix e: object-oriented programming with actionscript 1 objects in actionscript can be pure containers for data, or they can be graphically represented on the stage as movie clips, buttons, or text fields. All movie clips are instances of the built-in movieclip class, and all buttons are ins...

  • Page 325

    About actionscript 1 325 creating a custom object in actionscript 1 to create a custom object, you define a constructor function. A constructor function is always given the same name as the type of object it creates. You can use the keyword this inside the body of the constructor function to refer t...

  • Page 326

    326 appendix e: object-oriented programming with actionscript 1 to assign a method to a custom object: 1. Define the constructor function circle() : function circle(radius) { this.Radius = radius; } 2. Define the getarea() method of the circle object. The getarea() method calculates the area of the ...

  • Page 327

    About actionscript 1 327 this new class is assigned to all instances of the movie clip that are added to the application by the timeline or that are added to the application with the attachmovie() or duplicatemovieclip() method. If you want these movie clips to have access to the methods and propert...

  • Page 328

    328 appendix e: object-oriented programming with actionscript 1 myclipclass.Prototype.Myfunction = function(){ trace("myfunction called"); } object.Registerclass("myclipid",myclipclass); this.Attachmovie("myclipid","clipname",3); creating inheritance in actionscript 1 inheritance is a means of organ...

  • Page 329

    About actionscript 1 329 instead of adding roll() to the mountainbike class and the tricycle class, you can create the mountainbike class with bike as its superclass, as shown in the following example: mountainbike.Prototype = new bike(); now you can call the roll() method of mountainbike, as shown ...

  • Page 330

    330 appendix e: object-oriented programming with actionscript 1 using function object properties in actionscript 1 you can specify the object to which a function is applied and the parameter values that are passed to the function, using the call() and apply() methods of the function object. Every fu...

  • Page 331

    About actionscript 1 331 for more information, see function.Apply() in flash actionscript language reference. To specify the object to which a function is applied using function.Apply(): • use the following syntax: myfunction.Apply(thisobject, argumentsobject) the method takes the following paramete...

  • Page 332

    332 appendix e: object-oriented programming with actionscript 1.

  • Page 333: Index

    333 index a accessing object properties 56 actions coding standards 83 repeating 59 actions panel 140 actions toolbox 141 yellow items in 144 actionscript comparing versions 84 editor 141, 144 flash player 85 interfaces 263 strict data typing not supported in actionscript 1 42 writing 85 actionscrip...

  • Page 334

    334 index c calling methods 37 capturing keypresses 184 cascading style sheets and textfield.Stylesheet class 228 applying style classes 231 applying to text fields 230 assigning styles to built-in html tags 231 combining styles 231 defining styles in actionscript 229 example of using with html tags...

  • Page 335

    Index 335 reserved words 75 variable names 70 coding standards 82 organizing scripts 82 collisions, detecting 192 between movie clip and stage point 193 between movie clips 193 colors in actions toolbox 144 values, setting 187 combining operations 55 comments 31 in classes 102 in code 77 communicati...

  • Page 336

    336 index dot operators 56 dot syntax 29 dragging movie clips 210 drawing lines and fills 194 shapes 216 duplicating, movie clips 212 dynamic classes 259 e ecma-262 compliance 14 specification 24 encapsulation 249 encoding text 152 equality operators 54 different from assignment operators 54 strict ...

  • Page 337

    Index 337 best practices 98 built-in 60 calling 63 coding conventions 72 constructor 324 conversion 35 custom 61 defining 61 for controlling movie clips 206 local variables in 62 passing parameters to 62 returning values 62 sample 27 g garbage collection 89 getascii() method 184 getter/setter method...

  • Page 338

    338 index k key codes, ascii function keys 315 getting 184 letter and number keys 313 numeric keypad 314 other keys 316 keyboard ascii key code values 313 shortcuts for pinned scripts 143 keyboard controls and test movie 153 to activate movie clips 186 keypresses, capturing 184 keywords 26 listed 32...

  • Page 339

    Index 339 loading swf files and jpeg files into 296 looping through children 60 managing depth 215 methods 206 methods and functions compared 205 methods, using to draw shapes 216 nested, defined 205 parent, defined 205 properties 209 properties, initializing at runtime 219 removing 212 sharing 212 ...

  • Page 340

    340 index p packages 260 coding conventions 73 naming 260 parameters defined 27 in parentheses 31 passing to functions 62 parent movie clips, defined 205 parentheses 31 checking for matching pairs 151 passing values by content 47 by reference 48 passwords and remote debugging 155 pausing (stepping t...

  • Page 341

    Index 341 scrolling text 244 security 288–294 and policy files 290 and porting scripts to flash player 7 15, 18, 19 data access across domains 289, 290 loadpolicyfile 292, 293 semicolon 31 sending information in xml format 276 to remote files 275 url-encoded format 276 via tcp/ip 276 server-side scr...

  • Page 342

    342 index test movie and keyboard controls 153 and unicode 153 testing. See debugging text assigning to text field at runtime 222 determining required size of textfield object 226 encoding 153 getting metric information 226 scrolling 244 using tag to flow around images 239 see also text fields text ...

  • Page 343

    Index 343 writing syntax and statements 89 compound 91 conditional 90 do-while 92 for statement 92 listener 94 return 93 switch 93 try-catch and try-catch-finally 93 x xml 280 dom 280 example of using with styles 235 hierarchy 280 in server-side scripts 281 sample variable conversion 280 sending inf...

  • Page 344

    344 index.