AS3 the cumulative second: from AS1 and AS2 to AS3 concept change

Source: Internet
Author: User
Tags constructor file system relative
http://as3blog.com/as3/as3tip-new-philosophy/

The change of AS1/2-AS3 concept (Meet with new philosophy)
For AS1, AS2 development model, flexibility is the biggest advantage. However, flexibility creates instability and disorder. This is taboo for the development of complex, long-term projects. On the (as1/2/1+2) flexible and durable (AS3) balance, I personally think it can be understood as "the fish and bear cake cannot have both", but I hope has been accustomed to AS1, AS2 friends do not put this conclusion too pessimistic.

AS3 is purely object-oriented, and I think it's more agile than the AS2 of the past. Even with more constraints, creating multiple helper Classes (helper Class) directly within package is a good message. That being the point, I feel that at least compared with clumsy AS2, AS3 's development efficiency will not be much discount. What we need is just grammar, habit, especially the change of idea. Of course, it takes time. As a long-time as1/2, I have encountered many problems and doubts in the process of transition to AS3. But I am happy to share with you the experience of what I have gained and how I have changed my mind.

ActionScript programming has been colorful since the day it was invented. Technology, especially the technical system of the Adobe product line, is not a rigid "pedantic system". I hope that my "wordy" can give you a more relaxed attitude.

To start with, let's talk about the biggest puzzles I've encountered in AS1/2 (1+2) into AS3:
start (how, and especially where, to get start)-friends who have played StarCraft must know that for different maps, such as Lost Temple and Wcg-groky Park (originally WCG has an island off, I forgot), all have their own classic, popular way to start. From the AS1/2 to the AS3, nothing but the transition from the lost temple to the Wcg-groky Park, you may have to take the gas mine first, build the Air Force, in order to develop smoothly.

In fact, Flash from AS1 to AS3, also have their own fixed, popular way to start.
For those accustomed to programming with AS1, the start of a flash is very flexible: you have a long timeline to use as soon as you enter Flash. You often need a loading, you can use 1-5 frames to do a loading first (remember the popular FLASHMTV production tutorials n years ago?) You can also take a frame, put a loading movieclip, and then write a onenterframe on the MovieClip to monitor the progress of the SWF file loading (which I'm passionate about). Next, you can deploy the program interface in the second frame or the nth frame. MovieClip's powerful frame API gives you the flexibility to do a lot of interesting logic (gotoAndPlay, gotoAndStop, Prevframe, etc.). Programming time can also be very arbitrary to find their own resources to control, I now remember just contact as when, a _root a _global, once let me always tried. Every time you encounter a problem, use these two things to solve.
AS2 's start is actually no change in nature, at least I think so. The only progress is more thorough than the AS1 OOP, module encapsulation. There are even a few setbacks, such as exclusively based on the Movieclip+attachmovie model, still easily lead to Run-time (Run-time) inefficiencies, and the concept of development is blurred. Because the library set up Linkage,new is clearly their own class,attach or MovieClip.
So a lot of people adopt as1+2 way, this is also I like. Now think of it, or more flexible and fast.
But in AS3, you seem to be in the dark. Flexbuilder has no timeline. Even with the "déjà Vu" FlashCS3 IDE, AS3 does not support the code on MovieClip and button. It's not easy to use "Onrelease=functioin" on a frame. Search the Internet for a tutorial, often get the following wording:
Aw.addeventlistener ("click", Fun);
function Fun (e:event) {trace (1);}
Really let the habit of AS1, 2 of the friends are depressed.

On the one hand see people with AS3 design out of the wonderful demo envy unceasingly, on the one hand to the program entrance touch irrelevant. This embarrassment I think is not to read one or two tutorials can be solved.

We need to "be reformed" and we need to "forget the past" (try to forget the past). Be bold enough to tell yourself that Onrelease=function has not only been "killed", but is not a good way to spell it, even if you still think it looks so pleasing. Boldly tell yourself that in AS3, all variables and functions belong to the class (the object's properties and methods) and no longer belong to the timeline, frame, even if the two lines listed above can be written on the timeline.

I personally suggest that the traditional AS1/2 programmer from the Flash CS3 IDE to start AS3, more appropriate. Because the Flash CS3 's entry (start) is very clear: document class (Documentation classes).

Run FLASHCS3, open the FLA file, find "Document Class" in the properties panel below the IDE, and fill in a name (because it is the class name, preferably the first letter, such as Mymainclass). It then creates an as file with the same name as the folder where the FLA file resides. Of course, you can also separate the FLA from the class file, which requires you to set the Classpath (File-publish settings-actionscript version:settings). The classpath can be entered below. I personally suggest that you enter a relative path. Relative, which means the relative current FLA file, the path, which is the folder in our computer file system. Do not write Dead "x:\xxx" is to allow the project to run in different environments, but also to better support the development of many people. The relative path is written using the "." Represents the current path, using the ".." Represents an upper-level path. For example, you can write:
"./classes/" or ".. /classes/".
Here to add, my advice is to put the original file together, the output of SwF placed in a different directory (usually called "bin"). The output directory is under the "Format" tab in the Panel, you can put the original file in the directory "src", and then set the SWF format file name to "... /BIN/SOMEFILE.SWF, it is recommended to export only SWF. HTML is better written by yourself.
Don't look at me. This is a lot of space to talk about these settings, but they are really good for standardizing your development habits and development concepts. Let you imperceptibly accept the "separation" thought in AS3 's philosophy.

Supplemental content: About the Flash CS3 class path setting, if you want to design your own package. Then you need to put the package under classpath instead of setting the package folder to Classpath.

To cut to the chase, we start:
First, the establishment of document class (documents Class)
Now we can start building the document class. Flash CS3 easily provides an "edit icon" that you can easily open to the class file. Recall that the last article mentions writing about classes: Each class should be in a package. I personally understand that the document class should be in a separate, generic "package", i.e.:
Package
{
Import Flash.display.Sprite;
public class Mymainclass extends Sprite
{
Public Function Mymainclass ()
{
Init ();
}
Private Function init ()
{
Do STH
}
}
}
We can even use some help classes
Class mymainclasshelper{}
Here, we have a successful "start".
Note that this document class must be public. The auxiliary class is not defined as public, private, and must be internal. The document class must inherit from Sprite or MovieClip. Because this document class represents this SWF, it is clear that SWF is a base container that needs to render the display on the screen (flash.display.DisplayObject) and provide resource-carrying capacity (flash.display.InteractiveObject).

Second, the logical start (Initialize the logic)
All of our logical portals start with the constructor of this class. AS3 's loading has some trouble, we skip it for the time being (we'll introduce it later).
Constructors generally need to be concise, and you may want to start with the popular Init method, which is to call an init function within the constructor. Remember, in the AS3, "_root" is dead, here is the traditional sense of "_root". You see this class (document Class), the first reaction should be this SWF file (as you originally saw "_root" should be reflected in the SWF file)! Here we can find many of the resources we need, such as we can find through the Loaderinfo:loaderinfo attribute (inherited from Displayobject), get the external parameters: Xxx.swf?somevar=1 came in the "Somevar", You can also perform various operations on the original stage class by using the Stage:stage property (inherited from Displayobject). I can also use the Contextmenu:contextmenu property (inherited from Interactiveobject) to control the contents of the Flash right-click menu.
All of this is done in the document class Init and other owning methods. All other functions can be encapsulated into other classes, packages for "modular" calls.

Iii. event mechanism (the new event system)
The courage to get used to the new event mechanism, I think, is quite a start. I used to be interested in xxx_mc.onrelease = function () {}, and have done more than n such projects. But when I really started using addeventlistener, I realized how elegant it was. Where is elegance:

    1. Unity : Only AddEventListener, no AddListener, no on (...), code can be placed uniformly.
    2. Clear : The event handler function as a class method (Methods) is enumerated clearly, imagine a onxxx after the assignment function placed in the code when many are hard to find.
    3. informative and accurate information : The new event mechanism conveys the information of the event to the receiving party by passing it to the receiver; function (method) and class tied to death, delegate can finally retire honorably.

Iv. Summary

    1. Accept the new OOP development system: Class/Object (Class/object) + constructor (constructor) + Member properties (properties) + Member method (methods), in addition to these things, ActionScript has no other form! Forget the timeline and the code on the instance.
      We want to "hug" the concept of Class! Everything in the AS3 revolves around the concept of a class. SWF is a class, with flex development, called application, with CS3, called document class (often inherited from Sprite). Any variable (property) belongs to a class, MovieClip has a member property Currentscene, it is an instance of the scene class, Sprite has member properties ContextMenu and stage, inherits from the Dispatchobject class, are instances of the ContextMenu class and the stage class, respectively.
    2. Without _root, all the _root-related operations are encapsulated into a member function (method) in the document class. _root is no longer the representative of the SWF, but instead is the document class (or application class in Flex)
    3. Functional modularity, Separation:
      Do not put everything into the document class, even if you can define a lot of auxiliary classes, after all, independent of the file easier to manage, integrate, reuse. and the interface (graphics, animation) and code are separated (in fact AS3 's document class and Addchild's content management system helped us do this). Whether you're alone. Code + Design or there is a team of collaborative division programming and animation. The benefit of separation is to make your features more powerful and maintainable.


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.