Flash implementation of the 3D class (tutorial source file)

Source: Internet
Author: User
Tags array empty integer php code printf rar requires reset
Let me elaborate on the usage of these types of classes under the tutorial.
Note that you first have to make sure that these class files are in the same subdirectory as your FLA file or in the publishing settings.
The corresponding class file is added, otherwise the instance will not succeed.

The first thing to introduce to you is the Tvector3d class.
This part of the content today may require you to have a bit of vector knowledge, and a little more boring and less practical than later. A person who lacks sufficient patience and does not want to learn vector knowledge temporarily can skip it.
The ability to express may be poor, look haihan.
3dclasses.rar
Each instance of the Tvector3d class represents a vector of 3D space, so it is three-dimensional. We use triples (x,y,z) to represent them, so instances of each Tvector3d class have 3 properties, respectively X,y and Z.
the way to apply for a new Tvector3d class instance is:
var your variable name = new Tvector3d (x's value, Y's value, Z's value);

Example:
var v = new Tvector3d (1,1,1);//v is now a tvector3d instance of a representation vector (1,1,1). Then, let's look at the important ways Tvector3d offers.
1.printf ()
printf prints this vector in three-tuple form with trace to the Output window without parameters.
Example:
var v = new Tvector3d (1,1,1);
V.printf (); After the operation, you will find in the Output window (1,1,1);
This method is generally used when debugging a program.
Note: Do not let your eyes be deceived: If a number is 1.4342e-15 or 5.45561234234142E-17, remember that this is 0, not a float that is not 0!
2.reset ()
Reset is for this tvector3d vector to be assigned a new value.
Example:
var v = new Tvector3d (1,1,1);
V.reset (2,2,2);
V.printf (); After running, you will find that Output window outputs (2,2,2) instead of (1,1,1)

3.getCopy ()

The Getcopy method is to copy the vector itself and pass it to another tvector3d vector.
Example:
var v = new Tvector3d (1,1,1);
var v1 = new Tvector3d (0,0,0);
V1 = V.getcopy ();
V1.printf (); At this time, the Output window is run (1,1,1) rather than (0,0,0);

4. Method of vector Operation
(1) Plusby
Yourvector.plusby (B:tvector3d);
The vector is added. A parameter b,b is required to add the vector to this vector.
Instance:
var v = new Tvector3d (1,2,3);
var v1 = new Tvector3d (3,2,1);
V.plusby (v1);
V.printf (); Post-run output (4,4,4)

(2) Minusby
Yourvector.minusby (B:tvector3d);
Vector subtraction. Requires a parameter b,b to be a vector that subtracts from this vector.
Instance:
var v = new Tvector3d (4,4,4);
var v1 = new Tvector3d (3,2,1);
V.plusby (v1);
V.printf ();
Post-run output (1,2,3) (3) Realmulby
Yourvector.realmulby (K:number);
Multiplied by the real number. Requires a parameter k,k to be a real number (number) multiplied by this vector.
Instance:
var v = new Tvector3d (1,4,2);
V.realmulby (2);
V.printf (); Post-run output (2,8,4)

(4) Dotproby

Yourvector.dotproby (B:tvector3d): number;
Vector dot multiplication. A parameter b,b is required for the vector that is multiplied with this vector dot. Has a return value, which is a real number (number).
Instance:
var v = new Tvector3d (1,2,3);
var v1 = new Tvector3d (3,2,1);
var r:number = V.dotproby (v1);
Trace (R); Output 10 after running

(5) Crosspro
Tvector3d.crosspro (A,b:tvector3d): Tvector3d;
Vector fork multiplication. static methods. Two parameters A and b,a and B are required to be used as vectors for fork multiplication. Has a return value that is

Tvector3d vector.
Here, let's look at the difference between the static method and the above method.
In fact, I just need to cite one example. When you use the Math class function, how do you use it?
Yes, like Math.Abs (). It's the same here. For static methods, use the Tvector3d. Method name () to invoke.
Instance:
var v = new Tvector3d (4,4,4);
var v1 = new Tvector3d (3,2,1);
var t = new Tvector3d (0,0,0);
t = Tvector3d.crosspro (V,V1);
T.printf (); Run after output (-4, 8,-4)

5.len2 ()

Yourvector.len2 (): number;
Returns the square of the length of the current vector.
We should realize that most of the time we need more than the length of the vector, but the square of its length. Dispense with a time-consuming root operation, why not?
Instance:
var v = new Tvector3d (1,2,2);
var l:number = V.len2 ();
Trace (L); Output as: 9

6.rotate
Yourvector.rotate (Tx,ty,tz:number);
This method is used to rotate your vector around an axis, notice that it is something.
Generally, the Tx,ty,tz three parameters are rotated around the corresponding axis, where at least two are 0, that is, do not rotate around two or three axes with the same call to the Rotate method, which should be called two or three times.
Tx,ty,tz Use angle system.
Instance:
var v = new Tvector3d (1,0,0);
V.rotate (0,90,0)//rotate 90 degrees around the y axis.
V.printf (); Output: (0,0,-1)

Using tutorial 2

tparitcle3d.as particle class.
This is actually a class provided by Robert Penner's book.
Dissect this class first:
1. Create an instance of the class.
If we want to create an instance of this class, first let's look at its creation function:
function Tparticle3d (X1:number, Y1:number, Z1:number, Dist1:number, Panel1:movieclip, id1:string, mcname1:String, DEP1 : number)
Remove the description of the type:
function Tparticle3d (x1, y1, Z1, Dist1, Panel1:, Id1, mcname1, DEP1)
Because there are more parameters, so create the time to be careful, do not make a mistake.
Take a look at the function of each parameter:
X1,Y1,Z1: These three real numbers represent the position of the particle you want to create in the coordinate system.
Dist1, which describes the distance from the eye of the observer to the plane of Xoy. The value of the distance is positive real number. Note that the smaller the distance, the more visible the "perspective" effect of the object you see, the greater the "perspective" effect of the object you see. In general, if you want a more visible perspective, select a value for 250~300, and take 1000, the perspective is less obvious. If you take 3000, you don't want perspective effects.
Panel1: This parameter is a movie clip (MovieClip, MC) that is used to place the 3D particles that will be plotted. In general, is an empty movie clip. So, we often need to create an empty movie clip (MC) and pass it as a parameter before creating an instance of the Tparticle3d class with Createmptymovieclip.
ID1: This is a string-type argument. It represents the ID of the movie clip (MC) that you are going to attach to the screen as a "particle". When we use the Attachmovie, the ID in it is the same as the ID here. So, set the ID of your particle MC First, because it will be attach. The method is set: Right click on the particle MC in the library, select linkage ..., tick the export for ActionScript, and

Name it in the identifier above. The name result is the ID.
MCNAME1: The name of the instance of the new particle you want to create. String type;
DEP1: Integer, the depth of the MC created.

2. Rotate (a,b,c);
The three parameters of this method are real numbers, respectively, representing the angle of rotation around the x,y,z axis.
In general, we can assume that at least two of the a,b,c three are 0, for reasons and in the previous article about Tvector

The rotate () method in the class is the same.

3.rotateAxis:
This method was not in the Robert Penner class, which I later added. The effect is to rotate around a particular axis, not to be introduced in detail here.

4.render ();
This is a vital method!!!
Before we create a class instance, it's just a logical thing. How to finish after the creation of the screen

Animation on the screen?
The render () method deals with the position of the particle and "draws" the corresponding particle (that is, the MC) on the screen. If you create only one class without render (), then nothing is seen. If you make a modification (such as rotation (rotate)) without render (), the changes will not appear on the screen. And animation, is constantly modified, constantly render () displayed.

OK, the class is finished, let's look at an example:
First, create an MC, like a ball. Make sure it has a center of 0, 0 points. Add an ID to it, for example "ball".

Then we need to create an empty MC:
_root.createemptymovieclip ("P", 1);//Create an empty MC, the name is P
p._x = 275;
P._y = 200;//Because the stage of most flash movies is set to 550*400, these two sentences put the MC
Stage the center of the canvas. Then, to create an instance of Tparticle3d, the instance name may be called Par:
var par = new Tparticle3d (0,-40,-80,300,_ROOT.P, "Ball", "a", 1);
The position of the corresponding particle in the coordinate system is (0,-40,-80), the distance from the Xoy plane of our observer is 300, and has a strong perspective effect (near big far and small). Its Father MC is _ROOT.P, is we just created the empty MC. "Ball" is the ID of our particle MC, just set up earlier. "A" indicates that the "ball" is placed on the canvas after the instance is named "a", and 1 indicates the depth of the MC.
This time, you run, you will find nothing on the screen. Why, then? That's right, render!.

Then, be ready to render ().
Add a sentence:
Par.render (); This time running, is not found to see the ball?

Now let's get it moving! As with many as animations, we might as well use the Onenterframe event handle.
Sum of the above code:
_root.createemptymovieclip ("P", 1);
p._x = 275;
p._y = 200;
var par = new Tparticle3d (0,-40,-80,300,_ROOT.P, "Ball", "a", 1);
Par.render ();
_root.onenterframe = function () {
Par.rotate (0,5,0);
Par.render ();
Run and you can see the effect of the rotation.
If you feel that the movement is not smooth, you can use SetInterval. Use the setinterval to view Help or search through old posts.
Code changed to:
_root.createemptymovieclip ("P", 1);
p._x = 275;
p._y = 200;
var par = new Tparticle3d (0,-40,-80,300,_ROOT.P, "Ball", "a", 1);
Par.render ();
Function Checker () {
Par.rotate (0,4,0);
Par.render ();
Updateafterevent ();
}
var timer1:number = setinterval (checker,30); Tparticle.rar
Tparticlesetinterval.rar
Frame Class (top)
tframe.asStill, in the example, make sure that the FLA and. As are associated with the same directory or. AS and the Fla.
The so-called frame class, in fact, is composed of polygons of the polyhedron class. Because it's made up of segments, it looks just like a polyhedron frame, so it's called a framework class. After learning this, you can easily create frame stereo graphics, such as cubes, positive tetrahedron and so on.

Dissect this class first:
1. Create an instance of the class.
If we want to create an instance of this class, first let's look at its creation function:
function Tframe (N1:number, V1:array, D1:number, E1:array, Back1:movieclip, Color1:number, Dist1:number)
Remove the description of the type:
function Tframe (n1,v1, D1, E1, Back1, Color1, Dist1)
Among these parameters:
N1: is an integer. It indicates how many points there are in a frame.
V1: is an array. Each of its elements is a Tvector3d class instance (Tvector3d, vector Class) that represents the coordinates of this point in the coordinate system. V1 altogether have N1 elements, subscript from v1[0] until v1[n1-1];
D1: Integer. Represents the number of sides of a frame. Each segment is called an Edge (edge).
E1: Array. E1 is used to describe each edge. Every element of E1 is a "record", and we can think of it as an object. Each element has two attributes, A and B, and two integers, representing the two points connected by the edge (the value of A and B represents the index (subscript) of the corresponding point in V1). E1 altogether has D1 elements, subscript from e1[0] to e1[d1-1].
Back1: Type is a movie clip. The MC that is used to hold this frame is generally an empty MC. So we often need to apply for an empty mc:createemptymovieclip () before creating a framework class instance;
Color1: Represents the color of the frame. You can use a decimal number (which is annoying), or you can use hexadecimal numbers (as in the case of colors in as Central) as 0x000000 for black.
Dist1: Distance, in the "Create class instances" in the previous article, no longer repeat.

At this point, creating a framework class requires only:
var your variable name = new Tframe (n1,v1, D1, E1, Back1, Color1, Dist1);
It's OK.
Such as:
var myframe = new Tframe (8,V1, E1, _ROOT.P, 0x000000, 1000); 2. Methods of class:
or render and rotate, no longer repeat.
Let's take a look at how to use the frame class to make a three-dimensional square, note that it is a square (note: All code is placed in the first frame).
First, place the four points in the array, where the array is v.
PHP Code: v = [];
V[0] = new Tvector3d (50,-50,0);
V[1] = new Tvector3d (50,50,0);
V[2] = new Tvector3d ( -50,50,0);
V[3] = new Tvector3d ( -50,-50,0);//The edges of the square is 100, and the above is the coordinates of its four points. The center of the square in (0,0,0) then we give a point-to-point relationship, which is stored in the e array. Slightly, E and V are passed as arguments to the Tframe creation function:

PHP Code: E = [];
E[0] = {a:0,b:1};//This sentence means that the 0th edge is connected in V to the point numbered E[0].A = 0 and the point numbered e[0].b = 1. The following analogy.
E[0] = {A:1,b:2};
E[0] = {A:2,b:3};
E[0] = {A:3,b:4}; Then apply for an empty MC for the Tframe instance:
PHP Code:
_root.createemptymovieclip ("P", 1);
p._x = 275;
p._y = 200;//Center The MC and start applying for the Tframe instance, and render to make it appear on the screen.
PHP Code:
var F0 = new Tframe (4,v,4,e,_root.p,0xff0000,300);
F0.render (); At this point, the creation of the square is completed, if you want to add the key to the keyboard rotation effect, plus the following code (not detailed, if read do not understand the procedure to detail):
PHP Code:
Function Checker () {
if (Key.isdown (key.up)) {
F0.rotate (-3, 0, 0);
F0.render ();
}
if (Key.isdown (Key.down)) {
F0.rotate (3, 0, 0);
F0.render ();
}
if (Key.isdown (Key.left)) {
F0.rotate (0, 3, 0);
F0.render ();
}
if (Key.isdown (key.right)) {
F0.rotate (0,-3, 0);
F0.render ();
}
Updateafterevent ();
}
var timer1:number = setinterval (checker, 30); Square. rar
Friendly tip: Put the. fla in the same directory, and then put all the as in that directory. Note the generated SWF does not need to rely on the classes it uses.
In the case of squares, tvector3d,tvector and tframe are used.

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.