Học Actionscript 3.0 - p 3 pot

10 321 0
Học Actionscript 3.0 - p 3 pot

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

Thông tin tài liệu

Preface xix Colophon Our look is the result of reader comments, our own experimentation, and feedback from distribution channels. Distinctive covers complement our distinctive approach to technical topics, breathing personality and life into potentially dry subjects. The text font is Linotype Birka; the heading font is Adobe Myriad Pro. Download from Wow! eBook <www.wowebook.com> Download from Wow! eBook <www.wowebook.com> 1 IN THIS PART Chapter 1 ActionScript Overview Chapter 2 Core Language Fundamentals GettinG Started PART I Part I starts this book off with a collection of basic overviews, spanning Chapters 1 and 2. It begins with a survey of ActionScript, providing a list of new feature highlights, a brief explanation of procedural versus object-oriented programming, and gets you started right away with your first script. It concludes with a review of core language fundamentals, most of which remain consistent across all versions of ActionScript. The material at the out- set of the book serves as an introduction to ActionScript for those new to the language, or as a refresher for those already familiar with it, and allows you to focus later on ActionScript 3.0–specific syntax. Download from Wow! eBook <www.wowebook.com> Download from Wow! eBook <www.wowebook.com> 3 IN THIS CHAPTER What’s New in ActionScript 3.0? The Flash Platform Procedural Versus Object- Oriented Programming The Document Class Legacy Code Compatibility Hello World What’s Next? While you likely know that ActionScript is the main scripting language of the Flash Platform, and you’re no doubt eager to begin working with the new version, a brief overview of its development will give you some insight into its use—particularly as related to Flash Player and how it handles different versions of ActionScript. This brief introductory chapter will give you a quick look at where ActionScript 3.0 fits into your workflow. Before we get started, it might help to understand how you get from ActionScript code to a finished file that you can put into a website. If this isn’t news to you, bear with us for just a paragraph or two. When you publish a Flash file—using Flash Professional’s File →Publish or Control →Test Movie—all of the internal graphics and other assets used in your movie, as well as all of the ActionScript code, are compiled into a final file format called a SWF (pronounced “swiff” or “S-W-F”). That is, a part of your Flash Platform application of choice (such as Flash Professional) con- tains software called the compiler. This software converts all of your human- readable scripts into an optimized, machine-readable format. It combines that code with your assets into a single SWF file that Flash Player can decode and play back for all to see. Although your SWF can load external assets not already compiled into your SWF (such as an MP3 or a video), any asset that you imported or embedded and all scripts—even if they originate outside the FLA (pronounced “flah” or “F-L-A”) file—must go through this compilation process to be included in the SWF. This is why you must publish a new SWF every time you make a change to your code. It’s also why you don’t have to distribute ActionScript files with your SWF, even if you created external files, such as classes, when coding. Distributing ActionScript files with your SWF won’t affect playback, but it may expose your source code to the public. This is fine when you’re contributing code for others to learn from, but it won’t make a client happy if you’re doing work for hire! What Is aCtIonsCrIPt? CHAPTER 1 Download from Wow! eBook <www.wowebook.com> Part I: Getting Started 4 For most users, the compilation process occurs behind the scenes and is handled by Flash Professional. At the time of this writing, the current version is Flash Professional CS5, but most of this book is compatible with versions dating back to Flash Professional CS3. Other applications, such as Adobe’s Flash Builder (or its predecessor Flex Builder), Power Flasher’s FDT, the open source FlashDevelop, and even text editors in combination with a command-line compiler, can transform ActionScript into SWFs. However, this book focuses primarily on Flash Professional as an ActionScript editor. Many examples will work seamlessly in any ActionScript editor; other exam- ples will rely on symbols found in the library of a sample Flash file (FLA). This will be discussed briefly in the “Flash Platform” section of this chapter, but be sure you’re comfortable with this workflow before investing any time in these examples. If your primary goal is to become a Flex developer, for example, with an equal emphasis on that technology’s MXML syntax, you may want to pick up a companion to this text that focuses more significantly on Flex, such as Learning Flex 4 (O’Reilly). • What Is ActionScript 3.0? Every new version of ActionScript intro- duces new features. ActionScript 3.0, however, was written from scratch (not built on prior versions of the language) and is handled entirely separately from previous versions of ActionScript anywhere the language is used. This intentional branching allows for syntax improvements and significantly improves performance, but also makes it more difficult to use multiple versions of ActionScript at the same time. • The Flash Platform. ActionScript 3.0 can be used in Flash, Flex projects, and AIR (Adobe Integrated Runtime) desktop applications, each of which are part of what is collectively known as the Flash Platform. Although they affect only a small portion of the language, differences in these envi- ronments prevent ActionScript 3.0 from working exactly the same way in every application that is part of the Flash Platform. The fundamentals, however—indeed, the bulk—of the language, are the same throughout. • Procedural Versus Object-Oriented Programming. A lot of attention has been focused on the object-oriented programming (OOP) capa- bilities of ActionScript 3.0, and the language’s power really shines in this area. However, embracing ActionScript 3.0 doesn’t mean that you must become an expert in OOP. Using Flash, it is still possible to write scripts in the timeline, using functions to organize more complex code. This is commonly called procedural programming. If you prefer object-oriented programming, enhancements to ActionScript’s OOP structure make ver- sion 3.0 more robust and bring it more in line with the features of other OOP-based languages (such as Java). This also makes moving between such languages a bit easier. What Is ActionScript? Download from Wow! eBook <www.wowebook.com> What’s New in ActionScript 3.0? Chapter 1: What Is ActionScript? 5 • The Document Class. Object-oriented programming is not for every- one, but for those starting on the OOP journey, Flash offers a simple step- ping off point in the Document class. Using this feature, you need only specify an external ActionScript class file as your starting point, and no timeline script is required. • Legacy Code Compatibility. Because ActionScript 3.0 can’t mingle with previous versions of the language in the same file, developing projects that support older code is a challenge. We’ll briefly introduce the issues involved and point to a technique that makes possible some communica- tion between ActionScript versions. • Hello World. This chapter will conclude with you writing your first ActionScript 3.0 application. We’ll dive into some syntax for text manip- ulation, but don’t worry: we’ll cover the material in more detail in a later chapter. This exercise is just to get you started and build a little confidence. What’s New in ActionScript 3.0? If you’re familiar with ActionScript or you’re learning it based on experi- ence with another programming language, you may want to know what ActionScript 3.0 has to offer. Although the third major version of the Flash Platform’s primary scripting language contains much that will be familiar to users of prior versions, it’s probably best to think of ActionScript 3.0 as entirely new, for a few simple reasons. First, a few things are quite different, such as how events are handled and the way assets are displayed. Second, subtle changes run throughout the lan- guage. (These are usually small concerns, such as a slight change in the name of a property, but if you are used to ActionScript 2.0, for example, old habits can die hard.) Most importantly, ActionScript 3.0 has been rewritten from the ground up and uses a different code base than prior versions of the language. This optimization provides relatively dramatic performance increases, but it means that ActionScript 3.0 code cannot be mixed with prior versions of the language in the same file. Regardless of your experience level, don’t let the newness of ActionScript 3.0 intimidate you. It’s true that its learning curve is steeper than that of prior ver- sions, but that is usually a function of its robustness more than one of difficul- ty. Typically, whether you are coming to ActionScript 3.0 from a prior version of ActionScript or another language altogether, there is an adjustment period during which users must occasionally adapt to a new way of doing things. Download from Wow! eBook <www.wowebook.com> Part I: Getting Started 6 What’s New in ActionScript 3.0? Here’s a look at some of the highlights of ActionScript 3.0. Keeping these benefits in mind may help make it easier to learn a robust language, or accept change—particularly when that change may initially seem tedious or overly complicated. Select new features include: Detailed error reporting ActionScript 3.0 supports strict data typing of variables, arguments, values returned from functions, and so on. Chapter 2 discusses data typing in depth, but it boils down to telling the compiler and Flash Player which kind of data you want to work with at different points within your proj- ect code. This allows the compiler to warn you if you use the wrong data type, catching related errors. ActionScript 3.0 supports static data type checking, which occurs at compile time (when publishing your SWF), and improves dynamic data type checking, which checks for errors at run- time. In ActionScript 3.0, errors will no longer fail silently. Understanding this fully in this overview isn’t important, and the benefits of data typing will become apparent after reading Chapter 2—and even more so after gaining a little experience with ActionScript 3.0. For now, just take heart knowing that error checking and reporting are more vigilant than in any prior version of ActionScript. Syntax improvements Syntax issues have been unified and cleaned up throughout the language. For instance, some property names have been clarified and made con- sistent by removing leading underscores. (Setting the x coordinate of a movie clip, for example, now uses x instead of _x.). Also, former multiple and varying ways of approaching the same or similar tasks have been simplified and made consistent. New display architecture The many previous approaches to displaying assets are now consolidated. ActionScript 3.0 has simplified how visible assets, such as movie clips and text fields, are handled, using a new display architecture called the display list. Chapter 4 examines this major change introduced by ActionScript 3.0. New event architecture Still another example of improved consistency, all events—such as a mouse click or key press—are handled by event listeners in ActionScript 3.0—essentially listening for a specific event to occur, and then reacting accordingly. The new event model is very powerful when combined with the display list, allowing mouse and keyboard events to propagate through multiple display objects. The event model is discussed in Chapter 3. Improved XML handling Working with complex XML documents is a pleasure with ActionScript 3.0. It allows you to reference XML data the same way you reference properties of other objects, such as movie clips or buttons, using a similar syntax. Download from Wow! eBook <www.wowebook.com> What’s New in ActionScript 3.0? Chapter 1: What Is ActionScript? 7 You’ll learn more about this in Chapter 14, but a simple example is refer- ring to an XML node called phone, nested inside a node called user, as user.phone. This is comfortable territory when you remember that a movie clip called mc2, nested inside a movie clip called mc1, is referenced as mc1.mc2. Additional text options New text-processing options now allow for much finer control over text manipulation. For example, you can now find the contents of a particular line in a text field, the number of characters in that line, and the char- acter at a specified point (such as under the mouse). Flash Professional CS5 also introduces a brand new text feature called the Text Layout Framework (TLF). This new engine provides a greater degree of text con- trol, including traditional typographic features, automatic text flow, and even support for right-to-left and vertical text layouts and double-byte languages (such as Chinese, Japanese, and Korean, among others). Text is discussed in Chapter 10. More sound management options ActionScript 3.0’s sound capabilities are among the jazziest changes to the language. On a practical level, they improve programmatic control over both individual sounds and all sounds playing. Sounds are now placed into separate channels, making it easier to work with more than one discrete sound. Sounds are also funneled through a sound mixer for collective control. You can get the amplitude and frequency spectrum data from sounds during playback, as well as from microphone input. Chapter 11 covers sound in detail. New access to raw data For more advanced needs, you can access raw binary data at runtime. Individual bytes of data can be read during download, during sound playback, or from bitmap data, to name a few examples. These bytes can be stored in a large list and still be accessed quickly and efficiently. We’ll show an example of this technique in Chapter 11 when discussing sound visualization. New automatic scope management In a programming language, the word scope is sometimes used to define the realm in which an object, such as a movie clip, lives. A movie clip might exist in one part of a Flash movie but not another. For example, a child movie clip might be nested inside one of two movie clips found in the main timeline. That nested movie clip exists within one clip but not the other. Its scope, therefore, is restricted to the movie clip in which it lives, or its parent. Programming structures have specific scopes, as well, and ActionScript 3.0 greatly simplifies this concept by automatically keep- ing track of where a particular block of code was defined—so you don’t have to. Download from Wow! eBook <www.wowebook.com> Part I: Getting Started 8 The Flash Platform Improved object-oriented programming If you’re familiar with object-oriented programming, you’ll be glad to know that ActionScript 3.0 supports this structure well. If you’re new to OOP, don’t worry: we’ll introduce it in this book at a comfortable pace. We’ll focus on syntax throughout by using simple examples, and we’ll start to discuss OOP in greater detail in Chapter 6. If you’re already familiar with OOP, you may be happy to know that sealed classes and new namespaces, among other things, have been added to ActionScript 3.0. Most classes are sealed by default, meaning the compiler recog- nizes only those properties and methods defined at compile time. This improves memory usage and performance. However, if you need to add properties to an instance of a class at runtime (for example), you can still use dynamic classes such as the MovieClip and Object, and you can make your own custom classes dynamic. Additionally, namespaces, including the ability to define custom namespaces, allow finer control over classes and XML manipulation. The Flash Platform It’s important to note that this book focuses primarily on developing ActionScript 3.0 applications using the Flash Professional application (also commonly referred to as an integrated development environment, or IDE). However, ActionScript 3.0 is the programming language used in Flash Platform technologies, as well—notably AIR and Flex. AIR is the Adobe Integrated Runtime application, a sophisticated way of delivering your applications to the computer desktop, rather than through a web browser. Flex is another technology for creating SWFs that includes not only the ActionScript 3.0 language, but also MXML, a tag-based language that is part of what is commonly called the Flex Framework. This book will not discuss MXML or the Flex Framework at all, but most of the ActionScript you learn herein can be used in ActionScript-only Flex projects. The existence of AIR and Flex means that the scripting skills you develop using Flash Professional will be largely applicable in other areas of the Flash Platform, extending your reach as a programmer. There are, however, some differences between these technologies that are important to understand when examining the big picture of cross-application scripting. For instance, each technology adds some features that are not available to the others. Using a feature that is specific to AIR or Flex, for example, means that your code may not compile in Flash Professional. The thing to keep in mind is that the ActionScript 3.0 language skills you develop will ease your move between these applications and even allow you to work with different authoring tools or compilers to create your finished product. N OT E AIR projects can also be created from HTML, JavaScript, and PDF, but ActionScript 3.0 is a large part of its appeal and the language most relevant to this discussion. N OT E This book is written for readers who have some familiarity with scripting but are new to ActionScript 3.0, and it assumes a working knowledge of the Flash Professional interface. See the Preface for more information about this expectation. While virtually all of the code in the book applies to any tool that sup- ports ActionScript 3.0, some of the examples use assets that are embedded within FLA files—the main document format used by Flash Professional. The companion website, http://www. LearningActionScript3.com, contains information about using the examples with applications other than Flash Professional. See the “Using the Book Examples” post as a starting point for learning more about this process. Download from Wow! eBook <www.wowebook.com> . focused on the object-oriented programming (OOP) capa- bilities of ActionScript 3. 0, and the language’s power really shines in this area. However, embracing ActionScript 3. 0 doesn’t mean that. <www.wowebook.com> Part I: Getting Started 8 The Flash Platform Improved object-oriented programming If you’re familiar with object-oriented programming, you’ll be glad to know that ActionScript 3. 0 supports. XML manipulation. The Flash Platform It’s important to note that this book focuses primarily on developing ActionScript 3. 0 applications using the Flash Professional application (also commonly

Ngày đăng: 06/07/2014, 18:20

Từ khóa liên quan

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

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

Tài liệu liên quan