編好一個(gè)類(lèi)之后,保存格式為.AS.這時(shí)候如果你要使用它,應該怎么辦呢?
把它放到MacromediaFlash MX 2004enFirst RunClasses目錄下.
保存類(lèi)的時(shí)候應該注意,文件名一定要和類(lèi)名一致.
調用的時(shí)候,就象調用其它內置類(lèi)一樣的調用.
比如我曾經(jīng)寫(xiě)的一個(gè)類(lèi)Kmath,用作FLASH內置類(lèi)--MATH類(lèi)的擴充,其內容如下:
class Kmath extends Math{
public function Kmath () {
}
//求余數
//modf(被除數,除數)
static public function modf(x:Number,y:Number):Number
{if(y!=0){
return x-int(x/y)*y}
else{return Number.NaN}
}
//奇偶驗證(偶數為真)
static public function even(x:Number):Boolean
{
if((x-int(x/2)*2)==0){return true}
else{return false}
}
//求弧度
//rad(角度值)
static public function rad():Number
{return deg*Math.PI/180
}
//以角度計算的三角函數
//sinD(角度);cosD(角度);tanD(角度)
static public function sinD():Number
{return Math.sin(deg*Math.PI/180)
}
static public function cosD():Number
{return Math.cos(deg*Math.PI/180)
}
static public function tanD():Number
{return Math.tan(deg*Math.PI/180)
}
//返回值為角度的反三角函數
//asinD(數值);acosD(數值);atanD(數值)
static public function asinD(x:Number):Number
{return Math.asin(x)*180/Math.PI
}
static public function acosD(x:Number):Number
{return Math.acos(x)*180/Math.PI
}
static public function atanD(x:Number):Number
{return Math.atan(x)*180/Math.PI
}
//計算兩點(diǎn)直線(xiàn)的傾斜角(1為起點(diǎn),2為終點(diǎn))
//atan2D(點(diǎn)1橫坐標值,點(diǎn)1縱坐標值,點(diǎn)2橫坐標值,點(diǎn)2縱坐標值)
static public function atan2D(x1:Number,y1:Number,x2:Number,y2:Number):Number
{
return Math.atan((y2-y1)/(x2-x1))*180/Math.PI
}
//計算兩點(diǎn)距離
//dist(點(diǎn)1橫坐標值,點(diǎn)1縱坐標值,點(diǎn)2橫坐標值,點(diǎn)2縱坐標值)
static public function dist(x1:Number,y1:Number,x2:Number,y2:Number):Number
{
return Math.sqrt(Math.pow((x1-x2),2)+Math.pow((y1-y2),2))
}
//返回X到Y之間的隨機整數
//random(較小數,較大數)
static public function rand(x:Number,y:Number):Number
{
return x+random(y-x)
}
}
public function Kmath () {
}
//求余數
//modf(被除數,除數)
static public function modf(x:Number,y:Number):Number
{if(y!=0){
return x-int(x/y)*y}
else{return Number.NaN}
}
//奇偶驗證(偶數為真)
static public function even(x:Number):Boolean
{
if((x-int(x/2)*2)==0){return true}
else{return false}
}
//求弧度
//rad(角度值)
static public function rad():Number
{return deg*Math.PI/180
}
//以角度計算的三角函數
//sinD(角度);cosD(角度);tanD(角度)
static public function sinD():Number
{return Math.sin(deg*Math.PI/180)
}
static public function cosD():Number
{return Math.cos(deg*Math.PI/180)
}
static public function tanD():Number
{return Math.tan(deg*Math.PI/180)
}
//返回值為角度的反三角函數
//asinD(數值);acosD(數值);atanD(數值)
static public function asinD(x:Number):Number
{return Math.asin(x)*180/Math.PI
}
static public function acosD(x:Number):Number
{return Math.acos(x)*180/Math.PI
}
static public function atanD(x:Number):Number
{return Math.atan(x)*180/Math.PI
}
//計算兩點(diǎn)直線(xiàn)的傾斜角(1為起點(diǎn),2為終點(diǎn))
//atan2D(點(diǎn)1橫坐標值,點(diǎn)1縱坐標值,點(diǎn)2橫坐標值,點(diǎn)2縱坐標值)
static public function atan2D(x1:Number,y1:Number,x2:Number,y2:Number):Number
{
return Math.atan((y2-y1)/(x2-x1))*180/Math.PI
}
//計算兩點(diǎn)距離
//dist(點(diǎn)1橫坐標值,點(diǎn)1縱坐標值,點(diǎn)2橫坐標值,點(diǎn)2縱坐標值)
static public function dist(x1:Number,y1:Number,x2:Number,y2:Number):Number
{
return Math.sqrt(Math.pow((x1-x2),2)+Math.pow((y1-y2),2))
}
//返回X到Y之間的隨機整數
//random(較小數,較大數)
static public function rand(x:Number,y:Number):Number
{
return x+random(y-x)
}
}
把這段代碼保存為Kmath.as,并放在MacromediaFlash MX 2004enFirst RunClasses文件夾中,然后,如果你要調用這個(gè)類(lèi)種的方法,比如說(shuō),我們要求余數,就只需要在動(dòng)作面板里輸入類(lèi)似:Kmath.modf(3,4).這樣的代碼.
你也可以在MacromediaFlash MX 2004enFirst RunClasses文件夾中新建一個(gè)專(zhuān)門(mén)放置自定義類(lèi)的文件夾,比方為myClass
那么再調用剛剛那個(gè)類(lèi)的方法的話(huà),就要類(lèi)似myClass.Kmath.modf(3,4).這樣的代碼了 .
以上.