一個用純AS寫的正態(tài)曲線畫法_Flash教程
教程Tag:暫無Tag,歡迎添加,賺取U幣!
推薦:用Flash 2004編寫簡單的運算類在Flash2004中新建一個ActionScript文檔,輸入下列的Action腳本//這個類是基本的運算類,通過屬性來引入數(shù)據(jù)接口,并且在類中異常拋出classoperationClass
我的這段AS寫了不少有用的函數(shù),如畫按鈕函數(shù),畫坐標軸函數(shù),畫框架函數(shù).這些函數(shù)都有很強的移植性,以后都可以直接拿來用; //================按鈕和坐標軸上的文字=====================//mytxt = ["開始", "暫停", "清除", "全屏", "退出", "繼續(xù)"];
myNum = ["-30", "-20", "-10", "0", "10", "20", "30", "X", "Y"];
//=====畫按鈕(type不為0時按鈕為凸起狀態(tài)type=0時按鈕為凹下狀態(tài))=====//
CommandButton = function (mc, t, type) {
c = 0xffffff;//按鈕左和上邊框顏色
c1 = 0x000000;//按鈕右和下邊框顏色
mc.createTextField("txt", 700, 12, 1, 27, 18);
if (!type) {
c = 0x000000;
c1 = 0xffffff; //type=0時左和上與右和下邊框顏色交換;
mc.createTextField("txt", 700, 13, 2, 27, 18);//按下時文本框向左下移,使字有凹下感覺
}
with (mc) {
moveTo(0, 20);
lineStyle(1, c, 100);
beginFill(0xD1DEE9);
lineTo(0, 0);
lineTo(50, 0);
lineStyle(1, c1, 100);
lineTo(50, 20);
lineTo(0, 20);
endFill();
txt.text = t;
}
};
//===========畫框架=============//
display = function (mc, x, y, w, h, txt, corl) {
with (mc) {
moveTo(x, y);
lineStyle(0.5, 0x00000, 100);
beginFill(corl, 40);
lineTo(x w, y);
lineTo(x w, y h);
lineTo(x, y h);
lineTo(x, y);
endFill();
createTextField("name", 300, x 2, y-17, 0, 0);
name.autoSize = "left";
name.selectable = false;
name.border = true;
name.background = true;
name.backgroundColor = 0x798DA6;
name.textColor = 0xffffff;
name.text = txt;
}
};
//========畫坐標軸上刻度及數(shù)字==============//
dial = function (mc, len, corl, txt, type) {
with (mc) {
moveTo(0, 0);
lineStyle(0.25, corl, 100);
if (type) {//type不為0時刻度堅著畫,為0時刻度橫著畫;
lineTo(0, len);
createTextField("num", 600, -7, len 1, 0, 0);
} else {
lineTo(len, 0);
createTextField("num", 600, len 1, -2, 0, 0);
}
num.autoSize = true;
num.selectable = false;
num.text = txt;
}
};
//========畫坐標軸============//
coordinate = function (x, y) {
_root.moveTo(x-215, y);
_root.lineStyle(0.25, 0x00000, 100);
_root.lineTo(x 215, y);
_root.lineTo(x 185, y 5);
_root.moveTo(x 215, y);
_root.lineTo(x 185, y-5);
_root.moveTo(x, y 20);
_root.lineTo(x, y-220);
_root.lineTo(x-5, y-190);
_root.moveTo(x, y-220);
_root.lineTo(x 5, y-190);
for (i=0; i<19; i ) {
_root.createEmptyMovieClip("l" i, 510 i);
if (i<13) {
!(i%2) ? dial(_root["l" i], 5, 0xff0000, myNum[i/2], 1) : dial(_root["l" i], 3, 0x000000, "", 1);//刻度隔一個為紅色,且有數(shù)字
_root["l" i]._x = x-198 33*i;
_root["l" i]._y = y;
}
if (i>12) {//同上
!(i%2) ? dial(_root["l" i], 5, 0xff0000, myNum[i/2-3], 0) : dial(_root["l" i], 3, 0x000000, "", 0);
_root["l" i]._x = x;
_root["l" i]._y = y-33*(i-12);
}
}
_root.createTextField("Xt", 250, x 220, y-3, 18, 18);
_root.createTextField("Yt", 260, x, y-230, 18, 18);
Xt.text = myNum[7];
Xt.selectable = false;
Yt.text = myNum[8];
Yt.selectable = false;
};
inputBoxs = function (x, y) {
var alpha = ["u =", "0", "o =", "1"];
for (i=0; i<4; i ) {
_root.createTextField("v" i, 800 i, x i*35, y, 30, 16);
if (i%2) {
_root["v" i].type = "input";
_root["v" i].border = true;
_root["v" i].text = alpha[i];
} else {
_root["v" i].autoSize = "right";
_root["v" i].selectable = false;
_root["v" i].text = alpha[i];
}
}
};
//======寫標題========//
headline = function (x, y, txt, dx) {
_root.createTextField("title", 900, x, y, 0, 0);
title.autoSize = true;
title.selectable = false;
title.text = txt;
mytxf = new TextFormat();//創(chuàng)建一個文本格式對象;
mytxf.size = dx;//太小
mytxf.color = 0xff0000;//顏色
mytxf.underline = true;//下劃線
title.setTextFormat(mytxf);
};
//=====開始畫線函數(shù)====//
startDraw = function () {
m = Number(v1.text);
n = Number(v3.text);//把v1,v3文本框中的值給m,n;
x = -200;
_root.createEmptyMovieClip("xian", 300);
xian.moveTo(-200, 100);
xian._x = 275;
xian._y = 193;
_root.onEnterFrame = function() {
a = -(100/(Math.sqrt(2*Math.PI)*25*n))*Math.exp((-(Math.pow((x-100*m), 2)))/ (2*Math.pow(25*n, 2)));//這個為正態(tài)曲線公式,根椐這個公式來畫線;
with (xian) {
lineStyle(2, 0xE001E0, 100);
lineTo(x, 50*a 100);//畫線
if (x<=200) {//畫線范圍
x = 3;//3為畫線速度,建議設小一點,fps設大一點,這樣使畫出的線更平滑;
}
}
};
_root.btn1.enabled = 1;//暫停按鈕可用
_root.btn5.enabled = 1;//繼續(xù)按鈕可用
};
//========繼續(xù)畫線========//
continueDraw = function () {
xian.moveTo(x-3, 50*a 100);//x,a繼續(xù)上面函數(shù)的值,從當前位置畫
_root.onEnterFrame = function() {
a = -(100/(Math.sqrt(2*Math.PI)*25*n))*Math.exp((-(Math.pow((x-100*m), 2)))/ (2*Math.pow(25*n, 2)));
with (xian) {
lineStyle(2, 0xE001E0, 100);
lineTo(x, 50*a 100);
if (x<=200) {
x = 3;//同上
}
}
};
};
//========暫停和清除函數(shù)==========//
pause_clear = function (k) {//不為0時為暫停,為0時為清除;
_root.onEnterFrame = function() {
x = 0;//x值不增加
};
if (k) {
xian.clear();//清除xian
_root.btn1.enabled = 0;
_root.btn5.enabled = 0;
}
};
_root.onLoad = function() {
headline(230, 20, "正態(tài)曲線", 20);
for (i=0; i<3; i ) {
_root.createEmptyMovieClip("frame" i, 400 i);
}
display(_root.frame0, 40, 55, 470, 270, "顯示", 0xE6E1CC);
display(_root.frame1, 40, 350, 160, 40, "變量", 0xDCE6ED);
display(_root.frame2, 220, 350, 290, 40, "操作", 0xDCE6ED);
coordinate(275, 295);
inputBoxs(50, 365);
_root.attachMovie("formula", "formula", 500);
formula.useHandCursor = false;//鼠標不變手形
formula._x = 42;
formula._y = 57;
for (i=0; i<6; i ) {
_root.createEmptyMovieClip("btn" i, 700 i);
CommandButton(_root["btn" i], mytxt[i], 1);//用循環(huán)來畫按鈕;
_root["btn" i]._x = 230 55*i;
_root["btn" i]._y = 360;
_root.btn5._x = 230 55*1;
_root.btn5._y = 360;
_root.btn5._visible = 0;//設置按鈕位置,并把暫停和繼續(xù)按鈕放在一起;且開始時繼續(xù)不可見
}
_root.btn1.enabled = 0;//開始時暫停不可用
};
_root.onEnterFrame = function() {//以下為按上每個按鈕的動作;
_root.btn0.onPress = function() {
CommandButton(_root.btn0, mytxt[0], 0);//按鼠標時按鈕凹下;
_root.btn1._visible = 1;//暫停按鈕可見
_root.btn5._visible = 0;繼續(xù)按鈕不可見
startDraw();//調用開始畫線函數(shù);
};
_root.btn0.onRelease = function() {
CommandButton(_root.btn0, mytxt[0], 1);//松開鼠標時按鈕凸起;
};
_root.btn1.onPress = function() {
CommandButton(_root.btn1, mytxt[1], 0);
pause_clear(0);//調用暫停
};
_root.btn1.onRelease = function() {
CommandButton(_root.btn1, mytxt[1], 1);
_root.btn1._visible = 0;
_root.btn5._visible = 1;//按了暫停按鈕時,變成繼續(xù)按鈕;
};
_root.btn2.onPress = function() {
CommandButton(_root.btn2, mytxt[2], 0);
pause_clear(1);//調用清除函數(shù);
};
_root.btn2.onRelease = function() {
CommandButton(_root.btn2, mytxt[2], 1);
};
_root.btn3.onPress = function() {
CommandButton(_root.btn3, mytxt[3], 0);
fscommand("fullscreen", true);//全屏
};
_root.btn3.onRelease = function() {
CommandButton(_root.btn3, mytxt[3], 1);
};
_root.btn4.onPress = function() {
CommandButton(_root.btn4, mytxt[4], 0);
fscommand("quit");//退出
};
_root.btn4.onRelease = function() {
CommandButton(_root.btn4, mytxt[4], 1);
};
_root.btn5.onPress = function() {
CommandButton(_root.btn5, mytxt[5], 0);
continueDraw();//調用繼續(xù)畫線函數(shù);
};
_root.btn5.onRelease = function() {
CommandButton(_root.btn5, mytxt[5], 1);
_root.btn5._visible = 0;
_root.btn1._visible = 1;//按繼續(xù)按鈕時,變成暫停按鈕;
};
_root.formula.onRollOver = function() {
formula._xscale = formula._yscale=330;//放大尺寸;
};
_root.formula.onRollOut = function() {
formula._xscale = formula._yscale=100;//尺寸還原
};
};
分享:flash action 詳解(9)同變量一樣,函數(shù)也可以具有全局性.只要在聲明時在前面給它加一個_global就可以了://計算矩形面積的全局函數(shù)_global.areaOfBox=function(a,b){retur
相關Flash教程:
- 相關鏈接:
- 教程說明:
Flash教程-一個用純AS寫的正態(tài)曲線畫法。