一个纯代码写的AS3按钮类

分类: Flash, 代码 | 标签: , | 日期:2008-02-18 | 1 views

因为是第一次用类定义来写,难免代码效率会低.
因为想要用纯代码写一个支持多种频谱效果的AS3播放器,更想将所有需要的东西全都装进包里.因此,没打算用Flash自带的组件,一是组件拖动到库里之后,导出的文件大小呈几何数量级翻倍,二是,借此机会,真正和类亲密接触一回.
首先,将按钮类实现了.发现自己用代码绘制按钮并不难,但是需要很仔细的计算好每个细节,包括鼠标按下按钮之后移出按钮区域又移回来时候的按钮绘制.代码如下,调用的时候还得自己将函数添加到侦听列表中.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.events.*;
import flash.display.GradientType;
import flash.geom.Matrix;
import flash.display.CapsStyle;
import flash.display.LineScaleMode;
class coolbutton extends Sprite {
private var mybutton:Sprite;
private var colormatrix:Matrix;
private var colorarray:Array;
private var ispressed:Boolean;
public function coolbutton(height:Number=32,width:Number=64) {
mybutton = new Sprite();
colorarray = new Array();
ispressed = false;
colorarray = [0,148,148,255];
colormatrix = new Matrix();
colormatrix.createGradientBox(width,height,Math.PI/2,0,0);
mybutton.height = height;
mybutton.width = width;
graphics.lineStyle(1,0x707070,1,true,LineScaleMode.NONE,CapsStyle.NONE,null,3);
graphics.drawRoundRect(3,3,width-1,height-1,3,3);
graphics.lineStyle(1,0xF3F3F3,1,true,LineScaleMode.NONE,CapsStyle.NONE,null,3);
graphics.beginGradientFill(GradientType.LINEAR,[0xF5F5F5, 0xE0E0E0, 0xCFCFCF, 0xC0C0C0],[1,1,1,1],colorarray,colormatrix);
graphics.drawRoundRect(4,4,width-3,height-3,3,3);
graphics.endFill();
}
public function onhover(event:Event) {
var height = event.target.height;
var width = event.target.width;
if (ispressed) {
graphics.lineStyle (1,0x2C628B,1,true,LineScaleMode.NONE,CapsStyle.NONE,null,3);
graphics.drawRoundRect(3,3,width-1,height-1,3,3);
graphics.lineStyle (1,0x63ACD3,1,true,LineScaleMode.NONE,CapsStyle.NONE,null,3);
graphics.beginGradientFill(GradientType.LINEAR,[0xE5F4FC, 0xC4E5F6, 0x98D1EF, 0x6DB6DD],[1,1,1,1],colorarray,colormatrix);
graphics.drawRoundRect(4,4,width-3,height-3,3,3);
graphics.endFill();
} else {
graphics.lineStyle (1,0x3C7FBE,1,true,LineScaleMode.NONE,CapsStyle.NONE,null,3);
graphics.drawRoundRect(3,3,width-1,height-1,3,3);
graphics.lineStyle (1,0xEFF9FE,1,true,LineScaleMode.NONE,CapsStyle.NONE,null,3);
graphics.beginGradientFill(GradientType.LINEAR,[0xF5F5F5, 0xD9F0FC, 0xBEE6FD, 0xA7D9F5],[1,1,1,1],colorarray,colormatrix);
graphics.drawRoundRect(4,4,width-3,height-3,3,3);
graphics.endFill();
}
}
public function onout(event:Event) {
var height = event.target.height;
var width = event.target.width;
graphics.lineStyle(1,0x707070,1,true,LineScaleMode.NONE,CapsStyle.NONE,null,3);
graphics.drawRoundRect(3,3,width-1,height-1,3,3);
graphics.lineStyle(1,0xF3F3FF,1,true,LineScaleMode.NONE,CapsStyle.NONE,null,3);
graphics.beginGradientFill(GradientType.LINEAR,[0xF5F5F5, 0xE0E0E0, 0xCFCFCF, 0xC0C0C0],[1,1,1,1],colorarray,colormatrix);
graphics.drawRoundRect(4,4,width-3,height-3,3,3);
graphics.endFill();
}
public function onpress(event:Event) {
var height = event.target.height;
var width = event.target.width;
ispressed = true;
graphics.lineStyle(1,0x2C628B,1,true,LineScaleMode.NONE,CapsStyle.NONE,null,3);
graphics.drawRoundRect(3,3,width-1,height-1,3,3);
graphics.lineStyle(1,0x63ACD3,1,true,LineScaleMode.NONE,CapsStyle.NONE,null,3);
graphics.beginGradientFill(GradientType.LINEAR,[0xE5F4FC, 0xC4E5F6, 0x98D1EF, 0x6DB6DD],[1,1,1,1],colorarray,colormatrix);
graphics.drawRoundRect(4,4,width-3,height-3,3,3);
graphics.endFill();
}
public function onrelease(event:Event) {
var height = event.target.height;
var width = event.target.width;
ispressed = false;
graphics.lineStyle(1,0x707070,1,true,LineScaleMode.NONE,CapsStyle.NONE,null,3);
graphics.drawRoundRect(3,3,width-1,height-1,3,3);
graphics.lineStyle(1,0xF3F3F3,1,true,LineScaleMode.NONE,CapsStyle.NONE,null,3);
graphics.beginGradientFill(GradientType.LINEAR,[0xF5F5F5, 0xE0E0E0, 0xCFCFCF, 0xC0C0C0],[1,1,1,1],colorarray,colormatrix);
graphics.drawRoundRect(4,4,width-3,height-3,3,3);
graphics.endFill();
}
public function onClick(event:Event) {
trace(event.target,'is just being clicked');
}
}

你也许会喜欢的日志

8人发表了评论  ↓发表评论↓
  • 看得我都崩溃了~“

    [回复]

    lx 回复:

    别灰心,慢慢看。

    [回复]

    haha 回复:

    学习了

    [回复]

    lx 回复:

    互相学习.呵呵

    carol @ 2008年02月20日

    回复
  • Sho-to-ka-ka-a-like:)

    [回复]

    IgnatIgnaty @ 2008年11月6日

    回复
  • I think that KaKA is the best player in the world

    [回复]

    lx 回复:

    I’ll search and try this

    [回复]

    dating advice for men @ 2008年12月23日

    回复
  • Вы производите обмен постовыми?

    здесь видел ет gamebulletin.ru

    [回复]

    Edurbibra @ 2009年05月25日

    回复

[ Ctrl+Enter提交 ]

使用新浪微博登陆