Flash AS实现时钟效果(全脚本实现)

最近工作中用到个Flash效果,好久没有写FlashAS脚本了,就想从以前写的代码中找一些实例。竟然看到硬盘中还留有若干年前的代码。

这个时钟效果是全部采用脚本实现,图形也是用脚本绘制的。写于2005年,当然还是基于As2.0编写。现在想想自己当时也是闲得没事儿,竟然写这个东东。呵呵。2005年啊,8年前……无限回味啊,当初自己还是个小菜鸟,写出这样的东西真不容易,现在都想不起怎么写出来的了。里面用到了圆方程!当时刚出校门,理论基础还算扎实,现在什么都忘记了。

现在也可以用,使用很简单,把代码复制到帧的动作面板中即可。代码如下:

/*
一个表的效果,全AS绘制,只需把一下面的代码全复制到
第一帧就OK了。
作者:晴风 10522779
*/ //生成元件的起始深度值 var lv:Number = 100; //钟表的半径 var radius:Number = 100; // 钟表跟随鼠标的速度 var speed:Number = 5; //钟表的颜色值及透明度 var my_color = 0xB1C0F3; var my_line = 0xffffff; var my_alpha = 100; //用于圆形方程计算 var p:Number = 0; var n:Number = 0; //设定文本样式的函数 function text_CSS(word) { CSS = new TextFormat(); CSS.size = radius*.14; //文本居中 CSS.align = "center"; //文本的颜色 CSS.color = my_color; //CSS.color = Math.floor(Math.random()*255) << 16 | Math.floor(Math.random()*255) << 8 | Math.floor(Math.random()*255); word.setTextFormat(CSS); } //生成一续列,动态文本字段 for (i=0; i<=30; i++) { createTextField("time"+i, lv++, 0, 0, radius*0.2, radius*.2); this["time"+i].selectable = false; this["time"+i]._alpha = my_alpha/3; } //生成标志文本 createTextField("biaozi", lv++, 0, 0, radius*0.6, radius*0.2); biaozi.text = "晴风时钟"; biaozi._alpha = my_alpha/3; text_CSS(biaozi); //绘制中心点 createEmptyMovieClip("zhongxin", lv++); with (zhongxin) { beginFill(0xffffff, my_alpha); moveTo(-2, 2); lineTo(2, 2); lineTo(2, -2); lineTo(-2, -2); lineTo(-2, 2); endFill(); _x = 200; _y = 150; _width = _height=radius*.04; } //绘制时针 createEmptyMovieClip("shi", lv++); with (shi) { beginFill(my_color, my_alpha); lineStyle(1, my_line, my_alpha/2); moveTo(-5, 6); lineTo(0, 10); lineTo(5, 6); lineTo(3, -35); lineTo(0, -37); lineTo(-3, -35); lineTo(-5, 6); endFill(); _x = _parent.zhongxin._x; _y = _parent.zhongxin._y; _height = radius*.50; _width = _height*.25; } //绘制分针 createEmptyMovieClip("fen", lv++); with (fen) { beginFill(my_color, my_alpha/3*2); lineStyle(1, my_line, my_alpha); moveTo(-3, 10); lineTo(0, 13); lineTo(3, 10); lineTo(2, -60); lineTo(-2, -60); lineTo(-3, 10); endFill(); _x = _parent.zhongxin._x; _y = _parent.zhongxin._y; _height = radius*.65; _width = _height*.15; } //绘制秒针 createEmptyMovieClip("miao", lv++); with (miao) { beginFill(my_color, my_alpha/2); moveTo(-1.5, 15); lineTo(1.5, 15); lineTo(.5, -65); lineTo(-.5, -65); lineTo(-1.5, 15); endFill(); //绘制秒针上的红星 beginFill(my_color, my_alpha); moveTo(-2, -53); lineTo(2, -53); lineTo(2, -55); lineTo(-2, -55); lineTo(-2, -53); endFill(); _x = _parent.zhongxin._x; _y = _parent.zhongxin._y; _height = radius*.7; _width = _height*.05; } //绘制刻度 for (m=1; m<=12; m++) { createEmptyMovieClip("kedu"+m, lv++); with (this["kedu"+m]) { //刻度为几边形的图案 var num:Number = 6; //刻度的半径 var r:Number = radius*0.12; var p:Number = 2*Math.PI/num; var start_x:Array = new Array(); var start_y:Array = new Array(); for (i=0; i<num; i++) { start_x[i] = Math.sin(p*i)*r; start_y[i] = Math.cos(p*i)*r; lineStyle(.5, my_color, my_alpha/1.5); for (n=0; n<num; n++) { moveTo(start_x[i], start_y[i]); lineTo(start_x[n], start_y[n]); } } } } //中心点调到最前面 zhongxin.swapDepths(lv+1000); onEnterFrame = function () { now_date = new Date(); zhongxin._rotation = now_date.getSeconds()*12; miao._rotation = now_date.getSeconds()*6; fen._rotation = now_date.getMinutes()*6; // +now_date.getSeconds()*.1; shi._rotation = now_date.getHours()*30+now_date.getMinutes()*.5; // 当前时间数组,依次为:年,月,日,周,时,分,秒。 var now = new Array(now_date.getFullYear(), now_date.getMonth()+1, now_date.getDate(), now_date.getDay(), now_date.getHours(), now_date.getMinutes(), now_date.getSeconds()); var xingqi = new Array("日", "一", "二", "三", "四", "五", "六"); var riqi = now[0]+"年"+now[1]+"月"+now[2]+"日"+" "+"星期"+xingqi[now[3]]; var shijian = now[4]+"点"+now[5]+"分"+now[6]+"秒 "; var now_time:String; switch (Math.floor(now[4]/4)) { case 0 : now_time = riqi+" "+"凌晨"+" "+shijian; break; case 1 : now_time = riqi+" "+"早上"+" "+shijian; break; case 2 : now_time = riqi+" "+"上午"+" "+shijian; break; case 3 : now_time = riqi+" "+"中午"+" "+shijian; break; case 4 : now_time = riqi+" "+"下午"+" "+shijian; break; case 5 : now_time = riqi+" "+"晚上"+" "+shijian; break; } p = 2*Math.PI/now_time.length; for (i=0; i<=now_time.length; i++) { // 逐个给动态文本赋值 this["time"+i].text = now_time.charAt(i); // 引用函数text_CSS,设定动态文本样式 text_CSS(this["time"+i]); var mc = this["time"+i]; mc._y += ((Math.cos(n+p*i)*radius+zhongxin._y-radius*0.1)-mc._y)/speed; mc._x += ((Math.sin(n+p*i)*radius+zhongxin._x-radius*0.1)-mc._x)/speed; // mc._yscale = mc._xscale=(mc._y-bb._y)/2+100; mc.swapDepths(lv+mc._y-zhongxin._y); } // 转动速度 n -= .02; // 时针.分针.秒针,全部跟随中心点,中心点随鼠标 shi._x = fen._x=miao._x=zhongxin._x += (_xmouse+radius*1.2-zhongxin._x)/speed; shi._y = fen._y=miao._y=zhongxin._y += (_ymouse+radius*1.2-zhongxin._y)/speed; // 标志也跟随中心点 biaozi._x += (zhongxin._x-biaozi._x)/speed-biaozi._width/10; biaozi._y += (zhongxin._y+radius*0.3-biaozi._y)/speed; // 布置刻度 p = 2*Math.PI/12; for (i=1; i<=12; i++) { this["kedu"+i]._y += ((Math.cos(p*i)*radius*.75+zhongxin._y)-this["kedu"+i]._y)/speed; this["kedu"+i]._x += ((Math.sin(p*i)*radius*.75+zhongxin._x)-this["kedu"+i]._x)/speed; } updateAfterEvent(); };

 

 源文件下载:watch.rar

posted @ 2013-06-21 22:59  songsoft  阅读(2511)  评论(0编辑  收藏  举报