2007-04-02
[教學]如何控制好Flash特效
|
作者 : Super-Tomato 這篇並不是一步步的教你如何製作任何效果, 只是要提醒那些從別處網站得到coding後依樣畫葫蘆得到了效果卻不會讓效果消失的朋友, 如何以比較好的方式來控制. 以下是從別處拿來的花瓣飄落效果. mover = function () { this._y += this.k; this._x += this.wind; if (this._y>height+10) { this._y = -20; } if (this._x>width+20) { this._x = -(width/2)+Math.random()*(1.5*width); this._y = -20; } else if (this._x<-20) { this._x = -(width/2)+Math.random()*(1.5*width); this._y = -20; } }; init = function () { width = 800; // pixels height = 600; // pixels max_snowsize = 5; // pixels snowflakes = 30; // quantity for (i=0; i<snowflakes; i++) { t = attachMovie("sakura", "sakura"+i, i); t._alpha = 20+Math.random()*60; t._x = -(width/2)+Math.random()*(1.5*width); t._y = -(height/2)+Math.random()*(1.5*height); t._xscale = t._yscale=50+Math.random()*(max_snowsize*10); t.k = 3+Math.random()*2; t.wind = -3.5+Math.random()*(1.4*3); //這裡開始花瓣會通過onEnterFrame的事件不停的在執行 t.onEnterFrame = mover; } }; init(); 以這樣的方式在之後的50個frame之後加上 stop(); 指令的話只能讓frame停止播放, 但花瓣的飄落卻無法停止, 原因就是出在 onEnterFrame 這個 event 上. 要讓花瓣停止的為一方法就是要停止這個 onEnterFrame, 那麼能夠做的就是 1. 刪除 onEnterFrame 事件, 2. 刪除所有花瓣, 那麼每片花瓣所擁有的屬性和事件也會一併的刪除. 當然我們會選擇第二的方法, 直接把花瓣刪除, 不然你讓花瓣停止了卻不消失有何用? 所以你就必須取得目前製作了多少片花瓣, snowflakes的數量, 然後再通過 for loop 把所有的花瓣removeMovieClip. 如: for (i=0; i<snowflakes; i++) this["sakura"+i].removeMovieClip(); 這樣雖然沒問題.... 但在製作一個比較大型的 flash 時, 你不可能會把所有的 variable 都記住. 要提升自己的製作速度就必須把所有東西都規劃好, 那麼你就該這樣做. mover = function () { this._y += this.k; this._x += this.wind; if (this._y>height+10) { this._y = -20; } if (this._x>width+20) { this._x = -(width/2)+Math.random()*(1.5*width); this._y = -20; } else if (this._x<-20) { this._x = -(width/2)+Math.random()*(1.5*width); this._y = -20; } }; init = function () { width = 800; // pixels height = 600; // pixels max_snowsize = 5; // pixels snowflakes = 30; // quantity //建立一個叫sakura的空MovieClip. this.createEmptyMovieClip("sakura", this.getNextHighestDepth()); for (i=0; i<snowflakes; i++) { //把所有的花瓣attach到這個sakura空MovieClip中. t = sakura.attachMovie("sakura", "sakura"+i, i); t._alpha = 20+Math.random()*60; t._x = -(width/2)+Math.random()*(1.5*width); t._y = -(height/2)+Math.random()*(1.5*height); t._xscale = t._yscale=50+Math.random()*(max_snowsize*10); t.k = 3+Math.random()*2; t.wind = -3.5+Math.random()*(1.4*3); t.onEnterFrame = mover; } }; init(); 那麼這樣一來你就只要記住這個效果叫 sakura, 其他的一概不管. 在要停止的時候就使用 sakura.removeMovieClip(); 這樣的一句就可以把 sakura 和其所蓋括的 movieclip 一併移除掉. 不是更加省事嗎? |
一共有 0 条评论