学习ActionScipt的一些提示 ActionScipt learning some tips
08月7th, 2006 — Dreamer 08月7 th, 2006 - Dreamer 英文原文:《Tips for learning ActionScript 3.0》 English text: "Tips for learning ActionScript 3.0"
原文地址:http://www. adobe .com/devnet/actionscript/articles/actionscript_tips.html Original address: http://www. Adobe .com / devnet / actionscript / articles / actionscript_tips.html
译者:Dreamer。 Translator: Dreamer.
学习ActionScipt的一些提示 ActionScipt learning some tips
ActionScript 3.0是一种强大的面向对象语言,它为Flash Player描绘了一种新的编程模型。 ActionScript 3.0 is a powerful object-oriented language, it depicts Flash Player for a new programming model. 如果你已经对ActionScript 1.0或2.0很熟悉,那么当你使用ActionScript 3.0开发你的第一个应用程序的时候需要知道一些语言差别。 If you have ActionScript 1.0 or 2.0 is very familiar with, so when you use ActionScript 3.0 development of your first application when the need to know some language differences.
注:更多资料,请参考Flex 2.0 Language Reference中ActionScript 2.0 和ActionScript 3.0之间的不同点的列表。 Note: More information, please refer to Flex 2.0 Language Reference in ActionScript 2.0 and ActionScript 3.0 between the different points of the list.
为了帮助大家更容易地过渡到ActionScript 3.0,我编辑了下面的列表,其中包括技巧和你可能在开发中会遇到的普遍的问题。 In order to help people more easily transition to ActionScript 3.0, I edited the list below, including skills and you may be encountered in the development of a widespread problem.
• 为所有变量,参数和返回值声明类型。 • for all variables, parameters and return value type of statement. 为所有变量,参数和返回值声明类型不是必须的,但是它被认为是最好的习惯。 For all variables, parameters and return value type of statement is not necessary, but it is considered the best habits. 它将会帮助编译器给你更多地有用的错误信息。 It will help the compiler to give you more useful error message. 它还会增强运行时的性能,因为虚拟机将会认为你事先知道你所做的工作。 It will also enhance the run-time performance, because the virtual machine will know that you think you advance the work done. 实际上,它是如此重要所以如果你没有声明我们会给出一个警告。 In fact, it is so important if you do not declare we will give a warning.
• 注意现在没有访问方式的声明都默认是internal方式,而不是public。 • do not have to pay attention to the statement are the default mode is internal, not public. 现在默认的访问方式是internal而不是public,这就是说这个声明只对含有这个声明的包可见,而不是对所有代码都可见。 Now the default access is internal rather than public, that is the only statement on the package containing the statement that, instead of the code are visible to all. 这一点和其它一些语言相一致,比如JAVA。 This and other language in line, such as JAVA. 因为ActionScript2.0的声明默认为是public,这个变化将很可能造成普遍的错误,所以请总是在声明前面加上访问方式,让你的意图更加清楚。 ActionScript2.0 because the statement is the default for the public, this change would likely cause widespread mistakes, so please always in a statement ahead with visits to make your intentions more clearly. 为了鼓励这个好习惯,ActionScript 3.0编译器将在没有访问方式声明的时候给出一个警告。 In order to encourage the good habits, ActionScript 3.0 compiler will not access the time the statement is given a warning.
• 注意类都是默认为封装(sealed)的,也就是说不能在运行时动态地添加属性。 • attention are the default category for the package (sealed), that is not in the running to add dynamic attributes. 现在类可以是动态的(dynamic)或者封装的(sealed)。 Now type can be dynamic (dynamic) package or the (sealed). 动态(dynamic)类可以在运行时添加动态属性;封装(sealed)类不行。 Dynamic (dynamic) category can be added in the run-time dynamic attributes; Packaging (sealed) category not. 因为不需要内部哈希表来保存动态的属性,封装(sealed)类占用内存少,而且编译器可以对它提供更好的错误反馈。 Because of internal hash table does not need to preserve the dynamic attributes, Packaging (sealed) category occupy less memory and compiler it can provide better feedback error. Class Foo这种声明就是封装(sealed)的。 This statement is the Class Foo package (sealed) of. 要声明一个动态(dynamic)类,就使用dynamic关键字——比如,dynamic class Foo。 Have to declare a dynamic (dynamic) category, the use of dynamic keyword - for example, dynamic class Foo.
• 使用包(package)声明把一个类放到一个包(package)里面。 • the use of package (package) statement to a category into a package (package) inside. Package是ActionScript 3.0中一个新的关键字。 Package is a new ActionScript 3.0 in the keywords.
ActionScript 2.0 代码: ActionScript 2.0 code:
class mx.controls.Button { … } class mx.controls.Button (…)
ActionScript 3.0 代码: ActionScript 3.0 code:
package mx.controls { class Button { .. } } package mx.controls (class Button (..))
在ActionScript 2.0中,一个public类必须放在一个文件名和类名相同的文件中。 In ActionScript 2.0, a public categories must be placed on a file name and type of the same document. 多个类可能在同一个文件中声明,但是只有一个类可能是public,并且这个类的名字必须和文件名字相同。 At the same category may be more than a statement in the document, but only one category may be public, and the name of this type must be the same document name.
• 导入类,即使对这个类的引用都是完全合法的。 • import category, even if the use of this type are totally legitimate. 要使用MyPackage.MyClass类,你必须导入它: To use MyPackage.MyClass category, you have to import it:
import MyPackage.MyClass; import MyPackage.MyClass;
你必须这样做既是所有的引用都是完全合法的,并且要使用完整的名字MyPackage.MyClass。 You have to do so is the use of all are completely legitimate, and we have to use the full name MyPackage.MyClass. 在ActionScript 3.0中,import语句表明你想要使用一个来自另外一个包中的类,而在ActionScript 2.0中,它只是用来帮助记忆类名。 In ActionScript 3.0 in, import statements indicate that you want to use one from another type of package, and in ActionScript 2.0, it is only used to help memory class name. 在ActionScript 3.0中,完整的类名只是用来消除歧义,而不再是import语句的替代品。 In ActionScript 3.0, the full class name is used to eliminate ambiguities, rather than a substitute for the import statement.
也可以使用* 通配符来导入一个包中的所有类。 * You can also use wildcards to import a package of all types.
import MyPackage.*; import MyPackage .*;
单个地导入类被认为是一个好习惯,因为这样更明确地表明了你的代码使用了哪一个类。 To import a single category was considered a good habit, because it clearly shows that the more you use a code which category.
• 始终标记方法的覆写。 • always override the tag method. Override关键字可以帮助避免覆写方法时常见的错误,比如对一个被覆写得方法定义了错误的名字或者被覆写的方法的名字改变导致的错误。 Override keywords can help to avoid overwriting methods for common errors, such as the lining of a written definition of the wrong method or the name of the method was override the name change in the wrong. 这样做会使包含有覆写方法的代码看起来更清晰。 This will overwrite methods include the code seems more clearly. 因为编译器能够知道一个方法是否试图覆写另外一个,它就能执行更好的检查。 Because the compiler to know whether one way of trying to override another, it can better implementation of the inspection. ActionScript 3.0中的override关键字是从C#的override关键字中获得的灵感。 ActionScript 3.0 in the override keyword from C # override the keywords get inspiration.
• 在函数中定义返回类型。 • function in the definition of return type. 为一个函数定义返回类型是一个好习惯。 Back to function as a definition of the type is a good habit. 如果你忽略了返回类型,会出现一个警告。 If you ignore the return type, there will be a warning. 这是为了类型的安全性,这样一来你就不会无意中没有写返回类型而得到默认的Object返回类型。 This is to type of security, this way you will not inadvertently not been written to return to the type of Object to return to the default type. 如果一个函数没有返回任何值,将它的返回类型声明为void。 If a function did not return any value, it will return to the type of statement is void.
• 注意,delegate现在被定义到了语言里,使得事件分派更简单。 • attention, delegate now be defined by language, making distribution more simple incident. 在ActionScript 2.0中,把事件指向到一个方法需要使用mx.utils.Delegate 类或者其他的工作: In ActionScript 2.0, pointing to the incident need to use a method mx.utils.Delegate or other type of work:
import mx.utils.Delegate; import mx.utils.Delegate;
myButton.addEventListener(”click”, Delegate.create(this, onClick)); myButton.addEventListener ( "click", Delegate.create (this, onClick));
在ActionScript 3.0中,方法的一个引用会自动记住它引用的对象实例。 In ActionScript 3.0, a method used will automatically remember it cited examples of the target. 这被称为method closure。 This is called method closure. 本质上来说,这就是一个自动的delegate。 In essence, this is an automatic delegate. 所以,代码可以简单地写成: Therefore, the code can be simply written:
myButton.addEventListener(”click”, onClick); myButton.addEventListener ( "click", onClick);
• 现在解除一个null或者undefined的引用将会抛出一个异常。 • now lift a null or undefined references will be dished out an anomaly. 解除一个null或者undefine的引用在以前的ActionScript中会被忽略并且定义引用为undefined。 A lifting of the null or undefine cited in previous ActionScript will be ignored and the definition used for undefined. 现在,一个TypeError异常会抛出。 Now, a TypeError anomalies will be thrown out. 提防无意中解除的null或undefined引用,并且依靠这个报告错误的特性。 Beware of the null or inadvertently lifted cited undefined, and reliance on the report of the error. 这种新的抛出异常的特性与ECMAScript定义相符合。 This new dished out the unusual characteristics consistent with the definition of ECMAScript.
• 使用-verbose-stacktraces和-debug选项。 • Use - verbose-stacktraces and - debug options. 使用命令行选项-verbose-stacktraces和-debug编译可以使文件名和行号出现在Flash Player的警告框中。 The use of command-line options - verbose-stacktraces and - debug compiler can make the file name and line number in the Flash Player of the warning box. 当一个运行时错误发生时,一个对话框会描述这个错误并且列出它发生错误的地方的调用堆栈。 When a run-time error, a dialog will describe the error and presents it in the wrong place the call stack. 使用-verbose-stacktraces和-debug选项可以让在源代码中定位错误变得简单。 Use - verbose-stacktraces and - debug option allows the source code of positioning errors simple.
• 显式声明bindable属性。 • explicit statement bindable properties. 属性不再默认为bindable。 The default is no longer attribute bindable. 你必须使用[Bindable]元数据标签来声明它们为bindable。 You have to use [Bindable] metadata labels to their statement for bindable.
• 注意Flash Player API被重新组织到了包里。 • attention to Flash Player API to be re-organized the bag. 以前Flash Player API中所有的类和函数都是全局的。 Flash Player API before all of the categories and functions are the overall situation. 现在有了许多包,象flash.display, flash.events, flash.ui以及其它。 Now, with many packages, as flash.display, flash.events, flash.ui and others. 举个例子,MovieClip现在是flash.display.MovieClip并且getTimer 和setInterval都被放进了flash.utils 包中。 For example, MovieClip is now flash.display.MovieClip and getTimer and setInterval have been put into the flash.utils package.
• 使用新的Timer类,而不是setInterval/setTimeout。 • the use of the new Timer category, rather than setInterval / setTimeout. 与setInterval/setTimeout函数相比,新的Timer类为timer事件提供了更清晰的机制。 And setInterval / setTimeout function compared to a new category for Timer timer events provide a clear mechanism. 新的Timer类与setInterval方法相比有很多优势,比如不需要处理ID数字间隔,还有一个更好的,面向对象的接口。 New Timer category compared with the setInterval method has many advantages, such as ID numbers do not need to deal with intervals, there is a better, object-oriented interface. 我们把使用Timer而不是使用setInterval和setTimeout看作是一个好习惯。 We use Timer rather than using setInterval and setTimeout seen as a good habit.
• 确认事件(event)是继承而来的 。 • confirm the incident (event) is inherited from. 现在事件(event)是强类型的,并且必须是新的Event基类的子类。 Now events (event) is the type of strong, and must be new Event-kind of sub-class. 新的Event类让事件体系更加清晰和有效。 Event for the new category system events more clearly and effectively. 但是,这也意味着当分派事件的时候你不能再使用Object类的通用实例了,并且你不能使用对象的文字简写——例如,{type: ‘customEvent’ }。 However, this also means that when assigned the case when you can no longer use the Object example of the kind of generic, and you can not use the text abbreviated object - for example, (type: 'customEvent').
你现在需要使用Event类(例如,dispatchEvent(new Event (’myCustomEventType’))),而不是创建一个通用的Object类。 You need to use the Event category (for example, dispatchEvent (new Event ( 'myCustomEventType'))), rather than create a generic Object category. 如果你想要传递附加的属性就需要从Event继承。 If you want to transfer additional attributes required from the Event of succession. 不使用Object的动机是为了获得更高的类型安全性和效率。 Object not use the motive is to get the type of higher security and efficiency.
• 注意可视的元素必须继承自DisplayObject,并且你可以像其它类一样定义它们。 • attention to the visual elements must be inherited from the DisplayObject, and you can be the same category as the other definition of them. 组件现在通过new动态创建并且使用addChild被添加到显示列表中。 Now through the new dynamic components to create and use addChild be added to the display list. 所以,不鼓励使用createChild。 Therefore, do not encourage the use of createChild. 可视化的实体,包括TextField,可以像其它对象一样实例化并且使用addChild或addChildAt简单地添加到显示列表中。 Visualization of the entities, including the TextField, can be the same as other examples of objects and the use of addChild or addChildAt simply added to the display list. 注意,这意味着一些API消失了,比如createEmptyMovieClip 和createTextField。 Note that this means that some API disappeared, such as createEmptyMovieClip and createTextField. 你可以使用new TextField而不是使用createTextField来创建一个新的TextField。 You can use the new TextField createTextField rather than to create a new TextField.
• 现在在Flash中推荐使用E4X (ECMAScript for XML)来操作XML。 • now in Flash recommended E4X (ECMAScript for XML) to operate XML. E4X与以前的Flash XML类相比非常强大并且更好地整合进了语言中,它还提供了很多新的性能。 E4X Flash XML and the former category compared with very strong and better integrated into the language, it offers a lot of new properties. 以前的Flash XML类依然可以使用。 Flash XML former category can still use. 如果你喜欢用以前的XML API,它就在flash.xml包中。 If you like to use the previous XML API, it's flash.xml package. 它依然可用只是重新命名成了XMLDocument。 It remains available only to rename a XMLDocument.
• 当使用E4X的时候使用toXMLString方法。 • When using E4X when the method used toXMLString. toString方法没有为对象返回完整的XML标记;如果你想获得,使用toXMLString方法。 toString method does not object to return to full XML tags if you want to access, use toXMLString methods. toString方法为XML对象返回一个简单的字符串。 toString method for XML object to return to a simple string. 它并不会完全将XML对象序列化。 It will not be completely sequence of XML object. 要获得XML标记,请调用toXMLString方法。 To get XML tags, call toXMLString methods.
• For……in循环将不再枚举类中声明的属性或者方法。 • For…… in circulation will not enumerated in the Declaration of property type or method. 它只枚举一个对象的动态属性。 It is only an enumeration of the dynamic attributes of a target. ActionScript 3.0特性中有一个针对对象自省的新的先进机制,叫做describeType。 ActionScript 3.0 characteristics of a target self-examination of the new advanced mechanism, called describeType. 在ActionScript 3.0中使用它来自省对象。 ActionScript 3.0 in the use of its objects from the province.
• 现在一个SWF文件的根对象可以是你自主选择的一个自定义类的实例。 • now a SWF file is the root object you can independently choose a custom category of examples. ActionScript 2.0中,一个SWF文件的根对象总是MovieClip的实例。 ActionScript 2.0, a SWF file MovieClip always target the root of the case. 而在ActionScript 3.0中,它可以使Sprite的任意子类。 In ActionScript 3.0, it can make any sub-category of Sprite. 你可以设定一个类作为一个SWF文件的DocumentRoot。 You can set up a class as a SWF file DocumentRoot. 当其加载时,SWF文件会将它实例化然后将其作为自己的根对象。 When loading, SWF examples of the document it will then be used as the root of their targets.
特别感谢我们的开发团体为这篇文章提出建议。 Special thanks to our development groups to make recommendations for this article. 这里列出的决不是详尽的,但是它是一个起点,可以帮助你打好学习ActionScript 3.0的基础。 Listed here are not exhaustive, but it is a starting point, can help you lay a good foundation for the learning ActionScript 3.0. 如果你熟悉其它的面向对象的语言,你可能发现这些提示只是一个复习——复习你已经在别处学习到的并且可以立即在ActionScript 3.0中使用的技巧。 If you are familiar with other object-oriented language, you may find these tips is only a review - of literature you have to learn elsewhere in the immediate and ActionScript 3.0 in the use of skills.
如果你对ActionScript 3.0和面向对象编程是一个新手,那么这些提示将会派得上用场。 If you ActionScript 3.0 and object-oriented programming is a novice, then these tips will be sent a useless. 对你来说,这就是一个简单的可以帮你巩固知识的列表。 For you, this is a simple knowledge can help you consolidate the list. 编码愉快! Coding happy!
本文链接: http://www.zhuoqun.net/html/y2006/189.html 转载请注明出处,谢谢。 This link: http://www.zhuoqun.net/html/y2006/189.html reprint please reference, thank you.
TrackBack引用地址: http://www.zhuoqun.net/html/y2006/189.html/trackback TrackBack used Address: http://www.zhuoqun.net/html/y2006/189.html/trackback



