Flash Object-oriented program design evolution
Note: The following quote from <<Flash ActionScript 2.0-RIA Applications>>, has been quoted the author: Luar permission. Involving copyright issues please contact me immediately erased. Job is reposted please indicate the source.
This section will be a particle system (particle system is in a large number of Movie Clip within the framework of random movement of the border will rebound when the encounter) for example, on Movie Clip object-oriented programming design changes.
1. Whole domain function
Development steps:
Ready for a first Movie Clip ball on behalf of particles, it will drag large stage, the importation of these names Movie Clip ball entities, such as the ball0 to ball14.


As particles to keep moving in order to keep the process moving responsible for the implementation cycle time frame must be shown continuously through a procedure frame for moving particles.

ActionScript more awareness of the artists, will be initialized and mobile procedures written function, or may be directly in paragraphs 1 and 2 the accession process. These functions, the path is the root _ as global function; now Most of Flash developers, the whole domain is still function method development and application procedures or program design interactive.
Good programming practices, the function is on the first one, in the accession process layer script code initBall () is the initialization function, at the beginning of each particle mobile perspective, and then divided into x and y velocity speed:
Code: function initBall ()
(
Var speed = 10;
For (var i = 0; i <ballno; i)
(
Var dir = random (360) * Math.PI/180;
This [ "ball" i]. Xdir = Math.cos (dir) * speed;
This [ "ball" i]. Ydir = Math.sin (dir) * speed;
)
)
Continue to join the code, program design mobile procedures function:
Code: function moveBall ()
(
For (var i = 0; i <ballno; i)
(
/ / Delete the use of common sub-expression increased speed of execution this var ball = [ "ball" i];
Var x = ball._x;
Var y = ball._y;
X = ball.xdir;
Y = ball.ydir;
If ((x <0 and ball.xdir <0) or (x> 292 and ball.xdir> 0))
(
/ / Will be encountered border rebound ball.xdir *= 1;
/ / Particle not crossed the border, the location of the border limit the size x = (x <0)? 0: 292;
)
Else if ((y <0 and ball.ydir <0) or (y> 292 and ball.ydir> 0))
(
/ / Will be encountered border rebound ball.ydir *= 1;
/ / Particle not crossed the border, the location of the border limit the size y = (y <0)? 0: 292;
)
Ball._x = x;
Ball._y = y;
)
)
In the first frame by adding variable ballno, representing the total number of particles; Finally, the call initBall () function to initialize to join the code.
Code: ballno = 15;
InitBall ();
In the second frame by adding code, call moveBall () function mobile particles:
Code: moveBall ();
In the third frame by adding code, jumped to the second cycle of broadcast frames to moveBall () function is invoked constantly:
Code: gotoAndPlay (2);
The whole function is to prepare the particles completed, the film can be tested.
Features of:
Global function is the most easy to learn programming skills, but it has the following three issues:
1. Object-oriented component in the process of the low, the object-oriented concept of little use in the graphic reached reuse requirements (all particles from the same entities are components of a Movie Clip).
2. Lack of Global Packaging function code and graphics will be separated when necessary reuse in other ad hoc, must be carefully selected code and the procedures necessary for the graphics, a complete lack of encapsulation.
3. Attention Movie Clip path global direct control function Movie Clip, assuming Movie Clip and the whole domain functions are in the same time frame (for example: the film), no path; But when the control is in another Movie Clip Movie Clip , the entire function is to amend controlled Movie Clip path, or the whole domain will be moved to the function of the time frame Movie Clip go.
2. Regional function as the regional function means that the original time frame of the film on the global move to object function, that is, on Movie Clip ball in the time frame.
Development steps:
Select the layer film script first to the third frame, right-click election "Cut Frames", as shown.


Code: function initBall ()
(
Var speed = 10;
Var dir = random (360) * Math.PI/180;
Xdir = Math.cos (dir) * speed;
Ydir = Math.sin (dir) * speed;
)
Function moveBall ()
(
Var x = _x;
Var y = _y;
X = xdir;
Y = ydir;
If ((x <0 and xdir <0) or (x> 292 and xdir> 0))
(
Xdir *= 1;
X = (x <0)? 0: 292;
)
Else if ((y <0 and ydir <0) or (y> 292 and ydir> 0))
(
Ydir *= 1;
Y = (y <0)? 0: 292;
)
_x = X;
_y = Y;
)
InitBall ();
Is amended by deleting the procedures for recycling, and the path by deleting (not their own path). Modified to test the film.
Features of:
Global and regional function of the different function from:
1. Procedures and graphics together, the package of improvements, easily copied to another use.
2. Path did not consider, in other Movie Clip, can be used.
However, regional function is a fatal problem: While regional function is written in the Movie Clip components, but each will have a Movie Clip regional entities function in their own time frame, and their function is the same region, so many regional Functions will be a serious waste of memory.
3. Prototype (Prototype) to expand because all the Movie Clip arena is a concrete manifestation of the MovieClip class (the only visual entities), in the MovieClip class directly joining the random movement on the way, all entities will have random particles Mobile call for the method to avoid wasting memory.
ActionScript 1.0 prototype is used to establish inheritance or custom attributes to objects and methods of use. Use of the keyword "prototype" for the MovieClip class two additional methods, namely initBall () and moveBall ().
Development steps:
Based on the above source code changes. Film Timelines retain only the first frame, additional layers script to include expansion of the MovieClip class method of procedure code (Movie Clip ball from the first frame reproduction):
Code: MovieClip.prototype.initBall = function ()
(
Var speed = 10;
Var dir = random (360) * Math.PI/180;
This.xdir = Math.cos (dir) * speed;
This.ydir = Math.sin (dir) * speed;
);
MovieClip.prototype.moveBall = function ()
(
Var x = this._x;
Var y = this._y;
X = this. Xdir;
Y = this.ydir;
If ((x <0 and this.xdir <0) or (x> 292 and this.xdir> 0))
(
This.xdir *= 1;
X = (x <0)? 0: 292;
)
Else if ((y <0 and this.ydir <0) or (y> 292 and this.ydir> 0))
(
This.ydir *= 1;
Y = (y <0)? 0: 292;
)
This._x = x;
This._y = y;
)
The code should be amended little, but with the keyword "this", the representative at their own properties.
There components Curry Movie Clip ball, retaining only the first frame, the code will be changed:
Code: initBall ();
This.onEnterFrame = moveBall;
Movie Clip onEnterFrame is one incident, it will be under video frame rate (Frame Rate) kept implementation procedures. Modified to test the film.
Features of:
Although expansion of the prototype to avoid duplication of regional waste of memory function, but because it directly in the MovieClip on the accession of random mobile and so on, polluted MovieClip class, because all the arena Movie Clip entities, if not the particles, which will have two alternatives.
4. Packaged as components for the prototype expansion shortcomings, the right approach is to create a new object (for example: particle), and the succession MovieClip class. This practice in the Flash MX, is the so-called ActionScript 1.0 for object-oriented programming design approach. fact, the development of this approach with Flash MX components is no difference, in other words, is the development of Flash MX components ActionScript 1.0 Object-Oriented Programming common situation.
Development steps:
Based on the above revised source. Copy of the film script first frame Timelines layer of code, and then delete layers script. Edit components Curry Movie Clip ball layers script of the first frame, there are two lines of the old code deleted, the film just paste code, and then the code for procedures such as the following modifications:
Code: # initclip
/ / Construction函å¼
BallClass = function ()
(
This.initBall ();
);
/ / Succession MovieClip class
MovieClip.prototype = new MovieClip ();
/ / Definition method
BallClass.prototype.initBall = function ()
(
Var speed = 10;
Var dir = random (360) * Math.PI/180;
This.xdir = Math.cos (dir) * speed;
This.ydir = Math.sin (dir) * speed;
This.onEnterFrame = this.moveBall;
);
BallClass.prototype.moveBall = function ()
(
Var x = this._x;
Var y = this._y;
X = this.xdir;
Y = this.ydir;
If ((x <0 and this.xdir <0) or (x> 292 and this.xdir> 0))
(
This.xdir *= 1;
X = (x <0)? 0: 292;
)
Else if ((y <0 and this.ydir <0) or (y> 292 and this.ydir> 0))
(
This.ydir *= 1;
Y = (y <0)? 0: 292;
)
This._x = x;
This._y = y;
);
/ / BallClass categories and associated components Movie Clip
Object.registerClass (the "ball" BallClass);
# Endinitclip
Curry components in the adjacent Movie Clip ball right-click election "link", as shown

Features of:
In Flash MX, is a common procedure for the design, as the practice of particle components written in the reuse of packaging and so on have achieved Flash object-oriented programming design. Only disadvantage is that Prototype Based Object-oriented program design is not the habit, so there will be a Flash MX 2004 ActionScript 2.0.
5.ActionScript 2.0 Movement system of particles to rewrite ActionScript 2.0, the object-oriented concept with no ActionScript 1.0, respectively, but in different ways.
Development steps:
Based on the above source code changes. Would Movie Clip ball script to delete layers. Components in the Curry Movie Clip ball right-click next election "link" category for the importation of AS 2.0 BallClass, this step has been equivalent to Object.registerClass ( "ball "BallClass);
BallClass.as establishment of a document entitled, by adding code:
Code: / / succession MovieClip class
Class BallClass extends MovieClip
(
/ / Attribute
Private var xdir: Number;
Private var ydir: Number;
/ / Construction函å¼
Function BallClass ()
(
InitBall ();
)
/ / Methods
Private function initBall (): Void
(
Var speed = 10;
Var dir = random (360) * Math.PI/180;
Xdir = Math.cos (dir) * speed;
Ydir = Math.sin (dir) * speed;
OnEnterFrame = this.moveBall;
)
Private function moveBall (): Void
(
Var x = _x;
Var y = _y;
X = xdir;
Y = ydir;
If ((x <0 and xdir <0) or (x> 292 and xdir> 0))
(
Xdir *= 1;
X = (x <0)? 0: 292;
)
Else if ((y <0 and ydir <0) or (y> 292 and ydir> 0))
(
Ydir *= 1;
Y = (y <0)? 0: 292;
)
_x = X;
_y = Y;
)
)
After testing can be completed films (remember to Flash 6 / 7 ActionScript 2.0 release).
Features of:
ActionScript 1.0 type, if there is no field name is _ root in the establishment of ActionScript 2.0, in the establishment of global _ and good name of the area to handle the problem.
From the above examples of the evolution of a step-by-step, we can see that both object-oriented programming syntax is how to change, the core particle random mobile control procedures are similar, and the object-oriented programming concept is the same. Therefore Flash MX OOP global function from the novice to the design process ActionScript 2.0 is no obstacle, in the object-oriented programming concepts, learn ActionScript 2.0, we should try to take the first step.
Tags: CSS style design






Leave a Reply