Flash actionscript knowledge: from AS2 to AS3

Source: Internet
Author: User
Tags constant control characters
constant operator Parameters

With the release of Flash CS3, it is estimated that many friends have started migrating from AS2 to AS3. But AS3 has changed so much more than AS2, many methods in the AS2 in the AS3 is not the same, or even disappear, and the current help document is E, now the AS3 basic article is not much, so special open this paste, set some basic points, as far as possible to illustrate the example, Let everyone move faster and better to AS3. If there are any mistakes in the article, please correct me. Interested friends can also be added together.

1, constant
First look AS2 code: VAR str:string;
var Num:number;
var Boo:boolean;
var obj:object;
var notyped;
Trace (str+newline+num+newline+boo+newline+obj+newline+notyped);
Return
Undefined
Undefined
Undefined
Undefined
Undefined

Look at the AS3 code again:

var str:string;
var Num:number;
var Boo:boolean;
var obj:object;
var notyped;
Trace (str+ "\ n" +num+ "\ n" +boo+ "\ n" +obj+ "\ n" +notyped);
Return
Null
NaN
False
Null
Undefined

It is clear from the above 2 examples that the default values for string, number, Boolean, Object are undefined in AS2, and null, NaN, FALSE, and Null in AS3. Only the default value of a variable that is not declared is undefined. Therefore, in the as2 of a "undefined" the era of conquering the world has become obsolete, in the AS3 more clearly. There is also a constant in the AS2 newline that has been canceled in AS3 and replaced by "\ n". Another special note is that the function of the trace method is also enhanced, the number of parameters is not limited, so the above can be changed to trace (str,num,boo,obj,notyped).

2. Operator
Here we only discuss instanceof. Although this operator is also retained in AS3, it is recommended to replace it with is in AS3. and is more flexible to use.
Code:

var mystr:string= "Test";
var mytest:test = new Test ();//test is extends SuperTest
Trace (mytest instanceof SuperTest); AS2 return False//AS3 return True
Trace (mystr instanceof String);//as2 return False
Trace (mystr instanceof String);//AS3 return True
Trace (MyStr is String);//AS3 return True

From this example, it can be seen that instanceof has changed in AS3 than in AS2. In AS2, instanceof does not convert the original type to a wrapper object, so it returns false in String validation in the example above, and it does not work on a superclass such as SuperTest. In AS3, it only checks the prototype chain, so it returns true in the first trace. However, instanceof does not support interfaces in AS3, and is supported.
Code:

var mytest:test = new Test ();//test implements Interfacetest
Trace (MyTest is interfacetest); AS3 return True
Trace (mytest instanceof interfacetest); AS3 return False

The syntax for add, EQ, GT, GE, <>, and, not, or, NE, lt, le, and so on is AS1 in the AS3.

3. Parameter
In the AS3, added a ... (rest) of such parameters. Its role is to indicate that a function can receive any number of comma-delimited parameters. AS3 Code:

function testfun1 (Param0, param1, ... arg)
{
Trace (Arg is array,arg,arg.length);
}
TESTFUN1 ("Param0", "param1", "param2", "param3");
Return:true PARAM2,PARAM3 2

Here ... arg represents an array named Arg. When run testfun1, arg=["param2", "param3"]. Remember the function has a arguments class, when the use of ... (rest), Argumnets is not available. However, Arg.length can also be used as shown in the previous example. However, there is no such method as Arguments.callee, so the Arguments.callee method is not used when using ... arg.

Access Control and namespaces

In AS2 we already know that there are public, private these 2 access control characters, in the AS3 added internal and protected, and the class, method access control more stringent, and more reasonable and convenient. In addition, namespace namespace is added to the AS3, so our access control is more flexible. (Public and private believe that we are already familiar with, so do not repeat) if there is a mistake, please correct me.

Internal indicates that classes, variables, constants, methods, and so on can be accessed at the package package level. You may ask about the difference between public and private. Public can only be used in package{}, but public-controlled classes or methods can be accessed anywhere, private only in class{}, and the classes or methods it controls can only be accessed in the appropriate class. Internal can be used outside of package{}, but it only works inside the as file. For example, the following ddd.as code is:
Package flashrek.example{
public class DDD {
function DDD () {
var e:eee=new EEE ();

}
}
}
Internal var internalstr:string= "Internalstr";
Class EEE {
function EEE () {
Trace (INTERNALSTR);
}
}

Then the following code to run the result is: import Flashrek.example.DDD;
var d=new DDD ();//output: INTERNALSTR
Trace (INTERNALSTR);//output: Error err

As you can see, class DDD can access class Eee outside of package (default is internal, so it can be omitted), and class Eee can also access variable INTERNALSTR. EEE and INTERNALSTR are not accessible anywhere else outside of the ddd.as file.

Unlike internal, protected can only be applied to class classes. It indicates that variables, constants, methods, and so on are accessible at the class classes level. The difference with private is that protected specified methods, variables, etc. are inheritable and can be accessed in subclasses.

Namespace namespace is also introduced in AS3. When you have some special-purpose methods distributed in different packet package, you want these methods to be available in all package, but you do not want to set these methods to public. This time, namespace will be able to achieve your goal.

First you have to define a namespace, (flashrek.as):

Package flashrek.example{
Public namespace flashrek= www.webjx.com;
}

The code in the Aaa.as file is then:
Package flashrek.example{
Import Flashrek.example.flashrek;
public class AAA {
function AAA () {
var b:bbb=new BBB ();
Flashrek::nstest ();
}
Flashrek function Nstest () {
Trace ("namespace test");
}
}
}
Classes, namespaces outside of package cannot be default and only internal, and can only be used within this package.
The Public keyword cannot be used outside the package.
Internal class BBB {
function BBB () {
Trace ("BBB");
var c:ccc=new CCC ();
}
}
Class CCC {
function CCC () {
Trace ("CCC");
}
}

Then look at the results of the operation: import Flashrek.example.AAA;
Import Flashrek.example.flashrek;
var a=new AAA ();
A.flashrek::nstest ();
Output:
Bbb
Ccc
Namespace test

From this we can see that through a.flashrek::nstest (), we can access the Nstest method, and other places cannot directly access this method.

In the previous example, it was found that namespace resembled a class. It's a bit like this: But namespace can be defined outside of package, class, and can be accessed using public, internal, and so on.
For example, remove the flashrek.as in the above example and change the aaa.as into the following:

Package flashrek.example{
public class AAA {
function AAA () {
var b:bbb=new BBB ();
Flashrek::nstest ();
}
Flashrek function Nstest () {
Trace ("namespace test");
}
}
}
Classes, namespaces outside of package cannot be default and only internal, and can only be used within this package.
The Public keyword cannot be used outside the package.
namespace flashrek= "Www.flashrek.com"
Internal class BBB {
function BBB () {
Trace ("BBB");
var c:ccc=new CCC ();
}
}
Class CCC {
function CCC () {
Trace ("CCC");
}
}

Here namespace Flashrek is defined outside of package and it can only be accessed at the package level. Therefore, it is wrong to use A.flashrek::nstest ().

A new loading (Loading) mechanism

Do a loading preload effect I believe we are already very familiar with, whether using onenterframe or setinterval, or more advanced moviecliploader. But when beginning to use AS3, even one of the simplest loading have no starting point. is the loader in AS3 more complex and difficult to control? It's not, actually. In AS3, a completely new class Loaderinfo is introduced, which can be used for any display object (display object) that contains load process detection, address loading, loading of objects, total bytes loaded (and number of bytes during loading). , loading the width of the object and so much content (too much content, more people can go to see Help).

There are 2 different ways to access the Loaderinfo object:
1 Accessing the Contentloaderinfo property of the Flash.display.Loader object;
2 any Visible object (Display object) has a loaderinfo attribute;

Note: an instance of the main class for each SWF document has the Loaderinfo attribute, each loader has a loaderinfo attribute, and it has a Contentloaderinfo property, through which you can access loaded of the Loaderinfo object. You can look at the following picture:

The following is the old habit, to example demonstration. First comes an example of an loading external file: var request:urlrequest = new URLRequest ("flashrek.swf");
var loader:loader = new Loader ();

Loader.contentLoaderInfo.addEventListener (progressevent.progress, loadprogress);
Loader.contentLoaderInfo.addEventListener (Event.complete, LoadComplete);

function Loadprogress (event:progressevent): void {
var percentloaded:number = event.bytesloaded/event.bytestotal;
percentloaded = Math.Round (percentloaded * 100);
Trace ("Loading:" +percentloaded+ "%");
}
function LoadComplete (event:event): void {
Trace ("Complete");
}

Loader.load (Request);
AddChild (loader);

Note here that the loader load method only accepts URLRequest objects as parameters, and Progressevent classes are simpler and easier to see.



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.