adobe flash cs5 on demand part 59 docx

6 216 0
adobe flash cs5 on demand part 59 docx

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

Thông tin tài liệu

ptg 360 Chapter 14 Working with Loops Loops allow Flash to perform an action repeatedly. You can use a loop to create a dynamic drop-down menu, validate data, search for text, duplicate movie clips, and even detect collisions in games that have pro- jectiles and objects. Conditional statements let you execute an action based on a specific condition. You can have a specific action con- tinue to loop until a certain condition is met. For example, continue to search for a specific text string until it is found or the end of the text document is reached. Loops come in two forms—While loops and For loops. While loops wait for a specific condition to start or stop the loop. That may sound similar to the For loop, with one exception: The For loop is self-contained, and the While loop works with an external condition, or one outside the scope of the loop. ◆ While Loops. While loops continue to execute while a certain condition exists (keep looping or searching) until a specific value is reached. i = 4; while (var i > 0) { my_mc.duplicateMovieClip("newMC" + i, i ); i ; } ◆ For Loops. For loops are self- contained counters. For example, loop (repeat the action) ten times and then stop. x = x; for (x=0; x<=10, ++x) { myClip.duplicateMovieClip ("myClip" + x, x); myClip._rotation =45 + x * 10; } When you create a Looping action, you can further control the loop by using the fol- lowing loop exceptions: ◆ Continue. The continue exception lets you stop the current loop from performing its actions and jump directly to the next cycle of the loop. ◆ Break. The break exception is used to exit a loop, even if the original condition that is driving the loop is still true. For example, if you create a While loop using the following script: total = 0; i = 0: while (++i <=20) { if (i == 10) { continue; } total +=i; } The results would be a script that executes and adds 1 to total; unless the value of i equaled 10. This would create a sequence of numbers 1 2 3 4 5 6 7 8 9 11 12 13 14 15 16 17 18 19 20. If you had used the break exception in place of continue, the values would read: 1 2 3 4 5 6 7 8 9. Therefore, it loops whether For or While are controlled by internal or external conditions, and using a break or con- tinue exception gives you further control over the loop. From the Library of Wow! eBook ptg Chapter 14 Using Basic ActionScripts 361 The For loop works with an increasing or decreasing numeric value. For example, you could use a For loop to create several copies of a movie clip on the Stage. Letting the For loop control movie clips to the Stage is far more efficient than having to move them one at a time. In addi- tion, the visitor can control when the items appear on the Stage using a button. Using For Loops Use For Loops Drag a movie clip from the Library to the Stage, and then select the movie clip. Enter a unique instance name for the movie clip in the Properties panel. Place a button on the Stage, and then select the button. Enter the script (ActionScript 2.0) as shown in the illustration. ◆ ActionScript 3.0 example files are available on the Web at www.perspection.com . When you play the movie, clicking on the button causes the action to loop 10 times. Each time it loops, it duplicates the original movie clip and rotate it by 45 degrees plus the current value of x times 10. 4 3 2 1 4 Did You Know? You can use a For Loop to pause a Flash movie. Select a value, instruct the loop to increment by 1, and then loop until the value is reached. Use a loop timer for such items as a Flash slide show, where you want the slides to display on the stage for a given number of seconds before moving to the next slide. 1 3 2 From the Library of Wow! eBook ptg 362 Chapter 14 Behaviors are time-savers because they give you sections of ActionScript 2.0 code (not supported in ActionScript 3.0; see Code Snippets in the next Chapter) for common Flash tasks. Behaviors are a great way to introduce yourself to the wonderful world of ActionScripting without having to write all the code. For example, if you want to add a Play ActionScript to a button, you can do it using the Add button in the Behaviors panel, or you can write out the code on your own; see the example code below. Using Behaviors, as opposed to writing the code by hand, is not better, it’s simply faster. The more time you save doing common Action-Scripting tasks using Behaviors, the more time you will have for the creative process. Using the Behaviors Panel You use the Behaviors panel to apply the behavior to a triggering object, such as a but- ton. You specify the event that triggers the behavior, such as releasing the mouse. Next select a target object, such as the movie clip instance, and then select settings for behavior parameters, such as a frame number or label, or a relative or absolute path. Flash comes with built-in behaviors, such as Load Graphic, Duplicate Movieclip, and GotoAndPlay At Frame Or Label. To add and configure a behavior, select a trigger object, and then step through the following general instructions (steps may vary depending on the behavior): Click the Window menu, and then click Behaviors . Click the Add ( + ) button, and then select a behavior from the menu. If necessary, select settings for the behavior parameters, and then click OK . Under Event, click On Release (the default event), and then select a mouse event from the menu. 4 3 2 1 Example Play ActionScript 2.0 code on (release) { if(this.video_1._parent._currentframe == this.video_1.parent._totalframes){ this.video_1parent.gotoAndPlay(1); } else { this.video_1._parent.play(); } } Working with ActionScript Behaviors Add button Click to select a mouse event. Behavior parameters From the Library of Wow! eBook 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 . objects. Conditional statements let you execute an action based on a specific condition. You can have a specific action con- tinue to loop until a certain condition is met. For example, continue. condition to start or stop the loop. That may sound similar to the For loop, with one exception: The For loop is self-contained, and the While loop works with an external condition, or one. button on the Stage, and then select the button. Enter the script (ActionScript 2.0) as shown in the illustration. ◆ ActionScript 3.0 example files are available on the Web at www.perspection.com . When

Ngày đăng: 02/07/2014, 21:20

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

Tài liệu liên quan