Tài liệu Thiết kế flash với flash cs5 part 58 ppt

5 96 0
Tài liệu Thiết kế flash với flash cs5 part 58 ppt

Đang tải... (xem toàn văn)

Thông tin tài liệu

ptg Working with ActionScript 3.0 Introduction Do you like Flash games and cool features on YouTube, like jumping from standard view to HD? There are two faces to Flash: the one you see and then the complex scripting engine, called ActionScript, you don’t. Without ActionScript Flash would not be interactive. ActionScript is the scripting language built within Flash that allows you to build interac- tive solutions. Any Flash movie where you need to click, drag or pause requires the use of ActionScript to instruct Flash what to do. ActionScript is the core to all interactivity and logic built into Flash. The release of Flash CS3 introduced ActionScript 3.0, a significant overhaul of the scripting language. Older versions of ActionScript ran slowly with large and complex scripts. ActionScript 3.0 changed this. ActionScript 3.0 is up to 10x faster than ActionScript 2.0 and brings a whole lot more func- tionality to the party. ActionScript 3.0 adopted a true object- oriented approach to development, a technique that allows Flash applications to compete with solutions developed with Microsoft’s .NET or Oracle’s Java. ActionScript 3.0 dramati- cally extends what can be accomplished by increasing the speed of Flash through the Flash Player 9 and 10. With ActionScript 3.0 and Flash Player 10.1, you can deliver Flash solutions to more desktops, devices and systems than ever before. ActionScript 3.0 is now the default scripting language in Flash CS5. In this chapter, you’ll learn why ActionScript 3.0 is a pro- gramming language you will want to start using, what has changed from earlier versions of ActionScript, what is the same and how you can make your applications rock by using ActionScript 3.0. The only challenge you have is to decide what to develop first. 15 15 What You’ll Do Use Object-Oriented Programming Enable Flash to Execute Solutions Faster with AVM 2.0 Use ActionScript 3.0 Change ActionScript 3.0 Settings Insert Code with Code Hints Insert and Create Code Snippets Develop Solutions Built with the DOM3 Model Work with Classes Work with Objects and Classes Use NameSpaces in your Projects Control Data Manipulate XML with E4X Use Regular Expressions Control Text Draw with the Shape Class Extend Flash with Open Source Libraries 363 From the Library of Wow! eBook ptg 364 Chapter 15 Objects are the key to understanding object- oriented programming. In object-oriented programming (OOP), an object is just as real as an object in this world. For example, your dog, or even your computer are objects that exist in the real world. Real-world objects share two characteristics with objects in the computer world: They have a specific state and behavior. For example, dogs have a state such as their name, color, breed, and if they're hungry. Dog behaviors would be: barking, fetching, and wagging their tails. All objects in the real and computer worlds have a behavior and a state. Computer objects are modeled after real- world objects in that they also have a specific state and behavior. A class is a representation of an object that stores information about its data type, state, and behaviors. A class can include three types of characteristics: proper- ties, methods, and events. A property repre- sents different data associated with an object. A method is an action that can be performed by an object. An event is a system, applica- tion, or user action, such as a mouse click, that triggers an action related to an object. After you create a class, you can also cre- ate new classes based on the existing one, known as subclassing or extending a class. The subclass inherits all the properties and methods of the parent class, known as the superclass . For example, you could create a superclass called Parents, and a subclass called Children. Inheritance allows one class definition (subclass) to include all the func- tionality of a different class definition (super- class). You can also additional methods and properties to the subclass as well as override methods inherited from a superclass, which is called polymorphism . Inheritance and sub- classing are very useful for large projects where you can share similar functionality and reduce the amount of code. An object maintains its state using one or more variables. A variable is an item of data named by an identifier, and an object per- forms its behavior with methods. Everything an object understands (its state) and what it can do (its behavior) is expressed by the vari- ables and the methods within that object. An object that represents a speeding car would have variables that instruct the object as to speed, direction, and color. These vari- ables are known as instance variables because they contain the state for a particular object, and in object-oriented terminology, a particular object is called an instance . In addi- tion to its variables, the car would have meth- ods assigned to change speed and turn on the lights. These methods are formally known as instance methods because they inspect or change the state of a particular instance. Variables Methods Using Object-Oriented Programming From the Library of Wow! eBook ptg Chapter 15 Working with ActionScript 3.0 365 Flash Player, the plug-in you install in your Web browser to play back Flash SWF files, is the key to success in building fast applications. To compile and then run any ActionScript in your Flash movies the Flash Player uses a tool called the ActionScript Virtual Machine, or AVM. For ActionScript 1.0 and 2.0, the Flash Player uses AVM 1.0. Flash Player 9 introduced a new AVM, called AVM 2.0, that is dedicated to running just ActionScript 3.0 SWF files. To put it simply: AVM 2.0 rocks. It makes your code zip along. AVM 2.0 is critical to Adobe’s future success with Flash. With AVM 2.0, you can build applications that have the speed and complexity of traditional desktop applications. Examples of this include Adobe’s WorkFlow Lab, a tool that allows you to develop complex Workflow solutions and competes directly with Microsoft’s desktop tool Visio; Adobe Story, a tool that allows you to script rich media and video productions; and Kuler, a sophisticated color management tool. Using these tools has the same feel and responsiveness as tradi- tional desk top application. But they are not. They are all running in Flash. Developing ActionScript 3.0 solutions that are targeted at the AVM 2.0 rendering engine ensures you’ll have highly optimized Flash solutions. While ActionScript 3.0 is not required for content that runs in Adobe Flash Player 9 or later, it allows performance improvements that are available only with the AVM 2.0. ActionScript 3.0 code can execute up to ten times faster than legacy ActionScript code. Flash Player 9 supports AVM 1.0 for backward compatibility with existing and legacy con- tent. Flash Player 7 and 8 only support AVM 1.0 and not AVM 2.0. However, there are a few things you need to know about compatibility. A SWF file cannot combine ActionScript 1.0 or 2.0 with ActionScript 3.0 code, and ActionScript code can load an SWF file with ActionScript 1.0 or 2.0, but it cannot access the file’s variables and functions (except you can use the loadMovieNum() command and pass a level parameter). If you have ActionScript 1.0 or 2.0 code and want to use version 3.0 code, you need to migrate all the old code to version 3.0. If you want to use behaviors, you need to use ActionScript 2.0; behaviors are not available for ActionScript 3.0. Virtual Machines and JavaScript Web Browsers use virtual machines to run JavaScript. JavaScript is a kissing cousin to ActionScript. Both technologies are structured on the international ECMA-262 scripting for- mat, or ECMAScript as it is often referred too. Web browsers such as Google’s Chrome, Apple’s Safari, Mozilla’s Firefox and Microsoft’s Internet Explorer all use Scripting JVM’s (JavaScript Virtual Machines) to render content. Many of these companies have even given their JVM engine’s cool code names, such as V8 for Chrome, SquirrelFish for Safari and TraceMonkey for FireFox. Unfortunately, Microsoft win’s the prize for least inspired name by calling their JVM just JS, for JavaScript. Of course, Adobe’s AVM 2.0 is a close second. Any chance of getting a cool code name for AVM 3.0, Adobe? Enabling Flash to Execute Solutions Faster with AVM 2.0 From the Library of Wow! eBook 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 . increasing the speed of Flash through the Flash Player 9 and 10. With ActionScript 3.0 and Flash Player 10.1, you can deliver Flash solutions to more. ActionScript to instruct Flash what to do. ActionScript is the core to all interactivity and logic built into Flash. The release of Flash CS3 introduced

Ngày đăng: 26/01/2014, 18:20

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan