|
|
|
How to create randomly flying away movie
Author: Sergiy Kamenyev | Email Website : www.vemix.com
Advertisement
where x and y are the usual coordinates and x' and y' are the coordinates, turned on a angle (see pic.1).

Here is the code on the first frame of movie that we are going to move:
1 //Destination "X" and "Y" coordinates
2 var XFA,YFA; //Assign this variables on "_parent".
3 var QXsC; //Number of steps
4 var IDesc; //If this variable is "true", the object will disappear, otherwise it will appear
5 if(IDesc==null)
6 IDesc=true;
7
8 var XB,YB; //Global variables to remember initial position of "this" movie
9
10 var AAlpha; //Angle of slope of the segment between (XB,YB) and (XFA,YFA)
11 var A0; //First coefficient of random polynomial, by what the movie will move
12 var XsRM,XsRFA; //Second and third roots of the polynomial in turned coordinates
13 var XsC; //Current "X" coordinate when "this" is moving (in turned coordinates)
14 var DXsC; //"X" step of movement in turned coordinates
15 var NXsC; //Step number
16
17 var IRun=null; //Action director
18 /*f***********/
19 function flyAwayRandomly()
20 {
21 //We came here after button was clicked
22 findPolynomialRandomly();
23 //Let's begin moving!
24 IRun=0;
25 }
26 /*f***********/
27 function findPolynomialRandomly()
28 {
29 var tanAlpha; //Slope of the segment between (XB,YB) and (XFA,YFA)
30 var xmax1,xmax2;
31 var dxr;
32
33 //Let's remember initial position of "this"
34 XB=_x;
35 YB=_y;
36
37 //Calculate angle of slope
38 if(XFA!=XB)
39 {
40 //If the direction of the destination is not vertical:
41 tanAlpha=(YFA-YB)/(XFA-XB);
42 AAlpha=Math.atan(tanAlpha);
43 if(XFA
44 AAlpha+=Math.PI;
45 }
46 else
47 {
48 //Calculate angle of vertical directions (straightly up and down):
49 AAlpha=Math.PI/2;
50 if(YFA
51 AAlpha+=Math.PI;
52 }
53
54 //Find "farthest" root of the polynomial
55 if(XFA!=XB)
56 XsRFA=(XFA-XB)/Math.cos(AAlpha);
57 else
58 XsRFA=Math.abs(YFA-YB);
59
60 //Find "middle" root of the polynomial
61 XsRM=XsRFA/4;
62 dxr=random(Math.abs(XsRFA))/2;
63 if(XsRM<0)
64 dxr=-dxr;
65 XsRM+=dxr;
66
67 //Find "elder" coefficient of the polynomial
68 A0=1;
69 xmax1=Math.abs(polRan((XsRM+XsRFA)/2));
70 xmax2=Math.abs(polRan(XsRM/2));
71 xmax1=Math.max(xmax1,xmax2);
72 A0=1/xmax1*50;
73 //Sometimes it is positive, sometimes - negative
74 if(random(2))
75 A0=-A0;
76
77 //Find "X" step of moving
78 DXsC=XsRFA/QXsC;
79
80 //Assign X in turned coordinates
81 XsC=0;
82 //Initialize step number
83 NXsC=0;
84 }
85 /*f***********/
86 function fnOnEnterFrame()
87 {
88 //On every frame we are here from "Timer"
89 if(IRun!=null)
90 moveThis();
91 }
92 /*f***********/
93 function moveThis()
94 {
95 var ysC;
96
97 if(NXsC>=QXsC)
98 {
99 //If "this" movie has reached destination,
100 //we do not allow it to move further,
101 //and preparing to stop
102 IRun=1;
103 XsC=XsRFA;
104 }
105
106 //Calculate "Y" by "X" in turned coordinates
107 ysC=polRan(XsC);
108
109 //Convert turned coordinates to used in Flash
110 _x=XB+XsC*Math.cos(AAlpha)-ysC*Math.sin(AAlpha);
111 _y=YB+XsC*Math.sin(AAlpha)+ysC*Math.cos(AAlpha);
112
113 //Decrease scales of "this" movie
114 if(IDesc)
115 _xscale=(QXsC-NXsC)/QXsC*100;
116 else
117 _xscale=NXsC/QXsC*100;
118 _yscale=_xscale;
119
120 //Step forward
121 XsC+=DXsC;
122 if(IRun==1)
123 {
124 //Call the callback function to inform "_parent" that the moving is over
125 _parent.cbFinished(this);
126 //Stop the moving
127 IRun=null;
128 }
129
130 NXsC++;
131 }
132 /*f*****************************************************
133 This function calculates result
134 of our polynomial in "Xsc"
135 *******************************************************/
136 function polRan(XsC)
137 {
138 var ys;
139 ys=A0*XsC*(XsC-XsRM)*(XsC-XsRFA);
140 return ys;
141 }
The function findPolynomialRandomly() calculates the coefficients of our random polynomial. Let's look at it.
Since we want to rotate coordinates so that its X-axis coincides with the segment between initial point and destination point, tangent of the angle a equals:
| | YFA - YB |
| tg α = | ----------- | (line 41 of the code above), |
| | XFA - XB |
(see pic.2).

Download Source File
We hope the information helped you. If you have any questions
or comments, please don't hesitate to post them on the
Forums section
Submit your Tutorial at Click Here
|
|
|