adobe flash cs5 on demand part 61

Chia sẻ: Mr Yukogaru | Ngày: | Loại File: PDF | Số trang:6

lượt xem

adobe flash cs5 on demand part 61

Mô tả tài liệu
  Download Vui lòng tải xuống để xem tài liệu đầy đủ

Phát triển các giải pháp xây dựng với sự kiện DOM3 Model ActionScript 3.0 bây giờ hỗ trợ cú pháp ECMAScript, DOM3, mô hình sự kiện. Trong điều kiện của giáo dân, điều này có nghĩa là bạn đang sử dụng các đối tượng Listener để phát hiện khi bạn tương tác với bộ phim Flash của bạn bằng cách sử dụng bàn phím, chuột, hoặc những cử chỉ mới giao diện sử dụng trực tiếp chạm vào màn hình.

Chủ đề:

Nội dung Text: adobe flash cs5 on demand part 61

  1. Developing Solutions Built with the DOM3 Event Model ActionScript 3.0 now supports the 3 Create a new layer in the Timeline with ECMAScript, DOM3, event model syntax. In the name Actions, and then select the laymen’s terms, this means that you now use layer. the Listener object to detect when you inter- act with your Flash movie using either a key- 4 Open the Actions panel, create the board, mouse, or the new gesture interfaces function, and then add the listener that triggers the function. used in touching the screen directly. This is a big move from ActionScript 2.0. As an example, the following script is ActionScript 2.0 that instructs a Movie Clip to jump to frame 25 of a movie: The function is called gotoFunction and the parenthesis dictates that it is looking for a mouse driven event. There is only one instruction in the function, the gotoAndStop function that moves the Timeline to frame 25. ActionScript 3.0 uses Listeners to trigger The first part of the Listener, myMovie, when an event occurs. Listeners are more instructs ActionScript to target the movie complex to work with, but, give you more clip object on the Stage. The second part flexibility. There are essentially two parts to a of the Listener, addEventListener, Listener. The first is a function that describes instructs Flash that you are using the what is going to happen when you trigger an Listener object.The parenthesis has two event. The second is the Listener that waits for parts that explain that the event is a a specified event to happen, such as mouse mouse event and to use the function clicking on a button. gotoFunction. The following steps adds an ActionScript 3.0 The ActionScript 3.0 event model gives you event that mimics the same event as the flexibility to write more complex scripts and ActionScript 2.0 example above. to extend the functionality of the event model beyond traditional mouse and keyboard inter- Use ActionScript 3.0 faces. To do this you use the core object class controlling events on the screen called the 1 Click the File menu, click New, click EventsDispatcher class. Through this you can ActionScript 3.0, and then click OK. not only leverage standard events such as 2 Create a new Movie clip on the Stage mouse clicks and the keyboard, but you can with the name myMovie. extend the class with your event types. 374 Chapter 15
  2. Working with Classes A common design pattern in Object Oriented 4 Remove the line that says //constructor programs separates design, programming code and replace it with: and data elements. Flash CS5 adds this func- tionality with the inclusion of Classes. A Class trace (“Hello, World”); is a packaged document that you can use to 5 Click the File menu, and then click Save explain how UI components, business logic to save the Class file. and data elements are to interact. 6 In the Flash document, click the Control A Class is a separate ActionScript file that menu, point to Test Movie, and then click is associated with the main Flash file and Test to view the results. movie clips. You can use Flash CS5 as the Class file editor or your favorite text editor The words “Hello, World” posted to the Output panel. such as Eclipse, Notepad or Textedit. A Class file is only a text file. It is very easy to create Classes provide you a way in which you entire Flash movies using just Class files and can create public and private class elements. not even add any content into a traditional The difference between the two is related to timeline. how you use the data. For instance, a public property can be shared throughout your Create a Simple Class File whole Flash movie. A private property can 1 Click the File menu, click New, click only be used within the class it is defined in. ActionScript 3.0, and then click OK. 2 Click the File menu, and then click Save Add Classes to Movie Clips to save the file with the name You can also add separate class references helloWorld.fla. directly to Movie Clips in your Library. 3 Open the Properties panel. 1 Open the Library panel. 4 Click the Edit Class Definition button 2 Right-click (Win) or Control-click (Mac) a (pencil icon) in the Property inspector, Movie Clip, and then click Properties. enter a new class with the name helloClass, and then click OK. 3 In the Properties dialog box, click Advanced if necessary, and then select A new ActionScript file named the Export For ActionScript check box. opens with the following ActionScript 3.0 script. 4 Click OK. A class is automatically created for the sym- bol using the name of the Movie Clip. You can now modify the Class file for the Movie Clip in your favorite text editor. Chapter 15 Working with ActionScript 3.0 375
  3. Working with Objects With ActionScript 3.0, you specify a class definition, either document or object level, to work with objects using ActionScript code. To create and Classes your own class, you need to follow a certain syntax. First, you enter a package statement to indicate where your class will be found. Then, you enter a class statement to define the name of the class. At this point, you define each property in the class by using variables, and each method by using functions. When you define a class element, such as a property or method, you can also specify an attribute. A pri- vate attribute can be called only by code within the class, while a pub- lic attribute can be called by any code in the program. After you define a class, you can define a subclass that inherits all the properties and methods of the parent class, or superclass. In the subclass, you can add methods and properties and override others from the superclass, known as polymorphism. If you want to trigger actions based on events, you can use the EventDispatcher class to keep track of event listeners and notify them of events. Work with Objects and Classes in ActionScript 2 4 5 6 1 Create or open a Flash document (ActionScript 3.0). 2 Open the Properties panel. 3 Click the Stage. 4 In the Class field, type the name of the ActionScript file to create a document class definition. IMPORTANT Be sure not to include the .as extension. 8 10 5 Open the Library panel. 6 Right-click (Win) or A-click (Mac) the object you want to control in ActionScript, and then click Properties. 7 7 Click Advanced, if available. 9 (Button name changes to Basic.) 8 Class definitions Enter a name for the object. 9 Select the Export for ActionScript check box. The Class appears with the same name as the object, and the Base class appears with object type. 376 Chapter 15
  4. 10 Click OK. Package Class 1 ActionScript file statement statement 11 If prompted, click OK to define the new class. 12 Click the File menu, and then click Save. 13 Click the File menu, and then click Function New. Variable 14 Click ActionScript File. 15 Click OK. 16 Click the File menu, and then click Save As. 17 Navigate to the folder with the Flash document, and then name the file (same as the one in Public attribute Change property Step 4). 18 Click Save. 19 In the Actions panel, enter the script as shown in the illustration. Common Syntax in ActionScript 3.0 IMPORTANT ActionScript 3.0 is Command Description case-sensitive, so upper- and Dot syntax Access the properties and methods of an object. lower-case make a difference. myDotEx.method (); 20 Click the File menu, click Save, Slash syntax Not supported in ActionScript 3.0. and then close the ActionScript Literals A value that appears directly in your code. file. “text”, true, false, 10 21 Click the Control menu, point to Semicolon (;) Terminates (ends) a statement. var a:int = 2; Test Movie, and then click Test. Parentheses Indicates order of operation, evaluates a series of expressions, or pass parameters. Comments Documents code. Single-line comments begin with //, Did You Know? while multi-line comment begin with /* and ends with */. You can define accessor methods. For Keywords Reserved words you cannot use as identifiers. as, class, advanced ActionScripting, you can const, if, var, true, in also define accessors methods, which Constants A fixed value. const MINIMUM:int = 0; ActionScript are a cross between a method and defines constants with all CAPS with words separated property (defined like a method, yet by an underscore. const MOUSE_DOWN:String creates an instance like a property). = “mouseDown”; Chapter 15 Working with ActionScript 3.0 377
  5. Using Namespaces in your Projects Namespaces are ways in which you can ating. This is commonly used in XML when This is an RSS feed you are importing documents using a URI indicator. This is a test RSS You can also use the NameSpace as a way document. to overload your objects. The principle in overloading is to create several methods that en-us have the same name but differ in the types of input and output they generate. The following example is built using a this class is to pull in an XML document and step through the formatting of the code. Using Namespaces you can instruct Flash where to find a definition of the document type you are using, in this case an RSS for- matted document type. 1 Click the File menu, click New, click BBC News Center ActionScript 3.0, and then click OK. 2 Open the Properties panel. Welcome to the BBC News 3 Click the Edit Class Definition button Center (small pencil icon) in the Properties BBC inspector, enter a new class with the name NamespacesExample, and then 2010-02-12 click OK. A new ActionScript file named 4 Create a simple RSS formatted XML BBC Radio Center document. You can use the following formatted RSS document: 378 Chapter 15
  6. 5 Open the NamespacesExample Class, for (var i:uint; i < len; i++) and then define the package with a { public class named NamespacesExample arr.push({title:items[i].title, that will extend the functionality of the Sprite object. creator:items[i].dc::creator, date:items[i].dc::date, link:items[i].link, package description:items[i].description}); { } return arr; import flash.display.Sprite; } public class NamespaceExample extends 8 Add a Public function that will use the Sprite RSS namespace and send the content to 6 Insert the Namespace reference that the Output panel: describes how to use RSS XML. public function NamespaceExample() { { private var rss:Namespace = new var myXML:XML = getRSS(); Namespace(""); var rssItems:Array = private var rdf:Namespace = new parseRSS(myXML); Namespace(" var len:uint = rssItems.length; rdf-syntax-ns#"); for (var i:uint; i < len; i++) private var dc:Namespace = new { Namespace(" trace(rssItems[i].title); "); trace(rssItems[i].creator); public function NamespaceExample() trace(rssItems[i].date); 7 RSS has several standard XML types. trace(rssItems[i].link); You are going to extract the following: trace(rssItems[i].description); title, creator, date, link and description. Each of these items will be formatted in } accordance to the namespace called } RSS. You will see in the third line of the ActionScript that you reference the RSS 9 Click the Control menu, point to Test namespace. Movie, and then click Test to see the RSS feed results sent to your Output panel. private function parseRSS(rssXML:XML):Array Namespaces are an effective way to man- { age your control over XML data. As with all default xml namespace = rss; core classes in Flash, you can extend the var items:XMLList = rssXML.item; Namespace to use it in conjunction with other objects and data types. var arr:Array = new Array(); var len:uint = items.length(); Chapter 15 Working with ActionScript 3.0 379
Đồng bộ tài khoản