日志文章

2007-04-02

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

作者 : Super-Tomato ]h~?2&  
OUl*2$e_  
WJ5ekYs  
這篇並不是一步步的教你如何製作任何效果, 只是要提醒那些從別處網站得到coding後依樣畫葫蘆得到了效果卻不會讓效果消失的朋友, 如何以比較好的方式來控制. 以下是從別處拿來的花瓣飄落效果. w \:$M   
CW=]Yd;5Qt  
0Pe64p@  
mover = function () { x ^%$45A;  
    this._y += this.k; !sO$ bi .  
    this._x += this.wind;
1he5U Cgl  
    if (this._y>height+10) { bgfKQ~r  
        this._y = -20; 4ks Z U  
    }
oFZD~8zu  
    if (this._x>width+20) { P$)pr%b5  
        this._x = -(width/2)+Math.random()*(1.5*width); yqM. z3T  
        this._y = -20; TH j GS  
    } else if (this._x<-20) { s ? Oms  
        this._x = -(width/2)+Math.random()*(1.5*width); 2>=l*  
        this._y = -20; s X wh`}a  
    } , }n'n_  
};
}*b;/&91  
init = function () { c,JHHB,  
    width = 800;  // pixels  kz5Wkh7 _  
    height = 600; // pixels  ewu]W  
    max_snowsize = 5; // pixels  LJ@![<}X  
    snowflakes = 30; // quantity  GT~(|[;  
    for (i=0; i<snowflakes; i++) { ^xC'0x!  
        t = attachMovie("sakura", "sakura"+i, i); @/8 t%N  
        t._alpha = 20+Math.random()*60; [pnu7&oA&  
        t._x = -(width/2)+Math.random()*(1.5*width); -t/F[Z~ !  
        t._y = -(height/2)+Math.random()*(1.5*height); cT$&[lK5M  
        t._xscale = t._yscale=50+Math.random()*(max_snowsize*10); g5Y(ky1  
        t.k = 3+Math.random()*2; V2FG'n4I  
        t.wind = -3.5+Math.random()*(1.4*3); Ka%u mgsF  
0W;snro3  
        //這裡開始花瓣會通過onEnterFrame的事件不停的在執行 s1uQ M?  
        t.onEnterFrame = mover; x/w kC  
    } mVm.G  
}; `ZieH|R  
init(); |-S}/3o$  
zK#Es  
!&`Y:F  
{N^#iO  
以這樣的方式在之後的50個frame之後加上 stop(); 指令的話只能讓frame停止播放, 但花瓣的飄落卻無法停止, 原因就是出在 onEnterFrame 這個 event 上. 要讓花瓣停止的為一方法就是要停止這個 onEnterFrame, 那麼能夠做的就是 1. 刪除 onEnterFrame 事件, 2. 刪除所有花瓣, 那麼每片花瓣所擁有的屬性和事件也會一併的刪除. r PAg  
n?S]:gX]  
當然我們會選擇第二的方法, 直接把花瓣刪除, 不然你讓花瓣停止了卻不消失有何用? =)z1$;S  
所以你就必須取得目前製作了多少片花瓣, snowflakes的數量, 然後再通過 for loop 把所有的花瓣removeMovieClip. 如: 98ZDT]  
;xh9RTDzUG  
Um$tGT}  
R[sOHFQ^M  
for (i=0; i<snowflakes; i++) this["sakura"+i].removeMovieClip(); 4"7 ~qWx:  
SygowNnQ{  
<<:cTBV  
*$zCmP  
這樣雖然沒問題.... 但在製作一個比較大型的 flash 時, 你不可能會把所有的 variable 都記住. 要提升自己的製作速度就必須把所有東西都規劃好, 那麼你就該這樣做. iL)9Nm")!  
-{V e21`  
Lc t> rPP  
mover = function () { 1! 2dzZC'  
    this._y += this.k; DC_*o~DR  
    this._x += this.wind;
POw$Z  
    if (this._y>height+10) { 1Dt Lcyl  
        this._y = -20; hc<% v  
    }
|J 1i|#  
    if (this._x>width+20) { QE{z}x  
        this._x = -(width/2)+Math.random()*(1.5*width); UJTn:1(cR  
        this._y = -20; $g1N z.,A  
    } else if (this._x<-20) { 8Qem,l2l  
        this._x = -(width/2)+Math.random()*(1.5*width); }5EIfmw  
        this._y = -20; 7Zq82=V  
    } tKaJ^B  
};
Z(x*  
init = function () { fL?bUL_  
    width = 800;  // pixels  cj' XCrd  
    height = 600; // pixels  / 8"%4LyK  
    max_snowsize = 5; // pixels  ar8v;e"/  
    snowflakes = 30; // quantity  L}[+Sb%  
Y d=XnAL  
    //建立一個叫sakura的空MovieClip. fg_MdQ!  
    this.createEmptyMovieClip("sakura", this.getNextHighestDepth()); k#s(39-S  
U0(gEB  
    for (i=0; i<snowflakes; i++) { W@[[nG=D  
        //把所有的花瓣attach到這個sakura空MovieClip中. jU%SEK[~X  
        t = sakura.attachMovie("sakura", "sakura"+i, i); 8o4?o\8o9  
        t._alpha = 20+Math.random()*60; 7d^tps <  
        t._x = -(width/2)+Math.random()*(1.5*width); -C$ 20Q  
        t._y = -(height/2)+Math.random()*(1.5*height); D@jbe}8  
        t._xscale = t._yscale=50+Math.random()*(max_snowsize*10); kukl\rFe  
        t.k = 3+Math.random()*2; C3Fp`gwss  
        t.wind = -3.5+Math.random()*(1.4*3);
v0k lZW%'  
        t.onEnterFrame = mover; \^zM^!(  
    } 3wk>N-">v  
};
A^!EnT  
init(); %B9~Y)8?k  
![1$S!j  
4 $=szy  
?A4"{eb}  
那麼這樣一來你就只要記住這個效果叫 sakura, 其他的一概不管. 在要停止的時候就使用 ,f$so/seZ^  
.P`&C^  
sakura.removeMovieClip(); BbCj][$Y%  
:  k6tH  
H.Uw7xM  
這樣的一句就可以把 sakura 和其所蓋括的 movieclip 一併移除掉. 不是更加省事嗎?


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