Flash: Tips for learning ActionScript

Source: Internet
Author: User
ActionScript 3.0 is a powerful object-oriented language that depicts a new programming model for Flash Player. If you are already familiar with ActionScript 1.0 or 2.0, then when you use ActionScript 3.0 to develop your first application Program You need to know some language differences.

Note: For more information, see the list of differences between ActionScript 2.0 and ActionScript 2.0 in flex 3.0 language reference.

To help you more easily transition to ActionScript 3.0, I edited the following list, including tips and common problems you may encounter during development.

All variables, parameters, and return value declaration types.For all variables, parameter and return value declaration types are not mandatory, but they are considered to be the best habit. It will help the compiler give you more useful error information. It also enhances the running performance, because the virtual machine will think that you know your work in advance. In fact, it is so important that we will give a warning if you do not declare it.

Note that all declarations without access methods are in the internal mode by default, rather than public mode.Now the default access method is internal rather than public, which means that this Declaration is only visible to the package containing this declaration, not to allCodeAre visible. This is consistent with other languages, such as Java. Because the declaration of actionscript2.0 is public by default, this change may cause common errors, so always add the access method before the Declaration to make your intention clearer. To encourage this habit, The ActionScript 3.0 compiler will give a warning when there is no access method declaration.

Note that all classes are sealed by default, that is, attributes cannot be dynamically added at runtime.The current class can be dynamic or encapsulated ). The dynamic (dynamic) class can add dynamic attributes at runtime; The encapsulated class cannot. Because the internal hash table is not required to store dynamic attributes, the encapsulation class occupies less memory, and the compiler can provide better error feedback for it. Class foo is encapsulated. To declare a dynamic (dynamic) class, use the dynamic Keyword -- for example, dynamic class Foo.

Use the package statement to place a class in a package.Package is a new keyword in ActionScript 3.0.
Code 2.0:

Program code class MX. Controls. Button {...}

Code 3.0:

Program code package MX. Controls {class button {..}}

In ActionScript 2.0, a public class must be placed in a file with the same file name and class name. Multiple classes may be declared in the same file, but only one class may be public, and the class name must be the same as the file name.

Import class, even if the reference to this class is completely legal.To use the mypackage. myclass class, you must import it:

Program code import mypackage. myclass;

You must make sure that all references are completely legal and use the full name mypackage. myclass. In ActionScript 3.0, the import statement indicates that you want to use a class from another package. In ActionScript 2.0, it is only used to help remember the class name. In ActionScript 3.0, the complete class name is only used to eliminate ambiguity and is no longer a substitute for the Import Statement.

You can also use the * wildcard to import all classes in a package. Program code import mypackage .*;

A single import class is considered a good habit, because it clearly shows which class your code uses.

Always Mark Method overwriting.The override keyword can help avoid common errors of overwriting methods. For example, an error occurs when an overwriting method is defined with an incorrect name or the name of the overwriting method is changed. This will make the code containing the overwriting method clearer. Because the compiler can know whether a method tries to override another one, it can perform better checks. The override keyword in ActionScript 3.0 is inspired by the override keyword in C.

Define the return type in the function.Defining the return type for a function is a good habit. If you ignore the return type, a warning is displayed. This is for type security, so that you will not accidentally get the default object return type without writing the return type. If a function does not return any value, declare its return type as void.

Note that delegate is now defined in a language, making event dispatch easier.In ActionScript 2.0, to point an event to a method, you need to use the MX. utils. Delegate class or other work:

Program code import MX. utils. Delegate;
Mybutton. addeventlistener ("click", delegate. Create (this, onclick ));

In ActionScript 3.0, a reference to a method automatically remembers the object instance it references. This is called method closure. In essence, this is an automatic delegate. Therefore, the code can be written as follows:

Mybutton. addeventlistener ("click", onclick );

Releasing a null or undefined reference will throw an exception.Releasing a null or undefine reference is ignored in the previous ActionScript and the definition reference is undefined. Now, a typeerror exception will be thrown. Guard against unintentional release of null or undefined references and rely on this report error feature. This new throw exception is consistent with the ecmascript definition.

Use the-verbose-stacktraces and-Debug options.Use the command line option-verbose-stacktraces and-Debug to compile the file name and line number in the Flash Player warning box. When a runtime error occurs, a dialog box will describe this error and list the call stacks where it occurs. Use the-verbose-stacktraces and-Debug options to enableSource codeIt is easy to locate errors.

Explicitly declare the Bindable attribute.The default attribute is Bindable. You must use the [Bindable] metadata tags to declare them as Bindable.

Note that the Flash Player API is reorganized into the package.In the past, all classes and functions of Flash Player apis were global. Now we have many packages, such as flash. display, Flash. Events, Flash. UI, and others. For example, movieclip is now flash. display. movieclip and both gettimer and setinterval are put into the Flash. utils package.

Use the new Timer class instead of setinterval/setTimeout.Compared with the setinterval/setTimeout function, the new Timer class provides a clearer mechanism for timer events. The new Timer class has many advantages over the setinterval method. For example, you do not need to process the ID number interval. In addition, there is a better object-oriented interface. It is a good habit to use timer instead of setinterval and setTimeout.

Confirm that the event is inherited. Currently, events are strongly typed and must be subclasses of the new event base class. The new event class makes the event system clearer and more effective. However, this also means that you can no longer use the common instance of the object class when dispatching events, and you cannot use the abbreviated text of the object-for example, {type: 'customcustomout '}.

Instead of creating a common object class, you need to use the event class (for example, dispatchevent (new event ('mycustomventtype. If you want to pass additional attributes, You need to inherit from the event. The motive for not using objects is to achieve higher type security and efficiency.

Note that visible elements must inherit from displayobject, And you can define them like other classes.The component is now dynamically created through new and added to the display list using addchild. Therefore, createchild is not encouraged. Visualized entities, including textfield, can be instantiated like other objects and simply added to the display list using addchild or addchildat. Note: this means that some APIs have disappeared, such as createemptymovieclip and createtextfield. You can use new textfield instead of createtextfield to create a new textfield.

We recommend that you use E4X (ecmascript for XML) to operate XML in flash.E4X is more powerful than the previous flash XML class and better integrated into the language, it also provides a lot of new performance. The previous flash XML class can still be used. If you like to use the previous xml api, it is in the Flash. xml package. It can still be renamed as xmldocument.

Use the toxmlstring method when using E4X.The tostring method does not return the complete XML mark for the object. If you want to obtain it, use the toxmlstring method. The tostring method returns a simple string for the XML object. It does not fully serialize XML objects. To obtain the XML tag, call the toxmlstring method.

For ...... The in loop will not enumerate the attributes or methods declared in the class.It only lists the dynamic attributes of an object. The WordPress 3.0 feature provides a new advanced mechanism for object introspection called describetype. Use it in ActionScript 3.0 from a province object.

Currently, the root object of a SWF file can be an instance of a custom class you choose.In ActionScript 2.0, the root object of a SWF file is always an instance of movieclip. In ActionScript 3.0, it can make any subclass of Sprite. You can set a class as the DocumentRoot of a SWF file. When it is loaded, the SWF file will instantiate it and use it as its root object.

We are particularly grateful to our development team for this article.ArticleMake suggestions. The list here is by no means detailed, but it is a starting point and can help you lay a good foundation for learning ActionScript 3.0. If you are familiar with other object-oriented languages, you may find that these tips are just a review-Review of the skills you have learned elsewhere and can be used immediately in ActionScript 3.0.

If you are a newbie to ActionScript 3.0 and object-oriented programming, these tips will come in handy. For you, this is a simple list that can help you consolidate your knowledge. Happy coding!
Transferred from http://www.cnflex.cn/html/as/20070320/73.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.