日志文章

2007-04-02

[教學]如何控制好Flash特效

作者 : Super-Tomato ~H+R'.S  
F %N B  
tA(:Sl  
這篇並不是一步步的教你如何製作任何效果, 只是要提醒那些從別處網站得到coding後依樣畫葫蘆得到了效果卻不會讓效果消失的朋友, 如何以比較好的方式來控制. 以下是從別處拿來的花瓣飄落效果. +]BY"gg%$@  
'U- ) pXn  
Z[46F8qBM  
mover = function () { G)Ot0+8O t  
    this._y += this.k; w@{1BZ6,  
    this._x += this.wind;
`XunX4&  
    if (this._y>height+10) { 1#D:qG"v0o  
        this._y = -20; _P7!pr;  
    }
1'mYfoTV  
    if (this._x>width+20) { E+~~M'0E  
        this._x = -(width/2)+Math.random()*(1.5*width); nPOp?Ax  
        this._y = -20; <3hnmZ,~  
    } else if (this._x<-20) { tArLYs-_e  
        this._x = -(width/2)+Math.random()*(1.5*width); eg>W50|46  
        this._y = -20; +Rif [*;  
    } F 844o/y  
};
jWeC$  
init = function () { htfly  
    width = 800;  // pixels  }74ZaeP)3  
    height = 600; // pixels  Y V o^WGc  
    max_snowsize = 5; // pixels  C $;"E5j  
    snowflakes = 30; // quantity  aDM! ?  
    for (i=0; i<snowflakes; i++) { w%? cenqQ'  
        t = attachMovie("sakura", "sakura"+i, i); ^J$k@F#,  
        t._alpha = 20+Math.random()*60; $RnI9tY<  
        t._x = -(width/2)+Math.random()*(1.5*width); [[x<`6`W  
        t._y = -(height/2)+Math.random()*(1.5*height); 5aj{  
        t._xscale = t._yscale=50+Math.random()*(max_snowsize*10); I,?|gwqY  
        t.k = 3+Math.random()*2; )sW?$2f|  
        t.wind = -3.5+Math.random()*(1.4*3); !T&.h8ey 4  
j8 9!bbc  
        //這裡開始花瓣會通過onEnterFrame的事件不停的在執行 %:\T^-i  
        t.onEnterFrame = mover; lC#r!Mr!  
    } IjTzDD"O0  
}; W$}MSht_a  
init(); iBYU d%n  
hZb%TD Ki  
X ZPut 7  
xu!._uq+0  
以這樣的方式在之後的50個frame之後加上 stop(); 指令的話只能讓frame停止播放, 但花瓣的飄落卻無法停止, 原因就是出在 onEnterFrame 這個 event 上. 要讓花瓣停止的為一方法就是要停止這個 onEnterFrame, 那麼能夠做的就是 1. 刪除 onEnterFrame 事件, 2. 刪除所有花瓣, 那麼每片花瓣所擁有的屬性和事件也會一併的刪除. W1bN% <X  
=]_l_-yqeo  
當然我們會選擇第二的方法, 直接把花瓣刪除, 不然你讓花瓣停止了卻不消失有何用? Bn:C4'x  
所以你就必須取得目前製作了多少片花瓣, snowflakes的數量, 然後再通過 for loop 把所有的花瓣removeMovieClip. 如: 2dy;I]A#  
` ~bi}"  
zuX%9B  
YX]Z[3w;G  
for (i=0; i<snowflakes; i++) this["sakura"+i].removeMovieClip(); l5'CWw9J[M  
y?<J7EJg  
wV :l991  
$cfu 1>}  
這樣雖然沒問題.... 但在製作一個比較大型的 flash 時, 你不可能會把所有的 variable 都記住. 要提升自己的製作速度就必須把所有東西都規劃好, 那麼你就該這樣做. nrZ:E/7  
YTYQ]sD  
= BPe?v]  
mover = function () { V{mFTT  
    this._y += this.k; %'HL*Kec `  
    this._x += this.wind;
lAws&m9]  
    if (this._y>height+10) { +)`m]*  
        this._y = -20; i:B018nz6  
    }
W!;OsQi&  
    if (this._x>width+20) { BJl>)9]%n  
        this._x = -(width/2)+Math.random()*(1.5*width); b=u- jr@x  
        this._y = -20; U.CQS2,I  
    } else if (this._x<-20) { =Vy r_VvO  
        this._x = -(width/2)+Math.random()*(1.5*width); Wq3CE\d&  
        this._y = -20; O#3;p4LGOq  
    } SdzHR|+  
};
SdcD lc9  
init = function () { \H,V?x  
    width = 800;  // pixels  iQ  q J  
    height = 600; // pixels  j[6st   
    max_snowsize = 5; // pixels  i 6 &Ie  
    snowflakes = 30; // quantity  V7drx6  
?*qGqJ5&.R  
    //建立一個叫sakura的空MovieClip. rp(v;mM  
    this.createEmptyMovieClip("sakura", this.getNextHighestDepth()); z2J8wJ  
i6./Tp99  
    for (i=0; i<snowflakes; i++) { 6suTzm9<g  
        //把所有的花瓣attach到這個sakura空MovieClip中. B%r[mSC<  
        t = sakura.attachMovie("sakura", "sakura"+i, i); "@Q;lA"J  
        t._alpha = 20+Math.random()*60; ]qAM_-HEr  
        t._x = -(width/2)+Math.random()*(1.5*width); >g}W\Gvx  
        t._y = -(height/2)+Math.random()*(1.5*height); mxZR/  
        t._xscale = t._yscale=50+Math.random()*(max_snowsize*10); ~/@.mL@?  
        t.k = 3+Math.random()*2; ku518#Mw  
        t.wind = -3.5+Math.random()*(1.4*3);
^XY?>c[V  
        t.onEnterFrame = mover; w/8S?mQV  
    } *nn[$iK  
};
mB d ?m  
init(); N;N%&&b  
yTZ,~J)  
4?0u6 m  
a3[U~ ;o  
那麼這樣一來你就只要記住這個效果叫 sakura, 其他的一概不管. 在要停止的時候就使用 $xfu8 4f/  
:[yp+27  
sakura.removeMovieClip(); `j T?|  
fIYi[F:tcT  
4 H!Nx.  
這樣的一句就可以把 sakura 和其所蓋括的 movieclip 一併移除掉. 不是更加省事嗎?


类别: 无分类 |  评论(0) |  浏览(17027) |  收藏