createjs初学-制作一个简单的TextButton
来源:互联网 发布:淘宝男装毛衣 编辑:程序博客网 时间:2024/06/06 21:39
这个例子是从 Beginning HTML5 Games with CreateJS里学来的,我自己简单做了一个。
这个TextButton很简单,只有两部分,一个部分是button的Label,要显示的标签,使用createjs.Text实现;另外一部分是button的背景,会根据鼠标事件发生变化,使用createjs.Shape实现。
(createjs.Shape是继承自createjs.DisplayObject类的,但是和actionscript中的DisplayObject类不同,createjs.DisplayObject可以接受所有类型的鼠标事件)
简单介绍一下createjs.Text这个类:
继承自DisplayObject,用来显示一行或者多行文字。
注意:并不支持htmlText,每个Text只能显示为一种样式。要想使用多种样式,要创建多个Text。
var text = new createjs.Text("Hello World", "20px Arial", "#ff7700");
构造函数接受三个参数,都是可选的。第一个是要显示的文本;第二个是使用的字体;第三个是使用的颜色。
textAlign属性表示横向 对齐的方式,它的值可以是start、end、left、right、center。
textBaseLine属性表示竖直对其方式,它的值可以是top、hanging、middle、alphabetic、ideographic、bottom。
下面是TextButton类的代码
(function(window){ function SHLTextButton(text,bwidth,bheight,textColor,fontsize,upColor,downColor)//在构造函数中穿一些必要的参数进来 { this.Container_constructor(); this.buttonText=text; //要显示是文字 this.bwidth=bwidth; //按钮的宽度 this.bheight=bheight; //按钮的高度 this.textColor=textColor; //文字的颜色 this.fontsize=fontsize; //文字的字体大小 this.upColor=upColor; //按钮正常状态的颜色 this.downColor=downColor; //按钮按下的颜色 this.labelText; //用于显示文字的Text this.bg; //用于显示背景的Shape this.drawButtton(); //绘制Button this.setListener(); //设置事件侦听 } var p=createjs.extend(SHLTextButton,createjs.Container); //SHLTextButton这个继承自createjs.Container p.drawButtton=function() { this.removeAllChildren(); //创建了text,并设置了对其方式和位置,这样就可以居中显示 this.labelText=new createjs.Text(this.buttonText,this.fontsize+"px Microsoft YaHei",this.textColor); this.labelText.textAlign="center"; this.labelText.textBaseline="middle"; this.labelText.x=this.bwidth/2; this.labelText.y=this.bheight/2; //调用drawBG方法绘制了背景 this.bg=new createjs.Shape(); this.drawBG(this.upColor); this.addChild(this.bg); this.addChild(this.labelText); }; //添加mousedown和pressup的事件侦听 p.setListener=function() { this.on("mousedown",function(e) { this.drawBG(this.downColor); }); this.on("pressup",function(e){ this.drawBG(this.upColor); }); }; //这个方法根据背景的颜色绘制了一个圆角矩形的背景 p.drawBG=function(bgColor) { this.bg.graphics.clear(); this.bg.graphics.beginFill(bgColor); this.bg.graphics.drawRoundRect(0,0,this.bwidth,this.bheight,4,4); this.bg.graphics.endFill(); }; window.SHLTextButton=createjs.promote(SHLTextButton,"Container");}(window));
这里事件侦听用的是on这个方法,不是addEventListener这个方法。
on ( type listener [scope] [once=false] [data] [useCapture=false] )
on是addEventListener的一个简洁的写法,但是提供了更多的功能。
type和listener和之前一样,表示要侦听的事件和调用的方法,
scope表示可以指定listener中的作用域,
once表示是否只侦听一次,如果是true,事件第一次相应之后,listener就会自动移除,
data是附加参数,可以在listener里面添加第二个参数
function handleClick(evt, data)
这样就可以读取到data数据,
useCapture表明是侦听捕获阶段还是冒泡阶段,默认是false。
最后给出一个使用的例子,也很简单
var bigger=new SHLTextButton("放大",50,20,"#ffffff",14,"#999999","#66666");bigger.on("click",onButtonClick);function onButtonClick(e){ console.log(e.currentTarget.buttonText);}
- createjs初学-制作一个简单的TextButton
- createjs初学-简单的图片拖拽
- createjs初学-创建一个button
- createjs初学-preloadJS的使用
- createjs初学-preloadJS的使用
- createjs初学-关于cache
- createjs初学-关于Ticker
- createjs事件侦听的一个小坑
- createjs初学-所有的显示对象介绍(1)
- createjs初学-所有的显示对象介绍(2)
- createjs初学-BitmapText使用实例
- 制作一个简单的setup
- 制作一个简单的计算器
- 一个简单的外挂制作
- 制作一个简单的文件系统
- 制作一个简单的动画
- 一个简单的表格制作
- 制作一个简单的画板
- Push和Pop操作原理
- C语言static 详解
- 1020. Tree Traversals (25)和1385,重建二叉树
- nginx结合tomcat使用
- 使用Hibernate SQLQuery执行原生SQL
- createjs初学-制作一个简单的TextButton
- ArcGIS Runtime for Android开发教程V2.0(4)基础篇---MapView
- maven调入依赖包
- 最少拦截系统-贪心或最长上升子序列
- 一个空格引起的访问404问题
- Eclipse导入项目:No projects are found to import
- POJ - 3264 - Balanced Lineup (线段树)
- iOS—UI-手势及控件(segment switch slider)的使用
- c++ const 成员函数 & 临时变量 & 右值引用 & move