學習ActionScript3-day01

ActionScript 語文新特性

AtionScript 其它其它編程語言一樣需要編譯生成字節碼,給AVM 虛擬機執行

Run-time exceptions 運行時異常
ActionScript 3.0 reports more error conditions than previous versions of ActionScript. Run-time exceptions are used for common error conditions, improving the debugging experience and enabling you to develop applications that handle errors robustly. Run-time errors can provide stack traces annotated with source file and line number information, helping you quickly pinpoint errors.
Run-time types  運行時類型
In ActionScript 2.0, type annotations were primarily a developer aid; at run time, all values were dynamically typed. In ActionScript 3.0, type information is preserved at run time, and used for a number of purposes. Flash Player and Adobe AIR perform run-time type checking, improving the system’s type safety. Type information is also used to represent variables in native machine representations, improving performance and reducing memory usage.
Sealed classes   封閉類
ActionScript 3.0 introduces the concept of sealed classes. A sealed class possesses only the fixed set of properties and methods that were defined at compile time; additional properties and methods cannot be added. This enables stricter compile-time checking, resulting in more robust programs. It also improves memory usage by not requiring an internal hash table for each object instance. Dynamic classes are also possible using the dynamic keyword. All classes in ActionScript 3.0 are sealed by default, but can be declared to be dynamic with the dynamic keyword.
Method closures  方法關閉
ActionScript 3.0 enables a method closure to automatically remember its original object instance. This feature is useful for event handling. In ActionScript 2.0, method closures would not remember what object instance they were extracted from, leading to unexpected behavior when the method closure was invoked. The mx.utils.Delegate class was a popular workaround, but it is no longer needed.
ECMAScript for XML (E4X)   XML解析的支持
ActionScript 3.0 implements ECMAScript for XML (E4X), recently standardized as ECMA-357. E4X offers a natural, fluent set of language constructs for manipulating XML. In contrast to traditional XML-parsing APIs, XML with E4X performs like a native data type of the language. E4X streamlines the development of applications that manipulate XML by drastically reducing the amount of code needed. For more information about the ActionScript 3.0 implementation of E4X, see “Working with XML” on page 223.
To view ECMA’s E4X specification, go to www.ecma-international.org.

Regular expressions  正則表達式的支持
ActionScript 3.0 includes native support for regular expressions so that you can quickly search for and manipulate strings. ActionScript 3.0 implements support for regular expressions as they are defined in the ECMAScript (ECMA-262) edition 3 language specification.


Namespaces   命名空間
Namespaces are similar to the traditional access specifiers used to control visibility of declarations (public, private, protected). They work as custom access specifiers, which can have names of your choice. Namespaces are outfitted with a Universal Resource Identifier (URI) to avoid collisions, and are also used to represent XML namespaces when you work with E4X.


New primitive types  新增其本類型  int uint
ActionScript 2.0 has a single numeric type, Number, a double-precision, floating-point number. ActionScript 3.0 contains the int and uint types. The int type is a 32-bit signed integer that lets ActionScript code take advantage of the fast integer math capabilities of the CPU. The int type is useful for loop counters and variables where integers are used. The uint type is an unsigned, 32-bit integer type that is useful for RGB color values, byte counts, and more.


Flash Player API features  Flash 播放API的增強 
The Flash Player APIs in ActionScript 3.0 contain many classes that allow you to control objects at a low level. The architecture of the language is designed to be more intuitive than previous versions. While there are too many new classes to cover in detail here, the following sections highlight some significant


DOM3 event model DOM事件模型
Document Object Model Level 3 event model (DOM3) provides a standard way of generating and handling event messages so that objects within applications can interact and communicate, maintaining their state and responding to change. Patterned after the World Wide Web Consortium DOM Level 3 Events Specification, this model provides a clearer and more efficient mechanism than the event systems available in previous versions of ActionScript.
Events and error events are located in the flash.events package. The Flash components framework uses the same event model as the Flash Player API, so the event system is unified across the Flash platform.


Display list API  UI 組件的改進   Sprite  Shape 
The API for accessing the Flash Player and Adobe AIR display list—the tree that contains any visual elements in the application—consists of classes for working with visual primitives.
The new Sprite class is a lightweight building block, similar to the MovieClip class but more appropriate as a base class for UI components. The new Shape class represents raw vector shapes. These classes can be instantiated naturally with the new operator and can be dynamically re-parented at any time.
Depth management is now automatic and built into Flash Player and Adobe AIR, rendering assignment of depth numbers unnecessary. New methods are provided for specifying and managing the z-order of objects.


Handling dynamic data and content  處理動態數據
ActionScript 3.0 contains mechanisms for loading and handling assets and data in your application that are intuitive and consistent across the API. The new Loader class provides a single mechanism for loading SWF files and image assets and provides a way to access detailed information about loaded content. The URLLoaderclass provides a separate mechanism for loading text and binary data in data-driven applications. The Socket class provides a means to read and write binary data to server sockets in any format.


Low-level data access  低級別的數據訪問
Various APIs provide low-level access to data that was never before available in ActionScript. For data that is being downloaded, the URLStream class, which is implemented by URLLoader, provides access to data as raw binary data while it is being downloaded. The ByteArray class lets you optimize reading, writing, and working with binary data. The new Sound API provides detailed control of sound through the SoundChannel and SoundMixer classes. New APIs dealing with security provide information about the security privileges of a SWF file or loaded content, enabling you to better handle security errors.

 

Working with text  文件數據的處理
ActionScript 3.0 contains a flash.text package for all text-related APIs. The TextLineMetrics class provides detailed metrics for a line of text within a text field; it replaces the TextFormat.getTextExtent() method in ActionScript 2.0. The TextField class contains a number of interesting new low-level methods that can provide specific information about a line of text or a single character in a text field. These methods include getCharBoundaries(), which returns a rectangle representing the bounding box of a character, getCharIndexAtPoint(), which returns the index of the character at a specified point, and getFirstCharInParagraph(), which returns the index of the first character in a paragraph. Line-level methods include getLineLength(), which returns the number of characters in a specified line of text, and getLineText(), which returns the text of the specified line. A new Font class provides a means to manage embedded fonts in SWF files.

 

變量和常量  Variables and constants
    var value1:Number;
    var value2:Number = 17;
    const SALES_TAX_RATE:Number = 0.07;
數據類型  Data types
   基本數據類型
    •String: a textual value, like a name or the text of a book chapter
    •Numeric: ActionScript 3.0 includes three specific data types for numeric data:
    •Number: any numeric value, including values with or without a fraction
    •int: an integer (a whole number without a fraction)
    •uint: an “unsigned” integer, meaning a whole number that can’t be negative
    •Boolean: a true-or-false value, such as whether a switch is on or whether two values are equal
  複雜數據類型
    •MovieClip: a movie clip symbol
    •TextField: a dynamic or input text field
    •SimpleButton: a button symbol
    •Date: information about a single moment in time (a date and time)
下面的說法正確的
    •The data type of the variable myVariable is Number.
    •The variable myVariable is a Number instance.
    •The variable myVariable is a Number object.
    •The variable myVariable is an instance of the Number class.
ActionScript 中對像的三個組成部分:
    •Properties
    •Methods
    •Events
屬性的訪問
    square.x = 100;
    square.rotation = triangle.rotation;
    square.scaleX = 1.5;
方法的調用
    shortFilm.play();
    shortFilm.stop();
    shortFilm.gotoAndStop(1);
Event  Events are the mechanism that determines which instructions the computer carries out and when.
事件機制確定計算在什麼時候執行什麼指定
事件三個組成部分:
    事件源    觸發事件的實例
    事件    確定將要發生的操作以及希望得到的響應
    事件響應/事件監聽器   當事件發生時需要執行的操作
    function eventResponse(eventObject:EventType):void{
        //執行操作
    }
    eventSource.addEventListener(EventType.EVENT_NAME, eventResponse);
    •First, the name of the specific event you want to respond to. Once again, each event is affiliated with a specific class, and that class will have a special value predefined for each event—sort of like the event’s own unique name, which you should use for the first parameter.
    •Second, the name of your event response function. Note that a function name is written without parentheses when it’s passed as a parameter.
    function eventResponse(event:MouseEvent):void
    {
        // Actions performed in response to the event go here.
    }
    myButton.addEventListener(MouseEvent.CLICK, eventResponse);
    每個事件源可能有多個事件監聽器在監聽,在實施過程中事件源會把維護一個自己的事件監聽器列表,供監聽不同事件使用
    在用戶單擊事件源(Button)時,1.Flash Player 會創建事件對應的一個實例,該實例包括一些事件發生的信息:
    2.Flash Player 會在監聽器列表中尋找對應的監聽器,調用監聽方法

    function updateOutput(event:TextEvent):void
    {
    var pressedKey:String = event.text;
    outputText.text = "You typed: " + pressedKey;
    }
    entryText.addEventListener(TextEvent.TEXT_INPUT, updateOutput);
   
    function gotoAdobeSite(event:MouseEvent):void
    {
    var adobeURL:URLRequest = new URLRequest("http://www.adobe.com/");
    navigateToURL(adobeURL);
    }
    linkButton.addEventListener(MouseEvent.CLICK, gotoAdobeSite);

    創建對偶的實例
    var someNumber:Number = 17.239;
    var someNegativeInteger:int = -53;
    var someUint:uint = 22;
   
    var firstName:String = "George";
    var soliloquy:String = "To be or not to be, that is the question...";
   
    var niceWeather:Boolean = true;
    var playingOutside:Boolean = false;
   
    var seasons:Array = ["spring", "summer", "autumn", "winter"];
   
    var employee:XML = <employee>
    <firstName>Harold</firstName>
    <lastName>Webster</lastName>
    </employee>;
    For any other data type, to create an object instance you use the new operator with the class name, like this:
    var raceCar:MovieClip = new MovieClip();
    var birthday:Date = new Date(2006, 7, 9);

    Note that even for those data types that let you create instances using a literal expression, you can still use the new operator to create an object instance. For instance, these two lines of code do exactly the same thing:
    var someNumber:Number = 6.33;
    var someNumber:Number = new Number(6.33);




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章