>[danger]## 制作动画或小游戏——CreateJS事件
* * * * *
<div class = "post">
<div id="cnblogs_post_body" class="blogpost-body"><p>在Canvas中如果要添加事件,就需要计算坐标来模拟各种事件,而EaselJS中已经封装好了多个事件,只需调用即可。</p>
<h1>一、事件</h1>
<p><span style="color: #008000;"><strong><span style="font-size: 16px;">1)点击<br /></span></strong></span></p>
<p>事件是绑定在<span style="color: #3366ff;"><a href="http://www.createjs.cc/easeljs/docs/classes/Shape.html" target="_blank"><span style="color: #3366ff;">Shape</span></a></span>类中的,<span style="color: #3366ff;"><a href="http://www.createjs.cc/easeljs/docs/classes/Shape.html#event_click" target="_blank"><span style="color: #3366ff;">click</span></a></span>事件与DOM中的意思是一样的,还有个双击事件<span style="color: #3366ff;"><a href="http://www.createjs.cc/easeljs/docs/classes/Shape.html#event_dblclick" target="_blank"><span style="color: #3366ff;">dblckick</span></a></span>,完整的代码<span style="color: #3366ff;"><a href="http://codepen.io/strick/pen/WwJxPO" target="_blank"><span style="color: #3366ff;">可以查看这里</span></a></span>。</p>
<div class="cnblogs_code">
<pre><span style="color: #0000ff;">var</span> object = <span style="color: #0000ff;">new</span><span style="color: #000000;"> createjs.Shape();
object.addEventListener(</span>'click', <span style="color: #0000ff;">function</span><span style="color: #000000;">(e) {
alert(</span>'click'<span style="color: #000000;">);
});</span></pre>
</div>
<p>可以用<span style="color: #3366ff;"><a href="http://www.createjs.cc/easeljs/docs/classes/Shape.html#method_addEventListener" target="_blank"><span style="color: #3366ff;">addEventListener</span></a></span>来做绑定,如果想要让移动端也支持,就需要在<span style="color: #3366ff;"><a href="http://www.createjs.cc/easeljs/docs/classes/Touch.html" target="_blank"><span style="color: #3366ff;">Touch</span></a></span>类中调用<span style="color: #3366ff;"><a href="http://www.createjs.cc/easeljs/docs/classes/Touch.html#method_enable" target="_blank"><span style="color: #3366ff;">enable</span></a></span>方法。</p>
<div class="cnblogs_code">
<pre>createjs.Touch.enable(stage);</pre>
</div>
<p>除了click外,还有好多其它方法,Shape类的所有事件如下:</p>
<p><img src="https://images2015.cnblogs.com/blog/211606/201604/211606-20160416145004754-1471008975.jpg" alt="" /></p>
<p> </p>
<p><span style="color: #008000;"><strong><span style="font-size: 16px;">2)移动<br /></span></strong></span></p>
<p>在CSS中有一个“:hover”的伪类,通过这<span style="color: #3366ff;"><a href="http://www.createjs.cc/easeljs/docs/classes/Shape.html#event_mouseover" target="_blank"><span style="color: #3366ff;">mouseover</span></a></span>和<span style="color: #3366ff;"><a href="http://www.createjs.cc/easeljs/docs/classes/Shape.html#event_mouseout" target="_blank"><span style="color: #3366ff;">mouseout</span></a></span>两个事件可以模拟出这个效果。</p>
<p>首先要设置允许触发,引用stage类中的<span style="color: #3366ff;"><a href="http://www.createjs.cc/easeljs/docs/classes/Stage.html#method_enableMouseOver" target="_blank"><span style="color: #3366ff;">enableMouseOver</span></a></span>方法,完整的代码可以<span style="color: #3366ff;"><a href="http://codepen.io/strick/pen/YqLGzy" target="_blank"><span style="color: #3366ff;">在这里查看到</span></a></span>。</p>
<div class="cnblogs_code">
<pre><span style="color: #000000;">stage.enableMouseOver();
circle.addEventListener(</span>"mouseover", <span style="color: #0000ff;">function</span><span style="color: #000000;">(e) {
circle.alpha </span>= .5<span style="color: #000000;">;
stage.update();
});
circle.addEventListener(</span>"mouseout", <span style="color: #0000ff;">function</span><span style="color: #000000;">(e) {
circle.alpha </span>= 1<span style="color: #000000;">;
stage.update();
});</span></pre>
</div>
<p><img src="https://images2015.cnblogs.com/blog/211606/201604/211606-20160416150821535-650100092.gif" alt="" data-pinit="registered" /></p>
<p> </p>
<p>有两个事件与这两个比较类似,<span style="color: #3366ff;"><a href="http://www.createjs.cc/easeljs/docs/classes/Shape.html#event_rollover" target="_blank"><span style="color: #3366ff;">rollover</span></a></span>和<span style="color: #3366ff;"><a href="http://www.createjs.cc/easeljs/docs/classes/Shape.html#event_rollout" target="_blank"><span style="color: #3366ff;">rollout</span></a></span>,有两个地方与上面的两个事件不同。</p>
<p style="margin-left: 30px;">1. 这两个事件不冒泡</p>
<p style="margin-left: 30px;">2. 当给<span style="color: #3366ff;"><a href="http://www.createjs.cc/easeljs/docs/classes/Container.html" target="_blank"><span style="color: #3366ff;">Container</span></a></span>设置四个事件后,只有当离开容器时触发<span style="color: #3366ff;"><a href="http://www.createjs.cc/easeljs/docs/classes/Shape.html#event_rollout" target="_blank"><span style="color: #3366ff;">rollout</span></a></span>,进入容器时<span style="color: #3366ff;"><a href="http://www.createjs.cc/easeljs/docs/classes/Shape.html#event_rollover" target="_blank"><span style="color: #3366ff;">rollover</span></a></span>,而在容器中的子元素进入或离开会分别触发<span style="color: #3366ff;"><a href="http://www.createjs.cc/easeljs/docs/classes/Shape.html#event_mouseover" target="_blank"><span style="color: #3366ff;">mouseover</span></a></span>和<span style="color: #3366ff;"><a href="http://www.createjs.cc/easeljs/docs/classes/Shape.html#event_mouseout" target="_blank"><span style="color: #3366ff;">mouseout</span></a></span>。</p>
<p>下面gif图是一次鼠标移动的过程,总共打印出了四组数据,详细代码可以<span style="color: #3366ff;"><a href="http://codepen.io/strick/pen/ZWopWm" target="_blank"><span style="color: #3366ff;">查看这里</span></a></span>。</p>
<p>第一次:是从外面进入蓝色区域。</p>
<p>第二次:是从蓝色区域进入红色区域。</p>
<p>第三次:是从红色区域进入蓝色区域。</p>
<p>第四次:是从蓝色区域进入到外面。</p>
<p><img src="https://images2015.cnblogs.com/blog/211606/201604/211606-20160416155838816-2121845566.gif" alt="" data-pinit="registered" /></p>
<p> </p>
<p><span style="color: #008000;"><strong><span style="font-size: 16px;">3)拖放</span></strong></span></p>
<p>并没有专门的拖放事件,但是可以通过其它事件来模拟。</p>
<p>Shape类中有个<span style="color: #3366ff;"><a href="http://www.createjs.cc/easeljs/docs/classes/Shape.html#event_mousedown" target="_blank"><span style="color: #3366ff;">mousedown</span></a></span>事件,鼠标按下,在这个事件内绑定<span style="color: #3366ff;"><a href="http://www.createjs.cc/easeljs/docs/classes/Stage.html" target="_blank"><span style="color: #3366ff;">Stage</span></a></span>类的<span style="color: #3366ff;"><a href="http://www.createjs.cc/easeljs/docs/classes/Stage.html#event_stagemousemove" target="_blank"><span style="color: #3366ff;">stagemousemove</span></a></span>与<span style="color: #3366ff;"><a href="http://www.createjs.cc/easeljs/docs/classes/Stage.html#event_stagemouseup" target="_blank"><span style="color: #3366ff;">stagemouseup</span></a></span>,</p>
<div class="cnblogs_code">
<pre> circle.addEventListener('mousedown', <span style="color: #0000ff;">function</span><span style="color: #000000;">(e) {
stage.addEventListener(</span>'stagemousemove', <span style="color: #0000ff;">function</span><span style="color: #000000;">(e) {
circle.x </span>=<span style="color: #000000;"> stage.mouseX;
circle.y </span>=<span style="color: #000000;"> stage.mouseY;
});
stage.addEventListener(</span>'stagemouseup', <span style="color: #0000ff;">function</span><span style="color: #000000;">(e) {
e.target.removeAllEventListeners();
});
});</span></pre>
</div>
<p><img src="https://images2015.cnblogs.com/blog/211606/201604/211606-20160416164115207-972669928.gif" alt="" data-pinit="registered" /></p>
<p>完整的代码可以在<span style="color: #3366ff;"><a href="http://codepen.io/strick/pen/LNmRLp" target="_blank"><span style="color: #3366ff;">这里查看</span></a></span>,e.target就是canvas对象,内容如下:</p>
<p><img src="https://images2015.cnblogs.com/blog/211606/201604/211606-20160416163923504-1962365740.jpg" alt="" data-pinit="registered" /></p>
<p>Stage类中全部事件如下:</p>
<p><img src="https://images2015.cnblogs.com/blog/211606/201604/211606-20160416164249223-994115476.jpg" alt="" data-pinit="registered" /></p>
<p> </p>
<h1>二、颜色拖放小游戏</h1>
<p>可以通过上面这些事件制作一个简单的颜色拖放小游戏,详细的代码可以<span style="color: #3366ff;"><a href="http://codepen.io/strick/pen/bpMwoL" target="_blank"><span style="color: #3366ff;">查看这里</span></a></span>。</p>
<p><img src="https://images2015.cnblogs.com/blog/211606/201604/211606-20160417103359504-233523852.gif" alt="" data-pinit="registered" /></p>
<p>下面是一个片段:</p>
<div class="cnblogs_code">
<pre><span style="color: #008080;"> 1</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> startDrag(e) {
</span><span style="color: #008080;"> 2</span> <span style="color: #0000ff;">var</span> shape =<span style="color: #000000;"> e.target;
</span><span style="color: #008080;"> 3</span> <span style="color: #0000ff;">var</span> slot =<span style="color: #000000;"> slots[shape.key];
</span><span style="color: #008080;"> 4</span> stage.setChildIndex(shape, stage.getNumChildren() - 1<span style="color: #000000;">);
</span><span style="color: #008080;"> 5</span> stage.addEventListener('stagemousemove', <span style="color: #0000ff;">function</span><span style="color: #000000;">(e) {
</span><span style="color: #008080;"> 6</span> shape.x =<span style="color: #000000;"> e.stageX;
</span><span style="color: #008080;"> 7</span> shape.y =<span style="color: #000000;"> e.stageY;
</span><span style="color: #008080;"> 8</span> <span style="color: #000000;"> });
</span><span style="color: #008080;"> 9</span> stage.addEventListener('stagemouseup', <span style="color: #0000ff;">function</span><span style="color: #000000;">(e) {
</span><span style="color: #008080;">10</span> <span style="color: #000000;"> stage.removeAllEventListeners();
</span><span style="color: #008080;">11</span> <span style="color: #0000ff;">var</span> pt =<span style="color: #000000;"> slot.globalToLocal(stage.mouseX, stage.mouseY);
</span><span style="color: #008080;">12</span> <span style="color: #0000ff;">if</span><span style="color: #000000;"> (slot.hitTest(pt.x, pt.y)) {
</span><span style="color: #008080;">13</span> shape.removeEventListener("mousedown"<span style="color: #000000;">, startDrag);
</span><span style="color: #008080;">14</span> score++<span style="color: #000000;">;
</span><span style="color: #008080;">15</span> <span style="color: #000000;"> createjs.Tween.get(shape).to({
</span><span style="color: #008080;">16</span> <span style="color: #000000;"> x: slot.x,
</span><span style="color: #008080;">17</span> <span style="color: #000000;"> y: slot.y
</span><span style="color: #008080;">18</span> }, 200<span style="color: #000000;">, createjs.Ease.quadOut).call(checkGame);
</span><span style="color: #008080;">19</span> console.log('h'<span style="color: #000000;">);
</span><span style="color: #008080;">20</span> } <span style="color: #0000ff;">else</span><span style="color: #000000;"> {
</span><span style="color: #008080;">21</span> <span style="color: #000000;"> createjs.Tween.get(shape).to({
</span><span style="color: #008080;">22</span> <span style="color: #000000;"> x: shape.homeX,
</span><span style="color: #008080;">23</span> <span style="color: #000000;"> y: shape.homeY
</span><span style="color: #008080;">24</span> }, 200<span style="color: #000000;">, createjs.Ease.quadOut);
</span><span style="color: #008080;">25</span> <span style="color: #000000;"> }
</span><span style="color: #008080;">26</span> <span style="color: #000000;"> });
</span><span style="color: #008080;">27</span> }</pre>
</div>
<p> </p>
<p><span style="color: #008000;"><strong><span style="font-size: 16px;">1)事件中的e</span></strong></span></p>
<p>Shape类中的 <span style="color: #3366ff;"><a href="http://www.createjs.cc/easeljs/docs/classes/Stage.html#event_stagemousemove" target="_blank"><span style="color: #3366ff;">stagemousemove</span></a></span>和<span style="color: #3366ff;"><a href="http://www.createjs.cc/easeljs/docs/classes/Stage.html#event_stagemouseup" target="_blank"><span style="color: #3366ff;">stagemouseup</span></a></span>事件内的参数“<span style="color: #808080;">e</span>”是一个<span style="color: #3366ff;"><a href="http://www.createjs.cc/easeljs/docs/classes/MouseEvent.html" target="_blank"><span style="color: #3366ff;">MouseEvent</span></a></span>对象,“stageX”与“stageY”指的是鼠标在画布上的位置。</p>
<p> </p>
<p><span style="color: #008000;"><strong><span style="font-size: 16px;">2)setChildIndex</span></strong></span></p>
<p>Stage类中的<span style="color: #3366ff;"><a href="http://www.createjs.cc/easeljs/docs/classes/Stage.html#method_setChildIndex" target="_blank"><span style="color: #3366ff;">setChildIndex</span></a></span>这个应该与CSS中的“z-index”类似,就是为了不被覆盖住,拖动黄色块的时候,能将静止的块覆盖。</p>
<p><img src="https://images2015.cnblogs.com/blog/211606/201604/211606-20160417104712738-828765264.jpg" alt="" data-pinit="registered" /></p>
<p> </p>
<p><span style="color: #008000;"><strong><span style="font-size: 16px;">3)globalToLocal</span></strong></span></p>
<p>Shape类中<span style="color: #3366ff;"><a href="http://www.createjs.cc/easeljs/docs/classes/Shape.html#method_globalToLocal" target="_blank"><span style="color: #3366ff;">globalToLocal</span></a></span>方法是做坐标转换,上面的代码是将鼠标的X和Y坐标转换相对于图形的坐标。</p>
<p><img src="https://images2015.cnblogs.com/blog/211606/201604/211606-20160417110225363-1220611741.jpg" alt="" data-pinit="registered" /><img src="https://images2015.cnblogs.com/blog/211606/201604/211606-20160417110316816-21813444.jpg" alt="" data-pinit="registered" /><img src="https://images2015.cnblogs.com/blog/211606/201604/211606-20160417110345770-874921652.jpg" alt="" /></p>
<p>第一张图中:蓝色边框左上角的点相对画布原点是(50,30)</p>
<p>第二张图中:将鼠标移动到了(0,8)的位置</p>
<p>第三张图中:a就是经过计算后坐标点(0-50,8-30)</p>
<p> </p>
<p><span style="color: #008000;"><strong><span style="font-size: 16px;">4)hitTest</span></strong></span></p>
<p><span style="color: #3366ff;"><a href="http://www.createjs.cc/easeljs/docs/classes/Shape.html#method_hitTest" target="_blank"><span style="color: #3366ff;">hitTest</span></a></span>测试图像是否与颜色相同的框有交集,并且交集要过了边框的中心点才返回true。</p>
<p><img src="https://images2015.cnblogs.com/blog/211606/201604/211606-20160417114608879-215030636.jpg" alt="" data-pinit="registered" /><img src="https://images2015.cnblogs.com/blog/211606/201604/211606-20160417114724723-1589096880.jpg" alt="" data-pinit="registered" /></p>
<p>第一张图:过了中心点;第二张图:是没过中心点</p>
<p> </p>
<h1>三、Text</h1>
<p><span style="color: #008000;"><strong><span style="font-size: 16px;">1)创建文本</span></strong></span></p>
<p>在Canvas上可以画出文字,在CreateJS中同样可以,并且还封装了很多方法,将会使用<span style="color: #3366ff;"><a href="http://www.createjs.cc/easeljs/docs/classes/Text.html" target="_blank"><span style="color: #3366ff;">text</span></a></span>类。</p>
<div class="cnblogs_code">
<pre><span style="color: #0000ff;">var</span> text = <span style="color: #0000ff;">new</span> createjs.Text("Game Over", "20px Arial", "#ff7700"<span style="color: #000000;">);
text.textBaseline </span>= "middle"<span style="color: #000000;">;
text.textAlign </span>= "center"<span style="color: #000000;">;
stage.addChild(text);</span></pre>
</div>
<p>完整的代码可以在<span style="color: #3366ff;"><a href="http://codepen.io/strick/pen/GZdvzO" target="_blank"><span style="color: #3366ff;">这里查看</span></a></span>。通过画字,就可以模拟超链接了,还能把“:hover”通过事件模拟出来。</p>
<p><img src="https://images2015.cnblogs.com/blog/211606/201604/211606-20160417121315691-557833074.jpg" alt="" /></p>
<p> </p>
<p><span style="color: #008000;"><strong><span style="font-size: 16px;">2)DOMElement类</span></strong></span></p>
<p>CreateJS还能将canvas外面的DOM元素添加进来,完整的代码可以在<span style="color: #3366ff;"><a href="http://codepen.io/strick/pen/yOjowr" target="_blank"><span style="color: #3366ff;">这里查看</span></a></span>。</p>
<div class="cnblogs_code">
<pre><span style="color: #0000ff;"><</span><span style="color: #800000;">div </span><span style="color: #ff0000;">id</span><span style="color: #0000ff;">="gameHolder"</span><span style="color: #0000ff;">></span>
<span style="color: #0000ff;"><</span><span style="color: #800000;">div </span><span style="color: #ff0000;">id</span><span style="color: #0000ff;">="instructions"</span><span style="color: #ff0000;"> style</span><span style="color: #0000ff;">="width: 400px;height: 300px;border: dashed 2 #008b8b;text-align: center;visibility: hidden"</span><span style="color: #0000ff;">></span>
<span style="color: #0000ff;"><</span><span style="color: #800000;">h3 </span><span style="color: #ff0000;">style</span><span style="color: #0000ff;">="font-family:arial;"</span><span style="color: #0000ff;">></span>Game Instructions<span style="color: #0000ff;"></</span><span style="color: #800000;">h3</span><span style="color: #0000ff;">></span>
<span style="color: #0000ff;"><</span><span style="color: #800000;">p</span><span style="color: #0000ff;">><</span><span style="color: #800000;">strong</span><span style="color: #0000ff;">></span>Click<span style="color: #0000ff;"></</span><span style="color: #800000;">strong</span><span style="color: #0000ff;">></span> on the <span style="color: #0000ff;"><</span><span style="color: #800000;">span </span><span style="color: #ff0000;">style</span><span style="color: #0000ff;">="color:red"</span><span style="color: #0000ff;">></span>RED<span style="color: #0000ff;"></</span><span style="color: #800000;">span</span><span style="color: #0000ff;">></span> balloons as they fall from the sky.<span style="color: #0000ff;"></</span><span style="color: #800000;">p</span><span style="color: #0000ff;">></span>
<span style="color: #0000ff;"><</span><span style="color: #800000;">a </span><span style="color: #ff0000;">href</span><span style="color: #0000ff;">="http://www.cnblogs.com/strick/"</span><span style="color: #ff0000;"> target</span><span style="color: #0000ff;">="_blank"</span><span style="color: #0000ff;">></span>pwstrick<span style="color: #0000ff;"></</span><span style="color: #800000;">a</span><span style="color: #0000ff;">></span>
<span style="color: #0000ff;"></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span>
<span style="color: #0000ff;"><</span><span style="color: #800000;">canvas </span><span style="color: #ff0000;">id</span><span style="color: #0000ff;">="canvas"</span><span style="color: #ff0000;"> width</span><span style="color: #0000ff;">="500"</span><span style="color: #ff0000;"> height</span><span style="color: #0000ff;">="400"</span><span style="color: #ff0000;"> style</span><span style="color: #0000ff;">="border: black solid 1px"</span><span style="color: #0000ff;">></</span><span style="color: #800000;">canvas</span><span style="color: #0000ff;">></span>
<span style="color: #0000ff;"></</span><span style="color: #800000;">div</span><span style="color: #0000ff;">></span></pre>
</div>
<p>与上面的创建文本类似,也是初始化后,添加多个属性。</p>
<div class="cnblogs_code">
<pre><span style="color: #0000ff;">var</span> de = <span style="color: #0000ff;">new</span><span style="color: #000000;"> createjs.DOMElement(instructions);
de.alpha </span>= 0<span style="color: #000000;">;
de.regX </span>= 200<span style="color: #000000;">;
stage.addChild(de);</span></pre>
</div>
<p><img src="https://images2015.cnblogs.com/blog/211606/201604/211606-20160417122740785-806085166.gif" alt="" data-pinit="registered" /></p>
<p> </p></div><div id="MySignature"></div>
<div class="clear"></div>
<div id="blog_post_info_block">
<div id="BlogPostCategory"></div>
<div id="EntryTag"></div>
<div id="blog_post_info">
</div>
<div class="clear"></div>
<div id="post_next_prev"></div>
</div>
- 序言
- 第一章、基础引擎
- 第二章、了解API,开始写动画
- 第三章、从文档到动画(1)
- demo1源码
- demo2源码
- demo3源码
- demo4源码
- 第四章、从文档到动画(2)
- 第五章、从文档到动画(3)
- 第六章、精灵Sprite类和简明动画(1)
- 第七章、精灵图片的制作-奔跑的马儿
- 第八章、野人大冒险(项目实战)
- 第九章、绘制动画走秀
- 第十章、createjs的位图渲染(非常重要)
- 第十一章、createjs绘制视频
- 第十二章、createjs与animateCC协作6
- 第十三章、createjs与animateCC协作5
- 十四章、createjs与animateCC协作4
- 十五章、createjs与animateCC协作3
- 十六章、createjs与animateCC协作2
- 十七章、createjs与animateCC协作1
- 十八章、前端转做createjs的误区
- 十九、createjs与白鹭和laya的性能比较
- 二十、createjs近期发现的坑与解决办法
- 二十一、createjs推出新版本stageGL
- 二十二、createjs强制横屏方法
- 二十三、get、set以及简单的封装
- 二十四、createjs新手教程-前端向
- 二五、阻止createjs鼠标穿透的方法
- 二六、Creatine-createjs的游戏引擎
- 二七、createjs性能实测与性能优化实测
- 二八、createjs手势解锁效果demo
- 二九、给createjs新手的一点建议
- 三十、createjs进阶—sprite精灵图2
- 三一、createjs进阶—sprite精灵图1
- 三二、createjs性能优化(持续更新)
- 三三、createjs帧频显示代码
- 三四、createjs做的移动端展示站
- 三五、createjs进阶—createjs的OOP
- 三六、新手写createjs时容易遇到的坑
- 三七、createjs基础教程2-flashcc
- 三八、createjs基础教程1-flashcc
- 三九、stagegl的用法介绍和注意事项
- 四十、stagegl性能实测
- 四一、算法的艺术(附算法特效demo)
- 四二、来一波硬货,常用类
- 四三、面向canvas,更加简单的自适应方式
- 思思、再讲讲自适应-移动端自适应
- 四五、有关遮罩和图层叠加的问题(附刮刮卡demo)