ptg 366 Chapter 15 ActionScript 3.0 has undergone significant changes. This does not mean that if you have worked with Flash ActionScript 1.0 or 2.0 that you will be completely lost, but it will require that you learn new ways to use your code. Understanding ActionScript 3.0 ActionScript 3.0 is a robust programming model (completely up-to-date with the latest release of the ECMAScript standard) familiar to developers with a basic knowledge of object-oriented programming. If you have developed for other programming languages, such as Java, C++ or C#, then you have heard of this term. Object-oriented programming allows you break your code into objects that can be easily re-used. With object-oriented programming, it’s easier to manage an appli- cation that has a collection of smaller files than one large file. There are number of changes that will take a while to get used. The main changes are: ◆ ActionScript is located in Class files or in the Timeline. ◆ Code design is truly object oriented. ◆ You cannot add ActionScript directly to movie clips or buttons instances. ◆ Triggering events has changed. ◆ Loading data has changed. ◆ XML is managed differently. Some other enhancements include more run-time exceptions for common error condi- tions, added use of run-time type information, improved use of properties and classes with sealed classes, added use of method closures for event handling, added use of industry standard ECMAScript for XML, more support for regular expressions, and added primitive types (int and uint). There are a score of smaller changes. As you dive directly into ActionScript 3.0 you will find a learning curve as you move from ActionScript 2.0. Is it all worthwhile to go through the pain? Absolutely. ActionScript 3.0 is simply so much faster and more powerful that it is worth the undertaking. Working with ActionScript 3.0 There are two ways in which you can work with ActionScript. The first method is to add ActionScript to the Timeline. This will be famil- iar to earlier Flash developers. A good prac- tice to get into is to have a layer in that Timeline that is dedicated to just working with ActionScript. Adobe recommends that you label the ActionScript label “Actions.” Locking the “Actions” layer will prevent you from acci- dentally adding movie clips into it. The second method is to insert ActionScript into Flash files with a Class file. Class files have a long history in the develop- ment world. With a Class file you can now specify both Public and Private Classes. Using the Private keyword restricts the use of the attribute defined in just that class and not shared with other classes. This is useful as you develop instruction that needs to be exe- cuted privately in a closed environment. Other major changes include: ◆ Developing Solutions Built with the DOM3 event model. ◆ Using Namespaces in your projects. ◆ Controlling Data. ◆ Controlling Text. ◆ Drawing with the Shape Class. ◆ Ability to easily work with external ActionScript Libraries. Using ActionScript 3.0 From the Library of Wow! eBook ptg Chapter 15 Working with ActionScript 3.0 367 ActionScript 2.0 and 3.0 Similarities With all of the changes between ActionScript 3.0 and 2.0 it’s important to remember that there is a lot of functionality that is the same between them. For instance you still use the following in the same way: ◆ Variables ◆ Math object ◆ If/Else Statements ◆ Switch Statements ◆ String ◆ Date to control the use of date and time ◆ Array to build a structured collection of data ◆ Boolean to specify a true or false The following works in ActionScript 2.0 and ActionScript 3.0: var str:String = new String("Hello, "); var str2:String = new String("World"); trace (str + str2); You can see that the overall syntax struc- ture is the same between both versions of ActionScript. You still end your code with curly brackets, your variable names are still case sensitive, your variable names cannot start with a number and you still use number class objects the same way. For specific details about using an ActionScript language, click the Help menu, click Flash Help, and then look in the Programming ActionScript 2.0 or 3.0 or the ActionScript 2.0 or 3.0 Language and Components Reference online chapter. Extending ActionScript with Open Source Libraries The core to ActionScript 3.0 is that it can be easily extended. To this end, you can use dozens of great Open Source Libraries that can be used to extend the functionality of Flash. Great examples are: ◆ Box2D Physics Engine ( http://box2dflash.sourceforge.net/ ). ◆ CoreLib ( http://code.google.com/p/as3corelib/ ) a collection of basic utilities such as MD5 hashing, JSON serialization and advanced data parsing. ◆ Syndication Library ( http://code.google.com/p/as3syndication lib/ ) a library that allows you to parse all ATOM and RSS feeds easily. ◆ AlivePDF ( http://www.fpdf.org/ ) a library that allows you to convert your Flash screen content to PDF. These are some of the best Libraries you can use. Each comes with its own level of documentation. Extending ActionScript with C/C++ ActionScript can be extended using C/C++. The Open Source project Brajeshwar.com (http://brajeshwar.com/2008/cc-to-action- script-compiler/) is a project that allows you to take C++ programs and compile them to run in Flash. As an Example, they were able to take the C++ version of Quake, convert it and run it in Flash. That is darn impressive and could not be done with ActionScript 2.0. From the Library of Wow! eBook ptg 368 Chapter 15 To take advantage of the new po werful AV M 2.0 an d ActionScr ipt 3.0 you must be publishing your Flash movies to target Flash Player 9 or later. Adobe has done a great job of ensuring that the latest release of the Flash Player is installed on your computer. For instance, with the release of Flash Player 9, Adobe installed a feature that automatically updates your Player on your PC, Mac users receive updates through the Mac OS update tool and—as part of Flash Player 10.1—will now receive automatic updates as soon as new releases are available. Flash Player 10.1 will help your Flash movies to play back more efficiently on Mac based computers, especially for mobile devices, such as an Android, Palm Pre or BlackBerry phones. Flash Player 10.1 dramatically optimized code for the Cocoa Framework, it uses Apple’s Core Animation to render animation). Mobile users simply have not been able to view Flash. Now they can. To change which Flash Player you are targeting you need to open the Publish Settings and modify the Flash Player settings. Changing ActionScript 3.0 Settings Change Publishing Setting for ActionScript 3.0 Click File, and then click Publish Settings. The default settings on the Formats tab include Flash and HTML. Click the Flash tab. Click the Player list arrow, and then click Flash Player 9 or Flash Player 10. Click the Script list arrow, and then click ActionScript 3.0. Click the HTML tab. To publish settings for a specific subversion (an update to a main release) of the selected Flash Player, select the Detect Flash Version check box, and then enter the Flash Player subversion you want. For example, 10.1.52. This uses SWObject2 for version detection and embedding SWF into Web pages. (New!) Click OK. You are ready to start developing a ActionScript 3.0 Flash movie that uses AVM 2.0. 7 6 5 4 3 2 1 4 3 7 52 From the Library of Wow! eBook ptg Chapter 15 Working with ActionScript 3.0 369 6 Change ActionScript 3.0 Settings Click File, and then click ActionScript Settings. To create a document class, enter the filename of the ActionScript file without the .as extension. Specify the frame where the class definition resides. Specify any of the following options: ◆ Strict Mode. Select to to report compiler warnings as errors. ◆ Warnings Mode. Select to report warnings for issues when updating ActionScript 2.0 to ActionScript 3.0. ◆ Automatically declare state instances. Select to automatically declare instances on the Stage. ◆ Dialect. Select ECMAScript or ActionScript 3.0 (Recommended). Click any of the following tabs: ◆ Source path. Specify the location of the external ActionScript files that contain the class definition. ◆ Library path. Specify the location of pre-compiled ActionScript code, which resides in the SWC files. ◆ Config constants. Specify any configuration constants for use with conditional compilation. Click OK. 6 5 4 3 2 1 See Also See “Creating a New Document” on page 12 for more information on creat- ing an ActionScript 3.0 FLA document. 2 5 3 4 From the Library of Wow! eBook ptg 370 Chapter 15 Code hints makes it easy to insert and edit code accurately. With Code hints, you don’t have to remember all of the objects, methods, and prop- erties used in ActionScript. As you enter to type code, you can display the Code Hints popup menu to help you enter it correctly (New!). Code hints can appear after the dot (.) following a statement or command, where a property or method needs to appear next, or between paren- theses [()] in a method name. The Code Hints popup menu displays appropriate code options based on the currently entered code. When you enter code that refers to a custom ActionScript 3.0 class, you can also display the Code Hints popup menu to help you access the appro- priate code options (New!). When you use a custom class in a script, Flash automatically imports the related code for its use. Code hinting also works for nested class references, and all classes included in a SWC file. Inserting Code with Code Hints Use Code Hints Create or open a Flash document. Open the Actions panel or Script window. Place the insertion point where you want to add code. Start to type the code you want, and then press Control+Spacebar (Win) or A+Spacebar (Mac) or click the Show Code Hint button on the toolbar to manually display the code hints popup menu. Scroll through the list by using the scroll bar or pressing the Up Arrow and Down Arrow keys. To insert a code hint, do uble -click an item, or select it and then press Enter (Win) or Return (Mac). ◆ Press Esc to dismiss the list of code hints. 6 5 4 3 2 1 See Also See “Setting ActionScript Preferences” on page 349 for information on enabling code hinting. 2 4 3 5 6 From the Library of Wow! eBook ptg Chapter 15 Working with ActionScript 3.0 371 Use Code Hints for Custom Classes Create or open a Flash or class document (ActionScript 3.0). ◆ Store the custom classes in the same folder as the document. Open the Actions panel or Script window. Place the insertion point where you want to add code. Start to type the code you want, and then press Control+Spacebar (Win) or A+Spacebar (Mac) or click the Show Code Hint button on the toolbar to manually display the code hints popup menu. Scroll through the list by using the scroll bar or pressing the Up Arrow and Down Arrow keys. To insert a code hint, do uble -click an item, or select it and then press Enter (Win) or Return (Mac). ◆ Press Esc to dismiss the list of code hints. 6 5 4 3 2 1 Did You Know? You can reload code hints without restarting Flash. Open the Actions panel, click the Options menu, and then click Reload Code Hints. You can save time with Auto close- bracket insertion. When you type an open bracket { in the Actions panel or Script window, Flash automatically adds the corresponding close bracket }. (New!) Properties Options Type this Code hints for that same class Type new+<space> Displays a list of available classes and packages Type import+<space> Displays a list of all the built-in packages and custom packages Code Hinting for Custom Classes 2 4 3 5 6 From the Library of Wow! eBook ptg 372 Chapter 15 As you build Flash documents for ActionScript 3.0, it's almost a certainty that you will use elements over and over again. Flash created a solution that makes it easier to reuse elements called Code Snippets (New!). Snippets offer a simple and convenient method of creating reusable code, which especially helpful for those transitioning from ActionScript 2.0 to ActionScript 3.0. They are composed of bits of code and/or content along with comments for usage that you can save, edit, use, and reuse as often as you want. You can use a pre-defined code snippet supplied by Flash or create your own in the Code Snippets panel (New!). Inserting and Creating Code Snippets Insert a Code Snippet Select the object or Timeline frame in the Flash document where you want to insert/apply the code snippet. Open the Code Snippets panel. Click the arrow icon next to the folder (Actions, Timeline Navigation, Animation, Load and Unload, Audio and Video and Event Handlers) to display the snippet you want to insert. Select the snippet you want to insert. Click the Add to current frame button in the Code Snippets panel. TIMESAVER You can also drag a snippet from the Code Snippets panel directly onto an element, or double-click the code snippet in the Code Snippets panel. In the Actions panel, you can click the Code Snippets button. Click Yes to convert your selection to a movie clip, if necessary. A new ActionScript is added to a new layer, labeled Actions, in the Timeline. 6 5 4 3 2 1 1 2 5 3 4 Click to Play/Stop Sound snippet code New layer with ActionScript From the Library of Wow! eBook ptg Chapter 15 Working with ActionScript 3.0 373 Create a Code Snippet Open the Code Snippets panel. Click the Options button in the Code Snippets panel, and then click Create New Code Snippet. Type a title and tooltip. Type the code you want to use in the Code box or click Auto-fill to insert the code selected in the Actions panel. Select or clear the Automatically replace instance_name_here when applying code snippet check box. Click OK. 6 5 4 3 2 1 Did You Know? You can edit an existing code snippet. In the Code Snippets panel, select the snippet you want to edit, click the Options button, click Edit Code Snippets XML, make the changes you want, click the File menu, click Save, and then click the Close button in the Code Snippets tab. You can copy a code snippet to the clipboard. In the Code Snippets panel, select the snippet you want to copy, click the Copy to Clipboard button, and then click OK, if necessary. Now, you can paste it into the Actions panel or ActionScript file. Importing and Exporting Code Snippets If you want to share code snippets with others, you can import or export one or more from the Code Snippets panel. To import a code snippet, click the Options button in the Code Snippets panel, click Import Code Snippets XML, select the XML file, and then click Open. To ex por t a code sn ippe t, sel ect t he sni ppet y ou wan t to exp ort in th e Code Snippets panel, click the Options button, click Export Code Snippets XML, type a name, specify a location, and then click Save. For Your Information 1 2 3 4 5 6 From the Library of Wow! eBook . that contain the class definition. ◆ Library path. Specify the location of pre-compiled ActionScript code, which resides in the SWC files. ◆ Config constants. Specify any configuration constants. ActionScr ipt 3.0 you must be publishing your Flash movies to target Flash Player 9 or later. Adobe has done a great job of ensuring that the latest release of the Flash Player is installed on. configuration constants for use with conditional compilation. Click OK. 6 5 4 3 2 1 See Also See “Creating a New Document” on page 12 for more information on creat- ing an ActionScript 3.0 FLA document. 2 5 3 4