佳礼资讯网

 找回密码
 注册

ADVERTISEMENT

查看: 1621|回复: 3

[教學]如何控制好效果

[复制链接]
发表于 2-4-2007 03:39 PM | 显示全部楼层 |阅读模式
作者 : 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 一併移除掉. 不是更加省事嗎?
回复

使用道具 举报


ADVERTISEMENT

发表于 16-4-2007 03:49 PM | 显示全部楼层

回复 #1 super-tomato 的帖子

多谢tomato大大的解答,as是我最头痛的地方..
虽然还不是很懂,我会继续努力。。。
回复

使用道具 举报

发表于 24-4-2007 01:25 AM | 显示全部楼层
多谢你的提醒
真好
回复

使用道具 举报

发表于 14-5-2007 01:55 PM | 显示全部楼层
那么好的原创帖子,怎么都没有人要求加入精华,报告报告版主把主题加入精华!!!!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

 

ADVERTISEMENT



ADVERTISEMENT



ADVERTISEMENT

ADVERTISEMENT


版权所有 © 1996-2023 Cari Internet Sdn Bhd (483575-W)|IPSERVERONE 提供云主机|广告刊登|关于我们|私隐权|免控|投诉|联络|脸书|佳礼资讯网

GMT+8, 13-5-2024 03:03 AM , Processed in 0.054933 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表