>[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)