通过进度条来学习代码书写格式

      教程习题 2008-11-28 14:58

步骤:
1.打开Flash MX 2004,选择矩形工具,在主场景中画出下一个只有边框有矩形,本例该矩形大小为350*16像素。
2.再在主场景中仍用矩形工具画出一个只有填充而无边框的矩形,并按F8键将其转换为影片剪辑(注:其注册点一定要选在该矩形的最左侧),其实例名为bar 。本例该矩形大小为345*11像素。
3.将上述两矩形在主场景中排列好,使边框矩形嵌套填充矩形。
4.在上述两矩形旁边用文字工具拖出一动态文本框,其变量名为bar_per。

至此,准备工作就绪,即建立了两矩形框和一动态文本框,下面准备编写代码。

5.在主场景中,新建一层,选中该层第1帧,按F9键打开动作脚本编辑窗口,输入以下代码 :

this.onLoad=function(){
myBytesTotal=_root.getBytesTotal();
}
this.onLoad();
this.onEnterFrame=function()
{
myBytesLoaded=_root.getBytesLoaded();
bar_xscale=myBytesLoaded/myBytesTotal*100;
percent=Math.round(bar_xscale);
this.bar._xscale=bar_xscale;
this.bar_per=percent+"%";
if(myBytesLoaded==myBytesTotal){
delete this.onEnterFrame;
_root.nextFrame();
}else{
this.stop();
}
}6.从主场景时间轴第2帧起制作你的flash影片。

注解:



this.onLoad=function(){
myBytesTotal=_root.getBytesTotal();
}此段代码是指,当影片剪辑(本例指两矩形和一动态文本框所存在的主场景)加载时,读取主时间轴存在的所有元素的总字节数并赋值给变量myBytesTotal。







this.onLoad();
flash事件处理函数MovieClip.onLoad=function(){…}有些奇怪,其中设置的代码,若不在后面加上this.onLoad()
这些代码并不能执行,因此加上这一句以便这些代码得以执行。

myBytesLoaded=_root.getBytesLoaded();//读取主时间轴存在的所有元素已加载的字节数,并将其赋值给变量myBytesLoaded。④

bar_xscale=myBytesLoaded/myBytesTotal*100;//将myBytesTotal折算成100时,
myBytesLoaded所得到的折算值赋给变量bar_xscale,
以便给主场景中bar的_xscale赋值(_xscale的最大值只能为100),这里用到了初等数学的比例计算。⑤

percent=Math.round(bar_xscale);//将变量bar_xscale的值取整后赋给变量percent,以便显示的百分比不带小数。
}拓展:
1.“下载速度”的代码设计
①在主场景中用文字工具拖出有适当宽度的动态文本框,并设其变量名为rate 。
②在主场景代码层第1帧this.onEnterFrame=function(){}代码体if语句前追加如下代码:
t=getTimer();
rate= "下载速度:" + Math.round(myBytesLoaded/t * 100)/100 + " K/s";

2.“已用时间和剩余时间”的代码设计
①在主场景中用文字工具拖出有适当宽度的动态文本框,并设其变量名为mytimes 。
②在主场景代码层第1帧this.onEnterFrame=function(){}代码体if语句前追加如下代码:




timeLoaded=Math.round(t/1000);
timeRemain=Math.round(timeLoaded*(myBytesTotal-myBytesLoaded)/myBytesLoaded);
timeRemain=Math.round(timeRemain/60)+":"+Math.round(timeRemain%60);
timeLoaded=Math.round(timeLoaded/60)+":"+Math.round(timeLoaded%60);
mytimes="已用时间"+timeLoaded+" "+"剩余时间"+timeRemain; 注:若“下载速度”的代码没有设计,则上述代码前应追加代码 t=getTimer();
拓展后主场景代码层第1帧的全部代码如下:


this.onLoad=function(){
myBytesTotal=_root.getBytesTotal();
}
this.onLoad();
this.onEnterFrame=function(){
myBytesLoaded=_root.getBytesLoaded();
bar_xscale=myBytesLoaded/myBytesTotal*100;
percent=Math.round(bar_xscale);
this.bar._xscale=bar_xscale;
this.bar_per=percent+"%";
t=getTimer();
rate= "下载速度:" + Math.round(myBytesLoaded/t * 100)/100 + " K/s";
timeLoaded=Math.round(t/1000);
timeRemain=Math.round(timeLoaded*(myBytesTotal-myBytesLoaded)/myBytesLoaded);
timeRemain=Math.round(timeRemain/60)+":"+Math.round(timeRemain%60);
timeLoaded=Math.round(timeLoaded/60)+":"+Math.round(timeLoaded%60);
mytimes="已用时间"+timeLoaded+" "+"剩余时间"+timeRemain;
if(myBytesLoaded==myBytesTotal){
delete this.onEnterFrame;
_root.nextFrame();
}else{
this.stop();
}
}

标签集:TAGS:
回复Comments() 点击Count()

回复Comments

{commentauthor}
{commentauthor}
{commenttime}
{commentnum}
{commentcontent}
作者:
{commentrecontent}