AS3.0 AS3 class files that contain multiple classes
The standard AS3 class file is a plain text file that ends with ". as". A class file can contain one or more class definitions and can even be written to a one-time executed statement. AS3, in the package curly braces, you can define one or more classes and do not have the same name as the file. The classes outside these curly braces, called the package outer classes of the current class file. Only members in the current class file can access it.
The generation of the outer package, each. As file by the compiler as a compilation unit, each compilation unit can only have an externally visible class, then package{} outside the class is not visible.
Why do I need to pack the outer class?
First, we can consider the package outer class as the "private class" of the current file. For example, some of the complex logic in the current class can be extracted individually to form a class, and if the extracted logic is only relevant to this current class and does not require external intervention, then these classes can be defined as package outer classes.
Second, because the package outside completely invisible to the outside, in the AS3 has its special use, for example, AS3 does not support private and protected constructors, in order to truly implement a single case pattern (Singleton), you can use the package outside the class.
The basic structure is as follows:
1.代码:
2.
3.package {
4.class MyClass {
5.function MyClass() {
6.var helper:MyHelper = new MyHelper();
7.}
8.}
9.}
10.class MyHelper {
11.function MyHelper() {
12.var helper:HelpersHelper = new HelpersHelper();
13.}
14.}
15.class HelpersHelper {
16.function HelpersHelper () {
17.}
18.}
Note: You can define at most one class in a package block. Auxiliary classes in the same file are not part of the package and can only be visible and used in this file.
Below we will rewrite our class into the Packge class form above. We write the following code in a documentclass.as file, and then enter the Documentclass class name in the document class entry box in the Properties panel in the FLA file.
1. Code:
2.
3.package {
4.import Flash.display.MovieClip;
5.import Flash.display.Sprite;
6.import flash.events.MouseEvent;
7.//Document Class
8.public class Documentclass extends MovieClip {
9.private var _circle:drag_circle;
10.private Const MAXBALLS:INT=100;
11.public function Documentclass () {
12.var I:int;
13.for (i=0 i <= maxballs; i++) {
14._circle=new drag_circle;
15._circle.scaley=_circle.scalex=math.random ();
16._circle.x=math.round (Math.random () * Stage.stagewidth-
17.
18._circle.width);
19._circle.y=math.round (Math.random () * Stage.stageheight-
20.
21._circle.height);
22.addChild (_circle);
23.}
24.}
25.}
26.}
27.import Flash.display.Sprite;
28.import flash.events.MouseEvent;
29.class Drag_circle extends Sprite {
30.private var _circle:sprite;
31.public function drag_circle () {
32._circle=new Sprite;
33._circle.graphics.beginfill (0xff0000);
34._circle.graphics.drawcircle ( -5,-5,10);
35._circle.graphics.endfill ();
36.addChild (_circle);
37.this.buttonmode=true;
38._circle.addeventlistener (Mouseevent.click,onclick);
39._circle.addeventlistener (Mouseevent.mouse_down,ondown);
40._circle.addeventlistener (Mouseevent.mouse_up,onup);
41.}
42.private function OnClick (event:mouseevent): void {
43.trace ("Circle clicked");
44.}
45.private function Ondown (event:mouseevent): void {
46._circle.startdrag ();
47.}
48.private function Onup (event:mouseevent): void {
49._circle.stopdrag ();
50.}
51.}
You can test your film.