第4節 三角函數值計算
在Flash 中,算術計算函數十分有限,很多函數計算都必須手寫很多代碼得以進行,這對一些不太懂編程的朋友來說是一件很痛苦的事情,此實例提供了一些常用的三角函數例如正弦和余弦的計算,表單最終效果如圖4.1所示:
圖4.1 最終效果
1.首先啟動Flash,新建一個影片,設置影片舞台大小為250px*200px(單位為象素),影片背景色為白色.
2.選擇工具箱中的文本工具,在舞台上的適當位置輸入提示文本"在此輸入角度:",然後還是使用文本工具在右邊繪制一個文本框,此文本框用來輸入一個角度值,此角度值可以被轉化為相應的弧度值,然後在屬性面板中設置其為輸入文本,文本框變量名為angle,其它相關設置如圖4.2所示:
圖4.2 設置文本框屬性
此時舞台如圖4.3所示:
圖4.3 繪制輸入文本框
3.按照相同的方法在舞台的下方輸入提示文字:"在此輸入角度",還是使用文本工具在右邊繪制一個文本框,此文本框用來輸出和輸入文本框中角度值相對應的弧度值.然後在屬性面板中設置其為動態文本框,設置文本變量為output,其它相關設置如圖4.4所示;
圖4.4 設置動態文本框屬性
此時舞台如圖4.5所示;
圖4.5 繪制動態文本框
4.下面制作用來控制角度轉換的部分,我們在一個影片剪輯裡實現這樣的功能.新建一個影片剪輯元件,命名為trig,進入元件的編輯區後,添加3個圖層,自上向下分別命名為stop, set values和Action.
選中圖層stop,在第1幀添加Action: stop();
5.選擇圖層set values,在第1幀添加如下Action:
sin0 = 0;
sin1 = .0175;
sin2 = .0349;
sin3 = .0523;
sin4 = .0698;
sin5 = .0872;
sin6 = .1045;
sin7 = .1219;
sin8 = .1392;
sin9 = .1564;
sin10 = .1736;
sin11 = .1908;
sin12 = .2079;
sin13 = .2250;
sin14 = .2419;
sin15 = .2588;
sin16 = .2756;
sin17 = .2924;
sin18 = .3090;
sin19 = .3256;
sin20 = .3420;
sin21 = .3584;
sin22 = .3746;
sin23 = .3907;
sin24 = .4067;
sin25 = .4226;
sin26 = .4384;
sin27 = .4540;
sin28 = .4695;
sin29 = .4848;
sin30 = .5;
sin31 = .5150;
sin32 = .5299;
sin33 = .5446;
sin34 = .5592;
sin35 = .5736;
sin36 = .5878;
sin37 = .6018;
sin38 = .6157;
sin39 = .6293;
sin40 = .6428;
sin41 = .6561;
sin42 = .6691;
sin43 = .6820;
sin44 = .6947;
sin45 = .7071;
sin46 = .7193;
sin47 = .7314;
sin48 = .7431;
sin49 = .7547;
sin50 = .7660;
sin51 = .7771;
sin52 = .7880;
sin53 = .7986;
sin54 = .8090;
sin55 = .8192;
sin56 = .8290;
sin57 = .8387;
sin58 = .8480;
sin59 = .8572;
sin60 = .8660;
sin61 = .8746;
sin62 = .8829;
sin63 = .8910;
sin64 = .8988;
sin65 = .9063;
sin66 = .9135;
sin67 = .9205;
sin68 = .9272;
sin69 = .9336;
sin70 = .9397;
sin71 = .9455;
sin72 = .9511;
sin73 = .9563;
sin74 = .9613;
sin75 = .9659;
sin76 = .9703;
sin77 = .9744;
sin78 = .9781;
sin79 = .9816;
sin80 = .9848;
sin81 = .9877;
sin82 = .9903;
sin83 = .9925;
sin84 = .9945;
sin85 = .9962;
sin86 = .9976;
sin87 = .9986;
sin88 = .9994;
sin89 = .9998;
sin90 = 1;
count = 91;
while (Number(count)<=180) {
set("sin" add count, eval ("sin" add (180 - count)));
count = Number(count)+1;
}
count = 181;
while (Number(count)<=270) {
set("sin" add count, eval ("sin" add (count - 180)) * -1);
count = Number(count)+1;
}
count = 271;
while (Number(count)<=360) {
set("sin" add count, eval ("sin" add (360 - count)) * -1);
count = Number(count)+1;
}
count = 0;
while (Number(count)<=90) {
set("cos" add count, eval ("sin" add (90 - count)));
count = Number(count)+1;
}
count = 91;
while (Number(count)<=180) {
set("cos" add count, eval ("cos" add (180 - count)) * -1);