原文:http://www.jb51.cc/article/p-kimawtbd-bho.html
自己学了快两个月的cocos2d-x了,前几天自己看着教学视频,跟着做了一个小游戏——别踩白块。
今天来说一说我自己是如何写这个项目的。逻辑有点乱,不知道看不看得懂。。。。
首先,这个游戏有两个场景,一个是开始场景,一个是失败场景。
开始场景如下:
开始场景里面有三类块:
- 起始块(黄色,有Start Game)
- 正常块(黑白块)
- 结束块(绿色,有You Win)
起始块如下:
正常块如下:
结束块如下:
因为这个游戏都是以块为单位的所以可以将这些块统称为抽象块,所以要给块来写一个 create 方法如下:
这个函数里面有五个参数,第一个参数是块的大小 size ;第二个参数是块的颜色 color ;第三个参数是在块上要显示的字符串 str ;第四个参数是字符串的大小 strSize ;第五个参数是字符串的颜色 strColor 。
我们创建块的时候要确定块的大小,如
- 起始块:宽为,winSize.width;高为,winSize.height/4
- 正常块:宽为,winSize.width/4;高为,winSize.height/4
这里除以4是将屏幕给分成了4×4个正常块 - 结束块:宽为,winSize.width;高为,winSize.height
注意:这里的 winSize 就是屏幕的大小。
现在把 Block 类写好:
将 Block 类写好后,就可以开始写游戏逻辑了。
- 游戏一开是就进入开始场景,只要玩家一开始点击黑块游戏就会开始。
- 玩家只能按顺序点击黑块,即每次都是点击最下面的黑块。
- 玩家只要正确地点击了黑块,那个被点击的黑块就会变成灰色,
被点击那一行块就会往下移动,并且在屏幕上方添加一新行。 - 玩家在点击玩所有的黑块后,即赢得胜利,出现结束块。
- 若玩家点到了白块则游戏失败,切换到游戏失败的场景。
失败场景如下:
开始游戏,我们需要将屏幕最开始的布局给设置好,大家可能已经注意到 Block 类中有一个行号函数
Box-sizing: border-Box; margin-top: 0px; margin-bottom: 1.1em; font-family: 'Source Code Pro',monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">CC_SYNTHESIZE(<span class="hljs-keyword" style="color: rgb(0,LineIndex);</code><ul class="pre-numbering" style="Box-sizing: border-Box; position: absolute; width: 50px; background-color: rgb(238,221); list-style: none; text-align: right;"><li style="Box-sizing: border-Box; padding: 0px 5px;">1</li></ul>
行号函数就是用来设置(set)和得到(get)块的行号的,行号在屏幕上只有0到3行,
如下图所示
在开始的布局中第0行添加开始块,第 1,2,3行添加正常块。
在点击完所有的黑块后添加结束块。
Box-sizing: border-Box; margin-top: 0px; margin-bottom: 1.1em; font-family: 'Source Code Pro',102); Box-sizing: border-Box;">::addNormalLineBlocks</span>(int LineIndex) { Size normalBlockSize <span class="hljs-subst" style="color: rgb(0,102); Box-sizing: border-Box;">.</span>width/<span class="hljs-number" style="color: rgb(0,102); Box-sizing: border-Box;">4</span>,102); Box-sizing: border-Box;">4</span>); int idx <span class="hljs-subst" style="color: rgb(0,0); Box-sizing: border-Box;">=</span> rand()<span class="hljs-subst" style="color: rgb(0,0); Box-sizing: border-Box;">%</span><span class="hljs-number" style="color: rgb(0,102); Box-sizing: border-Box;">4</span>;<span class="hljs-comment" style="color: rgb(136,0); Box-sizing: border-Box;">//取一个0到3之间的随机值</span> for(int i <span class="hljs-subst" style="color: rgb(0,0); Box-sizing: border-Box;">=</span> <span class="hljs-number" style="color: rgb(0,102); Box-sizing: border-Box;">0</span>; i <span class="hljs-subst" style="color: rgb(0,0); Box-sizing: border-Box;"><</span> <span class="hljs-number" style="color: rgb(0,102); Box-sizing: border-Box;">4</span>; i<span class="hljs-subst" style="color: rgb(0,0); Box-sizing: border-Box;">++</span>)<span class="hljs-comment" style="color: rgb(136,0); Box-sizing: border-Box;">//一下添加一行,即4块</span> { Block <span class="hljs-subst" style="color: rgb(0,102); Box-sizing: border-Box;">::create</span>(normalBlockSize,i <span class="hljs-subst" style="color: rgb(0,0); Box-sizing: border-Box;">==</span> idx <span class="hljs-subst" style="color: rgb(0,0); Box-sizing: border-Box;">?</span> Color3B<span class="hljs-tag" style="color: rgb(0,102); Box-sizing: border-Box;">::BLACK</span> : Color3B<span class="hljs-tag" style="color: rgb(0,102); Box-sizing: border-Box;">::WHITE</span>,<span class="hljs-comment" style="color: rgb(136,0); Box-sizing: border-Box;">/*设置黑白块*/</span> <span class="hljs-string" style="color: rgb(0,0); Box-sizing: border-Box;">""</span>,102); Box-sizing: border-Box;">::WHITE</span>); b<span class="hljs-subst" style="color: rgb(0,0); Box-sizing: border-Box;">-></span>setLineIndex(LineIndex); <span class="hljs-comment" style="color: rgb(136,0); Box-sizing: border-Box;">//根据 i 和 LineIndex 来设置位置</span> b<span class="hljs-subst" style="color: rgb(0,0); Box-sizing: border-Box;">-></span>setPosition(Point( i <span class="hljs-subst" style="color: rgb(0,0); Box-sizing: border-Box;">*</span> winSize<span class="hljs-built_in" style="color: rgb(102,LineIndex <span class="hljs-subst" style="color: rgb(0,102); Box-sizing: border-Box;">4</span> ) ); this<span class="hljs-subst" style="color: rgb(0,0); Box-sizing: border-Box;">-></span>addChild(b); } _lineCount<span class="hljs-subst" style="color: rgb(0,0); Box-sizing: border-Box;">//行数+1</span> }</code><ul class="pre-numbering" style="Box-sizing: border-Box; position: absolute; width: 50px; background-color: rgb(238,221); list-style: none; text-align: right;"><li style="Box-sizing: border-Box; padding: 0px 5px;">1</li><li style="Box-sizing: border-Box; padding: 0px 5px;">2</li><li style="Box-sizing: border-Box; padding: 0px 5px;">3</li><li style="Box-sizing: border-Box; padding: 0px 5px;">4</li><li style="Box-sizing: border-Box; padding: 0px 5px;">5</li><li style="Box-sizing: border-Box; padding: 0px 5px;">6</li><li style="Box-sizing: border-Box; padding: 0px 5px;">7</li><li style="Box-sizing: border-Box; padding: 0px 5px;">8</li><li style="Box-sizing: border-Box; padding: 0px 5px;">9</li><li style="Box-sizing: border-Box; padding: 0px 5px;">10</li><li style="Box-sizing: border-Box; padding: 0px 5px;">11</li><li style="Box-sizing: border-Box; padding: 0px 5px;">12</li><li style="Box-sizing: border-Box; padding: 0px 5px;">13</li><li style="Box-sizing: border-Box; padding: 0px 5px;">14</li><li style="Box-sizing: border-Box; padding: 0px 5px;">15</li><li style="Box-sizing: border-Box; padding: 0px 5px;">16</li><li style="Box-sizing: border-Box; padding: 0px 5px;">17</li><li style="Box-sizing: border-Box; padding: 0px 5px;">18</li></ul>
到这里,我们就可以设置好最开始的布局了,写一个StatGame函数如下:现在来有了最开始的布局了,就可以开始写点击事件了,因为我看的视频教程是cocos2d-x3.0以下的版本,而我自己用的却是3.0的版本,所以,就造成了我写的代码3.0以下的版本和3.0版本的混搭……
首先设置触摸监听:
Box-sizing: border-Box; margin-top: 0px; margin-bottom: 1.1em; font-family: 'Source Code Pro',monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"> <span class="hljs-comment" style="color: rgb(136,0); Box-sizing: border-Box;">//3.0版本</span> auto listener <span class="hljs-subst" style="color: rgb(0,0); Box-sizing: border-Box;">=</span> EventListenerTouchOneByOne<span class="hljs-tag" style="color: rgb(0,102); Box-sizing: border-Box;">::create</span>(); listener<span class="hljs-subst" style="color: rgb(0,0); Box-sizing: border-Box;">-></span>setSwallowTouches(<span class="hljs-literal" style="color: rgb(0,102); Box-sizing: border-Box;">true</span>);<span class="hljs-comment" style="color: rgb(136,0); Box-sizing: border-Box;">//吞噬</span> listener<span class="hljs-subst" style="color: rgb(0,0); Box-sizing: border-Box;">-></span>onTouchBegan <span class="hljs-subst" style="color: rgb(0,0); Box-sizing: border-Box;">=</span> CC_CALLBACK_2(LayerGame<span class="hljs-tag" style="color: rgb(0,102); Box-sizing: border-Box;">::onTouchBegan</span>,this); this<span class="hljs-subst" style="color: rgb(0,0); Box-sizing: border-Box;">-></span>_eventDispatcher<span class="hljs-subst" style="color: rgb(0,0); Box-sizing: border-Box;">-></span>addEventListenerWithSceneGraPHPriority(listener,this);</code><ul class="pre-numbering" style="Box-sizing: border-Box; position: absolute; width: 50px; background-color: rgb(238,221); list-style: none; text-align: right;"><li style="Box-sizing: border-Box; padding: 0px 5px;">1</li><li style="Box-sizing: border-Box; padding: 0px 5px;">2</li><li style="Box-sizing: border-Box; padding: 0px 5px;">3</li><li style="Box-sizing: border-Box; padding: 0px 5px;">4</li><li style="Box-sizing: border-Box; padding: 0px 5px;">5</li></ul>然后在写点击事件函数:
Box-sizing: border-Box; margin-top: 0px; margin-bottom: 1.1em; font-family: 'Source Code Pro',monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;">bool LayerGame<span class="hljs-tag" style="color: rgb(0,102); Box-sizing: border-Box;">::onTouchBegan</span>(Touch <span class="hljs-subst" style="color: rgb(0,0); Box-sizing: border-Box;">*</span>touch,Event <span class="hljs-subst" style="color: rgb(0,0); Box-sizing: border-Box;">*</span>unused_event) { <span class="hljs-comment" style="color: rgb(136,0); Box-sizing: border-Box;">//遍历所有存在vec中的块 C++11方法</span> for (auto obj : Block<span class="hljs-tag" style="color: rgb(0,102); Box-sizing: border-Box;">::getBlockVector</span>()) { Block <span class="hljs-subst" style="color: rgb(0,0); Box-sizing: border-Box;">=</span> (Block <span class="hljs-subst" style="color: rgb(0,0); Box-sizing: border-Box;">*</span>)obj; <span class="hljs-comment" style="color: rgb(136,0); Box-sizing: border-Box;">//判断是否点击到该块,且行号为1</span> <span class="hljs-keyword" style="color: rgb(0,136); Box-sizing: border-Box;">if</span> (b<span class="hljs-subst" style="color: rgb(0,0); Box-sizing: border-Box;">-></span>boundingBox()<span class="hljs-built_in" style="color: rgb(102,102); Box-sizing: border-Box;">.</span>containsPoint(touch<span class="hljs-subst" style="color: rgb(0,0); Box-sizing: border-Box;">-></span>getLocation()) <span class="hljs-subst" style="color: rgb(0,0); Box-sizing: border-Box;">&&</span> b<span class="hljs-subst" style="color: rgb(0,0); Box-sizing: border-Box;">-></span>getLineIndex() <span class="hljs-subst" style="color: rgb(0,0); Box-sizing: border-Box;">==</span> <span class="hljs-number" style="color: rgb(0,102); Box-sizing: border-Box;">1</span>) { <span class="hljs-keyword" style="color: rgb(0,0); Box-sizing: border-Box;">-></span>getColor() <span class="hljs-subst" style="color: rgb(0,0); Box-sizing: border-Box;">==</span> Color3B<span class="hljs-tag" style="color: rgb(0,102); Box-sizing: border-Box;">::BLACK</span>)<span class="hljs-comment" style="color: rgb(136,0); Box-sizing: border-Box;">//点到黑块</span> { <span class="hljs-comment" style="color: rgb(136,0); Box-sizing: border-Box;">//播放音效</span> SimpleAudioEngine<span class="hljs-tag" style="color: rgb(0,102); Box-sizing: border-Box;">::getInstance</span>()<span class="hljs-subst" style="color: rgb(0,0); Box-sizing: border-Box;">-></span>playEffect(<span class="hljs-string" style="color: rgb(0,0); Box-sizing: border-Box;">"onclick.wav"</span>); <span class="hljs-comment" style="color: rgb(136,0); Box-sizing: border-Box;">//开始计时</span> this<span class="hljs-subst" style="color: rgb(0,0); Box-sizing: border-Box;">-></span>startTimer(); <span class="hljs-comment" style="color: rgb(136,0); Box-sizing: border-Box;">//将被点击的黑块变灰</span> b<span class="hljs-subst" style="color: rgb(0,0); Box-sizing: border-Box;">-></span>setColor(Color3B<span class="hljs-tag" style="color: rgb(0,102); Box-sizing: border-Box;">::GRAY</span>); <span class="hljs-comment" style="color: rgb(136,0); Box-sizing: border-Box;">//被点击的这行下移</span> this<span class="hljs-subst" style="color: rgb(0,0); Box-sizing: border-Box;">-></span>moveDown(); } <span class="hljs-keyword" style="color: rgb(0,136); Box-sizing: border-Box;">else</span> <span class="hljs-keyword" style="color: rgb(0,102); Box-sizing: border-Box;">::GREEN</span> )<span class="hljs-comment" style="color: rgb(136,0); Box-sizing: border-Box;">//点到结束块</span> { <span class="hljs-comment" style="color: rgb(136,0); Box-sizing: border-Box;">"gamewin.wav"</span>); <span class="hljs-comment" style="color: rgb(136,0); Box-sizing: border-Box;">//终止计时</span> this<span class="hljs-subst" style="color: rgb(0,0); Box-sizing: border-Box;">-></span>stopTimer(); <span class="hljs-comment" style="color: rgb(136,0); Box-sizing: border-Box;">//下移</span> this<span class="hljs-subst" style="color: rgb(0,102); Box-sizing: border-Box;">::WHITE</span>)<span class="hljs-comment" style="color: rgb(136,0); Box-sizing: border-Box;">//点到白块</span> { <span class="hljs-comment" style="color: rgb(136,0); Box-sizing: border-Box;">"wrong.wav"</span>); Scene <span class="hljs-subst" style="color: rgb(0,0); Box-sizing: border-Box;">*</span> scene <span class="hljs-subst" style="color: rgb(0,0); Box-sizing: border-Box;">=</span> LayerFailed<span class="hljs-tag" style="color: rgb(0,102); Box-sizing: border-Box;">::scene</span>(); Director<span class="hljs-tag" style="color: rgb(0,0); Box-sizing: border-Box;">-></span>replaceScene(scene);<span class="hljs-comment" style="color: rgb(136,0); Box-sizing: border-Box;">//切换到失败场景</span> } break; } } <span class="hljs-keyword" style="color: rgb(0,136); Box-sizing: border-Box;">return</span> <span class="hljs-literal" style="color: rgb(0,102); Box-sizing: border-Box;">false</span>; }</code><ul class="pre-numbering" style="Box-sizing: border-Box; position: absolute; width: 50px; background-color: rgb(238,221); list-style: none; text-align: right;"><li style="Box-sizing: border-Box; padding: 0px 5px;">1</li><li style="Box-sizing: border-Box; padding: 0px 5px;">2</li><li style="Box-sizing: border-Box; padding: 0px 5px;">3</li><li style="Box-sizing: border-Box; padding: 0px 5px;">4</li><li style="Box-sizing: border-Box; padding: 0px 5px;">5</li><li style="Box-sizing: border-Box; padding: 0px 5px;">6</li><li style="Box-sizing: border-Box; padding: 0px 5px;">7</li><li style="Box-sizing: border-Box; padding: 0px 5px;">8</li><li style="Box-sizing: border-Box; padding: 0px 5px;">9</li><li style="Box-sizing: border-Box; padding: 0px 5px;">10</li><li style="Box-sizing: border-Box; padding: 0px 5px;">11</li><li style="Box-sizing: border-Box; padding: 0px 5px;">12</li><li style="Box-sizing: border-Box; padding: 0px 5px;">13</li><li style="Box-sizing: border-Box; padding: 0px 5px;">14</li><li style="Box-sizing: border-Box; padding: 0px 5px;">15</li><li style="Box-sizing: border-Box; padding: 0px 5px;">16</li><li style="Box-sizing: border-Box; padding: 0px 5px;">17</li><li style="Box-sizing: border-Box; padding: 0px 5px;">18</li><li style="Box-sizing: border-Box; padding: 0px 5px;">19</li><li style="Box-sizing: border-Box; padding: 0px 5px;">20</li><li style="Box-sizing: border-Box; padding: 0px 5px;">21</li><li style="Box-sizing: border-Box; padding: 0px 5px;">22</li><li style="Box-sizing: border-Box; padding: 0px 5px;">23</li><li style="Box-sizing: border-Box; padding: 0px 5px;">24</li><li style="Box-sizing: border-Box; padding: 0px 5px;">25</li><li style="Box-sizing: border-Box; padding: 0px 5px;">26</li><li style="Box-sizing: border-Box; padding: 0px 5px;">27</li><li style="Box-sizing: border-Box; padding: 0px 5px;">28</li><li style="Box-sizing: border-Box; padding: 0px 5px;">29</li><li style="Box-sizing: border-Box; padding: 0px 5px;">30</li><li style="Box-sizing: border-Box; padding: 0px 5px;">31</li><li style="Box-sizing: border-Box; padding: 0px 5px;">32</li><li style="Box-sizing: border-Box; padding: 0px 5px;">33</li><li style="Box-sizing: border-Box; padding: 0px 5px;">34</li><li style="Box-sizing: border-Box; padding: 0px 5px;">35</li><li style="Box-sizing: border-Box; padding: 0px 5px;">36</li><li style="Box-sizing: border-Box; padding: 0px 5px;">37</li><li style="Box-sizing: border-Box; padding: 0px 5px;">38</li><li style="Box-sizing: border-Box; padding: 0px 5px;">39</li><li style="Box-sizing: border-Box; padding: 0px 5px;">40</li><li style="Box-sizing: border-Box; padding: 0px 5px;">41</li><li style="Box-sizing: border-Box; padding: 0px 5px;">42</li></ul>Box-sizing: border-Box; margin-top: 0px; margin-bottom: 1.1em; font-family: 'Source Code Pro',102); Box-sizing: border-Box;">::moveDown</span>() { <span class="hljs-keyword" style="color: rgb(0,136); Box-sizing: border-Box;">if</span> (this<span class="hljs-subst" style="color: rgb(0,0); Box-sizing: border-Box;">-></span>getLineCount() <span class="hljs-subst" style="color: rgb(0,102); Box-sizing: border-Box;">20</span>)<span class="hljs-comment" style="color: rgb(136,0); Box-sizing: border-Box;">//如果行号小于20</span> { this<span class="hljs-subst" style="color: rgb(0,0); Box-sizing: border-Box;">-></span>addNormalLineBlocks(<span class="hljs-number" style="color: rgb(0,102); Box-sizing: border-Box;">4</span>);<span class="hljs-comment" style="color: rgb(136,0); Box-sizing: border-Box;">//则添加一正常行,并设置行号为4</span> } <span class="hljs-keyword" style="color: rgb(0,136); Box-sizing: border-Box;">if</span> (<span class="hljs-subst" style="color: rgb(0,0); Box-sizing: border-Box;">!</span>showEnd)<span class="hljs-comment" style="color: rgb(136,0); Box-sizing: border-Box;">//如果行号不小于20且标志showEnd为false</span> { this<span class="hljs-subst" style="color: rgb(0,0); Box-sizing: border-Box;">-></span>addEndLineBlock();<span class="hljs-comment" style="color: rgb(136,0); Box-sizing: border-Box;">//则添加结束块</span> showEnd <span class="hljs-subst" style="color: rgb(0,0); Box-sizing: border-Box;">=</span> <span class="hljs-literal" style="color: rgb(0,102); Box-sizing: border-Box;">true</span>;<span class="hljs-comment" style="color: rgb(136,0); Box-sizing: border-Box;">//并将showEnd设置为true</span> } for (auto obj : Block<span class="hljs-tag" style="color: rgb(0,102); Box-sizing: border-Box;">::getBlockVector</span>())<span class="hljs-comment" style="color: rgb(136,0); Box-sizing: border-Box;">//遍历这一行所有块</span> { Block <span class="hljs-subst" style="color: rgb(0,0); Box-sizing: border-Box;">*</span>)obj; b<span class="hljs-subst" style="color: rgb(0,0); Box-sizing: border-Box;">-></span>moveDownAndCleanUp();<span class="hljs-comment" style="color: rgb(136,0); Box-sizing: border-Box;">//将他们下移且从渲染树中删除</span> } }</code><ul class="pre-numbering" style="Box-sizing: border-Box; position: absolute; width: 50px; background-color: rgb(238,221); list-style: none; text-align: right;"><li style="Box-sizing: border-Box; padding: 0px 5px;">1</li><li style="Box-sizing: border-Box; padding: 0px 5px;">2</li><li style="Box-sizing: border-Box; padding: 0px 5px;">3</li><li style="Box-sizing: border-Box; padding: 0px 5px;">4</li><li style="Box-sizing: border-Box; padding: 0px 5px;">5</li><li style="Box-sizing: border-Box; padding: 0px 5px;">6</li><li style="Box-sizing: border-Box; padding: 0px 5px;">7</li><li style="Box-sizing: border-Box; padding: 0px 5px;">8</li><li style="Box-sizing: border-Box; padding: 0px 5px;">9</li><li style="Box-sizing: border-Box; padding: 0px 5px;">10</li><li style="Box-sizing: border-Box; padding: 0px 5px;">11</li><li style="Box-sizing: border-Box; padding: 0px 5px;">12</li><li style="Box-sizing: border-Box; padding: 0px 5px;">13</li><li style="Box-sizing: border-Box; padding: 0px 5px;">14</li><li style="Box-sizing: border-Box; padding: 0px 5px;">15</li><li style="Box-sizing: border-Box; padding: 0px 5px;">16</li><li style="Box-sizing: border-Box; padding: 0px 5px;">17</li></ul>moveDown函数中又有一个moveDownAndCleanUp函数,我们把它写在了Block类里面
上面moveDown函数和moveDownAndCleanUp函数都是为了实现玩家在点击时的滚屏效果。
前面说的都是游戏顺利完成的情况,下面来说,游戏失败的情况——在点击到白块时,游戏失败
我们用了一个if语句来判断是否点击到了白块:Box-sizing: border-Box; margin-top: 0px; margin-bottom: 1.1em; font-family: 'Source Code Pro',monospace;font-size:undefined; white-space: pre; border-top-left-radius: 0px; border-top-right-radius: 0px; border-bottom-right-radius: 0px; border-bottom-left-radius: 0px; word-wrap: normal; background-position: initial initial; background-repeat: initial initial;"><span class="hljs-keyword" style="color: rgb(0,102); Box-sizing: border-Box;">::WHITE</span>) { SimpleAudioEngine<span class="hljs-tag" style="color: rgb(0,0); Box-sizing: border-Box;">"wrong.wav"</span>); Scene <span class="hljs-subst" style="color: rgb(0,102); Box-sizing: border-Box;">::scene</span>(); Director<span class="hljs-tag" style="color: rgb(0,0); Box-sizing: border-Box;">-></span>replaceScene(scene); }</code><ul class="pre-numbering" style="Box-sizing: border-Box; position: absolute; width: 50px; background-color: rgb(238,221); list-style: none; text-align: right;"><li style="Box-sizing: border-Box; padding: 0px 5px;">1</li><li style="Box-sizing: border-Box; padding: 0px 5px;">2</li><li style="Box-sizing: border-Box; padding: 0px 5px;">3</li><li style="Box-sizing: border-Box; padding: 0px 5px;">4</li><li style="Box-sizing: border-Box; padding: 0px 5px;">5</li><li style="Box-sizing: border-Box; padding: 0px 5px;">6</li></ul>如果踩到白块,则播放 wrong 音效,并且切换到游戏失败的场景。
首先说说计时功能:
1.首先设置一个标志和一个变量
2.再创建一个 Label 来显示时间3.然后再 写两个函数计时器startTimer和stopTimer4.然后将startTimer与stopTimer分别放入触摸响应函数的if条件中Box-sizing: border-Box; margin-top: 0px; margin-bottom: 1.1em; font-family: 'Source Code Pro',102); Box-sizing: border-Box;">::BLACK</span>) { SimpleAudioEngine<span class="hljs-tag" style="color: rgb(0,0); Box-sizing: border-Box;">"onclick.wav"</span>); this<span class="hljs-subst" style="color: rgb(0,0); Box-sizing: border-Box;">-></span>startTimer();<span class="hljs-comment" style="color: rgb(136,0); Box-sizing: border-Box;">//开启计时器</span> b<span class="hljs-subst" style="color: rgb(0,102); Box-sizing: border-Box;">::GRAY</span>); this<span class="hljs-subst" style="color: rgb(0,0); Box-sizing: border-Box;">-></span>moveDown(); } <span class="hljs-keyword" style="color: rgb(0,102); Box-sizing: border-Box;">::GREEN</span> ) { SimpleAudioEngine<span class="hljs-tag" style="color: rgb(0,0); Box-sizing: border-Box;">"gamewin.wav"</span>); this<span class="hljs-subst" style="color: rgb(0,0); Box-sizing: border-Box;">-></span>stopTimer();<span class="hljs-comment" style="color: rgb(136,0); Box-sizing: border-Box;">//关闭计时器</span> this<span class="hljs-subst" style="color: rgb(0,0); Box-sizing: border-Box;">-></span>moveDown(); } </code><ul class="pre-numbering" style="Box-sizing: border-Box; position: absolute; width: 50px; background-color: rgb(238,221); list-style: none; text-align: right;"><li style="Box-sizing: border-Box; padding: 0px 5px;">1</li><li style="Box-sizing: border-Box; padding: 0px 5px;">2</li><li style="Box-sizing: border-Box; padding: 0px 5px;">3</li><li style="Box-sizing: border-Box; padding: 0px 5px;">4</li><li style="Box-sizing: border-Box; padding: 0px 5px;">5</li><li style="Box-sizing: border-Box; padding: 0px 5px;">6</li><li style="Box-sizing: border-Box; padding: 0px 5px;">7</li><li style="Box-sizing: border-Box; padding: 0px 5px;">8</li><li style="Box-sizing: border-Box; padding: 0px 5px;">9</li><li style="Box-sizing: border-Box; padding: 0px 5px;">10</li><li style="Box-sizing: border-Box; padding: 0px 5px;">11</li><li style="Box-sizing: border-Box; padding: 0px 5px;">12</li><li style="Box-sizing: border-Box; padding: 0px 5px;">13</li><li style="Box-sizing: border-Box; padding: 0px 5px;">14</li><li style="Box-sizing: border-Box; padding: 0px 5px;">15</li></ul>
再说说播放音乐的功能:1.首先要包含头文件