Flash AS3.0 Classes and bindings

Source: Internet
Author: User
Tags addchild inheritance new features

Author's blog: www.kingda.org

ActionScript 3.0 Series Tutorial (1): Have a close contact with Flash9 first!

ActionScript 3.0 Series Tutorials (2): AS3.0 classes and bindings

This time we begin to introduce how the classes in AS3.0 and the component bindings in the library, and the special document class design.

Total 4 steps:

1. Build a standard AS3.0 class (temporarily named KINGDAMC, what a great name, "famous")
2. Create a new component and set its linkage and the class bindings above.
3. Write code on the timeline, create n "famous" with AS3.0 code.
4. Delete the time axis code, use the Flash 9 new features document class on the stage to create N "famous." Just experience a hand.

(write to know to talk about the content of Ah, Halo, played two hours, tired, this section is devoted only to talk about AS3.0 class bar, the rest of the slowly speak)
Before you create AS3.0, allow me to pay tribute to the syntax and inheritance design of the AS3.0 class first. Fully compatible with the standard, better and stricter encapsulation features, especially the introduction of namespaces (name space). From today onwards, at the OOP level, AS3.0 has been on an equal footing with java,c#, even in some ways (such as name space) more interesting than Java.

Let me to AS2.0 veteran said a few words in the heart of the excitement of the language:
If AS2.0 is just outwardly close to the OOP standard language and is inherently a messy AS1.0 scripting language, then AS3.0, whether from the OOP design level or from the compiler level (for example, support for weak references--week reference--), evaluates to standard, authentic, Powerful language.

From the AS3.0, our ActionScript developers can straighten out their chests, we are the real industry standard level programmer.

Before I go on with the tutorial, I'm going to sing a song for everyone to hear:
AS3 's wise, is definitely not a sentence can be said clearly! ~~~~~~

Finish the call.

1. Establishment of AS3.0 class documents

What do you use a class file for? For example, we want an object to have a lot of functions, such as the MovieClip type, support drag, double-click, and so on. Then write the requirements and implementation methods in a class file, and then you can use this class to create many instances that all have these features. Write once, it can be used many times, how good. The most important thing is that it can also reuse a lot of code through inheritance, saving more time for the future.

Cut the crap, CTRL + N. Open the new window and choose to create ActionScript file, Ctrl+s, and staging as "kingdamc.as" files. (That is, the "famous" class file).

Enter the following code:

The code is as follows

Package See explain 1
Package {
Import Flash.display.MovieClip; Explanation 2
Import flash.events.MouseEvent;

Explanation 3
public class KINGDAMC extends MovieClip {
Public Function KINGDAMC () {
Trace ("Kingda Created:" + this.name);

This.buttonmode = true;
This.addeventlistener (Mouseevent.click, ClickHandler);
This.addeventlistener (Mouseevent.mouse_down, Mousedownlistener);
This.addeventlistener (mouseevent.mouse_up, Mouseuplistener);
}
Private Function ClickHandler (event:mouseevent): void {
Trace ("You clicked the Ball");
}
function Mousedownlistener (event:mouseevent): void {
This.startdrag ();
}
function Mouseuplistener (event:mouseevent): void {
This.stopdrag ();
}
}
}

Explanation 1: AS2.0 we use the full name to declare the class, popular said, including the path of the class in front of the class name. AS3.0 then extracts the path and puts it behind the package keyword. The class and FLA files in this example are in the same directory, so there is nothing behind the package. If the class file is in the Kingda directory under the org directory, write:

ActionScript 2.0
Class Org.kingda.KingdaMC {
}

ActionScript 3.0
Package Org.kingda {
public class KINGDAMC {}
}

You can define several classes in the package without having to write the full name. But I do not recommend it. A file a class is better managed.

Explanation 3:
The class also has a distinction between public and internal in AS3.0.
Public means that this class can be imported anywhere in use.
Internal says this class can only be used in the same package.
Do not write, it defaults to internal this new keyword.
Another attribute is final, which means that the class cannot be inherited, and the inheritance tree ends here.
Plainly, these three attributes are used to let us more standardized management of the relationship between the class, so that the future changes in the heart of the spectrum, greatly facilitate the modification.

At the same time, the design of the architecture is more demanding, the novice and the small project or more use public bar. The more later you will like internal. I just see internal and private these two keywords, the heart is extremely stable and comfortable. I think a lot of programmer and I will have the same feeling.

2. Create a new component and set its linkage and the class bindings above.

Like the first chapter of the tutorial, draw a box, press F8 into MovieClip, and then right click on it in the library, select "Linkage"

Write the KINGDAMC in class. Note that the ID input box has been revoked. Because in AS3.0, there is no Movieclip.attachmovie (), Movieclip.createemptymovieclip (), and Movieclip.createtextfield ().
All visible objects for the stage are created by new.
In this case, for example, Symbol1 binds KINGDAMC, so if I want to create a KINGDAMC on the stage, just write:

var B1:kingda = new KINGDAMC ();
AddChild (B1);

Can.

Remember the mess you used to create the syntax for movies and components? What Createclassobject (), Depthmanager createchildatdepth (), createclasschildatdepth (), etc. One of my Java colleagues just learned as, and was confused by the chaotic creation functions above. Everything is gone. Only Eminence New ClassName (), many standards, how comfortable ah, more studious ah.
Therefore, not learn AS2.0 Direct learning AS3.0 is definitely a beginner's blessing.

The second sentence, addchild this is very important.
It is not possible to have the first sentence new. That's just to tell Flash I built a name called B1 KINGDAMC to show, but didn't tell Flash when it was displayed.
When you hit Addchild (B1), Flash will show it on the stage. How, how simple.
In fact, here omitted a this. If you have a movieclip named Bigkingda and wish to add a KINGDAMC instance to this Bigkingda, write:

Bigkingda.addchild (B1);

Very simple.

Ctrl+enter test, found to create the KINGDAMC, supports drag and drop.
Let's try to create a few more KINGDAMC with the code, like using a for loop to build a 10 or 8 to play with.

Ok, tired, want to rest, next time speak document Class.

The code examples in this section refer to Jen DeHaan's tutorial , for the simple reason that her chosen example is a good illustration of binding and document Class, and black feather made a few Chinese changes. Her original source example points to this download .




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.